Skip to content

Instantly share code, notes, and snippets.

@jjbubudi
Last active August 29, 2015 14:14
Show Gist options
  • Save jjbubudi/c1a3a5fcc3a04e9cbb57 to your computer and use it in GitHub Desktop.
Save jjbubudi/c1a3a5fcc3a04e9cbb57 to your computer and use it in GitHub Desktop.
Guava clone
$subscriberClass = new \ReflectionClass(TestSubscriber::class);
$annotationReader = new AnnotationReader();
$mappings = [];
// Registry
foreach ($subscriberClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$annotation = $annotationReader->getMethodAnnotation($method, Subscribe::class);
$parameters = $method->getParameters();
if (!$annotation || empty($parameters)) {
continue;
}
$eventClass = $parameters[0]->getClass()->getName();
if (!isset($mappings[$eventClass])) {
$mappings[$eventClass] = [];
}
$mappings[$eventClass][] = $method;
}
// Fire
foreach ($mappings as $eventClass => $methods) {
/** @var \ReflectionMethod $method */
foreach ($methods as $method) {
$declaringClass = $method->getDeclaringClass()->getName();
$method->invoke(new $declaringClass, new $eventClass);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment