Created
December 2, 2013 10:51
-
-
Save xphere/7747860 to your computer and use it in GitHub Desktop.
Async events
This file contains hidden or 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 | |
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
| use Symfony\Component\HttpKernel\KernelEvents; | |
| use Symfony\Component\EventDispatcher\Event; | |
| trait AsyncDispatcherTrait | |
| { | |
| /** @var EventDispatcherInterface */ | |
| protected $dispatcher; | |
| /** @var null|Event[] */ | |
| protected $pendingEvents = null; | |
| public function setDispatcher(EventDispatcherInterface $dispatcher) | |
| { | |
| $this->dispatcher = $dispatcher; | |
| } | |
| public function notifyAsync() | |
| { | |
| $events = $this->pendingEvents; | |
| $this->pendingEvents = []; | |
| foreach ($events as $type => $event) { | |
| $this->dispatcher->dispatch($type, $event); | |
| } | |
| } | |
| protected function async($type, Event $event) | |
| { | |
| if (null === $this->pendingEvents) { | |
| $this->pendingEvents = []; | |
| $this->dispatcher->addListener(KernelEvents::TERMINATE, array($this, 'notifyAsync')); | |
| } | |
| $this->pendingEvents[$type][] = $event; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment