Skip to content

Instantly share code, notes, and snippets.

@gquemener
Created July 12, 2018 20:45
Show Gist options
  • Save gquemener/b6019a4079d17d3c226cff8caaca3291 to your computer and use it in GitHub Desktop.
Save gquemener/b6019a4079d17d3c226cff8caaca3291 to your computer and use it in GitHub Desktop.
<?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