/** * Get the event subscriber. * * @return EventSubscriber Returns an instance of the EventSubscriber class. */ protected function getEventSubscriber() { // Check if the event subscriber has already been initialized if (!$this->eventSubscriber) { // Get the reflection class $reflectionClass = new \ReflectionClass($this->getEventSubscriberClass()); // Get the class instance $this->eventSubscriber = $reflectionClass->newInstance(); // Set the annotation reader $this->eventSubscriber->setAnnotationReader($this->getAnnotationReader()); } return $this->eventSubscriber; }
/** * Adds an event subscriber that listens on the specified events. * Internally this will convert all handlers to listeners. * * @param EventSubscriber $eventSubscriber The subscriber * @throws \InvalidArgumentException * * @api */ public function addSubscriber(EventSubscriber $eventSubscriber) { $listeners = $eventSubscriber->getEvents(); foreach ($listeners as $listener => $data) { if (is_array($data)) { $data = new SubscriberConfiguration($data[0], $data[1] ?? self::USE_ALL, $data[2] ?? 100); } if (!$data instanceof SubscriberConfiguration) { throw new \InvalidArgumentException('SubscriberConfiguration for ' . $listener . ' in ' . get_class($eventSubscriber) . ' is invalid!'); } // horrible hack due to array not being a callable in this case ... whut? $listenerCallable = function ($events) use($eventSubscriber, $listener) { $eventSubscriber->{$listener}($events); }; $this->addWeightedListener($listenerCallable, $data->getUseType(), $data->getWeight(), ...$data->getEvents()); } }