Created
July 12, 2018 20:45
-
-
Save gquemener/b6019a4079d17d3c226cff8caaca3291 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A | |
{ | |
} | |
class B | |
{ | |
} | |
class C | |
{ | |
} | |
$map = [ | |
A::class => function(A $a) { | |
echo "A".PHP_EOL; | |
}, | |
B::class => function(B $b) { | |
echo 'B'.PHP_EOL; | |
} | |
]; | |
$bus = function (array $map): callable | |
{ | |
return function (object $cmd) use ($map): void | |
{ | |
$name = get_class($cmd); | |
if (!isset($map[$name])) { | |
throw new Exception(sprintf('Command "%s" unknown', $name)); | |
} | |
$map[$name]($cmd); | |
}; | |
}; | |
$dispatch = $bus($map); | |
$dispatch(new A); | |
$dispatch(new B); | |
$dispatch(new C); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment