Last active
November 27, 2015 00:55
-
-
Save tzkmx/138c4751aea8047241fc to your computer and use it in GitHub Desktop.
Test Symfony Events, modify event with Reflection
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 | |
require 'vendor/autoload.php'; | |
use Symfony\Component\Stopwatch\Stopwatch; | |
use Symfony\Component\EventDispatcher\EventDispatcher; | |
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; | |
use Symfony\Component\EventDispatcher\GenericEvent; | |
use Symfony\Component\EventDispatcher\Event; | |
class EventSubscriber implements EventSubscriberInterface | |
{ | |
public static function getSubscribedEvents() { | |
return array( | |
'data.emit' => [['hello', 20],['world', 5],['eventful', 10]], | |
'data.pass' => [['hello', 20],['world', 5],['eventful', 10]] | |
); | |
} | |
public function __call($name, $args) | |
{ | |
$event = $args[0]; | |
$subject = $event->getSubject(); | |
print_r($subject); | |
if ($event->getName() === 'data.pass') { | |
$r = new ReflectionClass($event); | |
$property = $r->getProperty('subject'); | |
$property->setAccessible(true); | |
unset($subject[$name]); | |
$property->setValue($event, $subject); | |
} | |
print_r(array('name' => $name, 'data' => $event->getSubject())); | |
return $event; | |
} | |
} | |
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); | |
$es = new EventSubscriber(); | |
$dispatcher->addSubscriber($es); | |
$event = new GenericEvent(array('hello' => 1, 'world' => 2, 'eventful' => 3)); | |
$dispatcher->dispatch('data.emit', $event); | |
$dispatcher->dispatch('data.pass', $event); | |
print_r($dispatcher->getCalledListeners()); | |
print_r($dispatcher->getNotCalledListeners()); | |
foreach ($s->getSections() as $section) { | |
print_r("section id: ". $section->getId() . "\n"); | |
foreach ($section->getEvents() as $event) { | |
print_r("\tevent category: ". $event->getCategory() . "\n"); | |
print_r("\tevent memory: ". $event->getMemory() . "\n"); | |
foreach ($event->getPeriods() as $period) { | |
print_r("\t\tperiod time: ". $period->getDuration() . "\n"); | |
print_r("\t\tperiod memory: ". $period->getMemory() . "\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment