{@inheritDoc}

Lazily loads listeners for this event from the dependency injection
container.

@throws \InvalidArgumentException if the service is not defined
484007
public function dispatch($eventName, Event $event = null)
    {
        $this->lazyLoad($eventName);

        return parent::dispatch($eventName, $event);
    }
245952
function initialize($instance, $container = null)
    {
        ($container !== null) ?: $container = $this->target;

        foreach(clone $this->_getPriorityQueue() as $initializer)
        {
            if (!is_callable($initializer) && !$initializer instanceof iContainerInitializer)
                throw new \InvalidArgumentException(sprintf(
                    'Invalid Initializer Provided. (%s).'
                    , \Poirot\Std\flatten($initializer)
                ));

            if ($initializer instanceof iContainerInitializer)
                $initializer->initialize($instance, $container);
            else /* Callable */
                $this->_initializeCallable($initializer, $instance, $container);
        }
    }
186347
private function initializeServiceContainer()
    {
        $this->containerManager = new ServiceContainerManager($this->dispatcher, $this->getLogger());
        $class = $this->getContainerClass();
        $container = $this->containerManager->initializeServiceContainer($class);
        $this->dispatcher->notify(new sfEvent($container, 'service_container.loaded'));

    }
344855
public function registerEventHandler($event, callable $handler)
    {
        if (empty($event)) {
            throw new InvalidArgumentException('Event name is required');
        }

        if (is_callable($handler)) {
            if (empty($this->event_handlers[$event])) {
                $this->event_handlers[$event] = [];
            }

            $this->event_handlers[$event][] = $handler;
        } else {
            throw new InvalidArgumentException('Handler not callable');
        }
    }
938925
protected function ensureListener($listener): Listener
    {
        if (Assert::instanceOf($listener, '\\One\\Event\\Listener')) {
            return $listener;
        } elseif (Assert::instanceOf($listener, '\\Closure') || Assert::array($listener)) {
            return new Listener($listener);
        } elseif (Assert::string($listener)) {
            return Reflection::newInstance($listener);
        }

        throw new InvalidArgumentException(
            '`Listener` must be `array`, `Closure` or extends class `One\Event\Listener`'
        );
    }
672567
private function loadDynamicServices(): void
    {
        $this->startMeasure(__METHOD__);

        /** @var array $objects */
        $objects = App::$Properties->getAll('object');
        if (!Any::isArray($objects)) {
            throw new NativeException('Object configurations is not loaded: /Private/Config/Object.php');
        }

        // each all objects as service_name => service_instance()
        foreach ($objects as $name => $instance) {
            // check if definition of object is exist and services list contains it or is null to auto build
            if (property_exists(get_called_class(), $name) && $instance instanceof \Closure && (isset($this->_services[$name]) || $this->_services === null)) {
                if ($this->_services[$name] === true || $this->_services === null) { // initialize from configs
                    self::${$name} = $instance();
                } elseif (is_callable($this->_services[$name])) { // raw initialization from App::run()
                    self::${$name} = $this->_services[$name]();
                }
            } elseif (Str::startsWith('_', $name)) { // just anonymous callback without entry-point
                @call_user_func($instance);
            }
        }

        $this->stopMeasure(__METHOD__);
    }
347288
public function setListener($event, callable $listener)
    {
        if (!is_string($event) || empty($event)) {
            throw new \InvalidArgumentException('The event name is invalid');
        }

        $this->listeners[$event] = $listener;
    }
340888
private function initBundleExposedActions()
    {
        if (true === $this->isEnabled()) {
            $container = $this->getApplication()->getContainer();
            foreach ((array) $this->getProperty('exposed_actions') as $controllerId => $actions) {
                if (false === $container->has($controllerId)) {
                    throw new \InvalidArgumentException(
                        "Exposed controller with id `$controllerId` not found for ".$this->getId()
                    );
                }

                $controller = $container->get($controllerId);
                $this->formatAndInjectExposedAction($controller, $actions);
            }
        }
    }
103343
private function processEvents()
	{
		$builder = $this->getContainerBuilder();

		$eventManager = $builder->addDefinition($this->prefix('eventManager'))
			->setClass(EventManager::class);

		$entityListenerResolver = $builder->addDefinition($this->prefix('entityListenerResolver'))
			->setClass(ContainerEntityListenerResolver::class);

		$rc = new \ReflectionClass(Events::class);
		$events = $rc->getConstants();

		foreach ($this->config['listeners'] as $event => $listeners) {
			if (!isset($events[$event])) {
				throw new InvalidStateException("Event '$event' is not valid doctrine event. See class "
					. Events::class . ' for list of available events.');
			}
			foreach ((array) $listeners as $listener) {
				$builder->getDefinition($this->prefix('eventManager'))->addSetup('$service->addEventListener(['
					. Events::class . '::?], ?)', [$event, $listener]);
			}
		}
	}
520254
public function loadConfiguration(): void
	{
		if (!class_exists('Symfony\Component\Console\Application')) {
			throw new ServiceCreationException('Missing Symfony\Component\Console\Application service');
		}

		// Skip if it's not CLI mode
		if (!$this->cliMode) return;

		$builder = $this->getContainerBuilder();

		// Helpers
		$builder->addDefinition($this->prefix('connectionHelper'))
			->setFactory(ConnectionHelper::class)
			->setAutowired(false);

		//Commands
		$builder->addDefinition($this->prefix('importCommand'))
			->setFactory(ImportCommand::class)
			->addTag('console.command', 'dbal:import')
			->setAutowired(false);

		$builder->addDefinition($this->prefix('reservedWordsCommand'))
			->setFactory(ReservedWordsCommand::class)
			->addTag('console.command', 'dbal:reserved-words')
			->setAutowired(false);

		$builder->addDefinition($this->prefix('runSqlCommand'))
			->setFactory(RunSqlCommand::class)
			->addTag('console.command', 'dbal:run-sql')
			->setAutowired(false);
	}
732249
private function loadApiDefinition($forceReload = false)
    {
        $definitionLoader = $this->getDefinitionLoader();

        $supported = $definitionLoader->supports($this->options['uri']);

        if ($forceReload || ($this->definition == null && $supported)) {
            $this->definition = $definitionLoader->load($this->options['uri']);
        } elseif (!$supported) {
            throw new \RuntimeException("This resource (".$this->options['uri'].") is not supported.");
        }
    }
879
public static function flushEventListeners()
    {
        if (! isset(static::$dispatcher)) {
            return;
        }

        $instance = new static;

        foreach ($instance->getObservableEvents() as $event) {
            static::$dispatcher->forget("eloquent.{$event}: ".static::class);
        }

        foreach (array_values($instance->dispatchesEvents) as $event) {
            static::$dispatcher->forget($event);
        }
    }
972054
public function addListener( $event, EventListener $listener ) {

		if ( !is_string( $event ) ) {
			throw new InvalidArgumentException( "Expected a string" );
		}

		$event = strtolower( $event );

		if ( !isset( $this->dispatchableListeners[$event] ) ) {
			$this->dispatchableListeners[$event] = array();
		}

		$this->dispatchableListeners[$event][] = $listener;
	}
870302
private function addListenerServicePattern($eventPattern, $callback, $priority = 0)
    {
        if (!is_array($callback) || 2 !== count($callback)) {
            throw new \InvalidArgumentException('Expected an array("service", "method") argument');
        }

        $container = $this->container;
        $listenerProvider = function() use ($container, $callback) {
            return array($container->get($callback[0]), $callback[1]);
        };

        $this->addListenerPattern(new LazyListenerPattern($eventPattern, $listenerProvider, $priority));
    }
424746
public function initializeEventListeners()
    {
        $configuration = $this->getConfiguration();
        $eventDispatcher = $this->getContainer()->get('event_dispatcher');
        foreach ($configuration->getEventListeners() as $eventName => $listeners) {
            foreach ($listeners as $listener) {
                list($listenerClassName, $listenerMethodName) = explode('::', $listener);

                $listenerInstance = ObjectFactory::getInstance()->newInstance($listenerClassName);
                if ($listenerInstance !== null) {
                    $eventDispatcher->addListener($eventName, array($listenerInstance, $listenerMethodName));
                }
            }
        }

        foreach ($configuration->getEventSubscribers() as $subscriber) {
            $subscriberInstance = ObjectFactory::getInstance()->newInstance($subscriber['class'], $subscriber);
            if ($subscriberInstance instanceof EventSubscriberInterface) {
                $eventDispatcher->addSubscriber($subscriberInstance);
            }
        }
    }
1275510
public function attach($event, Closure $listener)
    {
        if (!is_callable($listener)) {
            throw new InvalidArgumentException(sprintf(
                'Second argument must be callable not "%s"', gettype($listener)
            ));
        }

        if (!is_string($event)) {
            throw new InvalidArgumentException(sprintf(
                'First argument must be string, got "%s"', gettype($event)
            ));
        }

        $this->listeners[$event] = $listener;
        return $this;
    }
733854
public function loadPluginConfiguration(): void
	{
		// Receive container builder
		$builder = $this->getContainerBuilder();
		$globalConfig = $this->compiler->getExtension()->getConfig();

		$builder->addDefinition($this->prefix('dispatcher'))
			->setFactory(JsonDispatcher::class)
			->setAutowired(false);

		// Catch exception only in debug mode if explicitly enabled
		$catchException = !$globalConfig['debug'] || $globalConfig['catchException'];

		$errorHandler = $builder->addDefinition($this->prefix('errorHandler'))
			->setFactory(SimpleErrorHandler::class)
			->setType(IErrorHandler::class)
			->addSetup('setCatchException', [$catchException]);

		// Set handler with logging, if logger available
		if ($builder->findByType(LoggerInterface::class) !== []) {
			$errorHandler->setFactory(PsrLogErrorHandler::class);
		}

		$builder->addDefinition($this->prefix('dispatcher.wrapper'))
			->setFactory(WrappedDispatcher::class, ['@' . $this->prefix('dispatcher')]);

		$builder->addDefinition($this->prefix('application'))
			->setFactory(Application::class)
			->setType(IApplication::class);

		$builder->addDefinition($this->prefix('router'))
			->setType(IRouter::class)
			->setFactory(SimpleRouter::class);

		$builder->addDefinition($this->prefix('handler'))
			->setType(IHandler::class)
			->setFactory(ServiceHandler::class);

		$builder->addDefinition($this->prefix('schema'))
			->setFactory(Schema::class);
	}