Skip to content

Instantly share code, notes, and snippets.

@acairns
Created July 24, 2016 11:33
Show Gist options
  • Save acairns/8a5610a62c7b7fc5af30b7fcc0c58f2e to your computer and use it in GitHub Desktop.
Save acairns/8a5610a62c7b7fc5af30b7fcc0c58f2e to your computer and use it in GitHub Desktop.
Reflection-based Event Emitter
<?php
final class WarpDriveEngaged
{
private $speed;
public function __construct($speed)
{
$this->speed = $speed;
}
public function getSpeed()
{
return $this->speed;
}
}
final class DisableTransporters
{
public function whenWarpDriveIsEngaged(WarpDriveEngaged $event)
{
var_dump($event);
echo "Disabling Transporters!\n";
}
}
final class NewInstanceListenerLocator implements \Cairns\Radiate\Locator\ListenerLocator {
public function locate($fqcn) {
return new $fqcn;
}
}
$registry = new \Cairns\Radiate\Registry\TypehintedClassRegistry;
$registry->register(DisableTransporters::class);
$invoker = new \Cairns\Radiate\Middleware\InvokeListenerMiddleware(
$registry,
new NewInstanceListenerLocator,
new \Cairns\Radiate\Inflector\TypehintMethodInflector
);
$emitter = new \Cairns\Radiate\Emitter([
$invoker
]);
$emitter->emit(new WarpDriveEngaged(9));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment