Assigns a listener to the given event.

@param string $event The event to assign the listener to
@param callable $listener The listener to register
@throws \InvalidArgumentException if the event name is invalid.
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;
    }
840707
public function initialize(Listener $listener)
    {
        $this->listener = $listener;
        $this->topDirs = array_merge($this->topDirs,$listener->getPaths());
        $this->tracker->initialize($listener);
    }
320982
public function triggerEvent($event, $args = [])
    {
        if (!is_string($event) && !$event instanceof Event) {
            throw new \InvalidArgumentException(sprintf(
                '%s::%s only allow Event object or string.',
                get_called_class(),
                __FUNCTION__
            ));
        }

        if (!($event instanceof Event)) {
            if (isset($this->events[$event])) {
                $event = $this->events[$event];
            } else {
                $event = new Event($event);
            }
        }

        $event->mergeArguments($args);

        $listeners = [];

        if (isset($this->listeners[$event->getName()])) {
            foreach ($this->listeners[$event->getName()] as $listener) {
                if ($event->isStopped()) {
                    return $event;
                }

                if (!is_callable($listener)) {
                    $listener = [$listener, $event->getName()];
                }

                if (!is_callable($listener)) {
                    continue;
                }

                $listener($event);

                if ($this->debug) {
                    $listeners[] = $listener;
                }
            }
        }

        if ($this->debug) {
            $executedListeners = [];

            foreach ($listeners as $listener) {
                if ($listener instanceof \Closure) {
                    $ref = new \ReflectionFunction($listener);

                    $listener = ['{Closure}: ' . $ref->getFileName() . ' - Line: ' . $ref->getStartLine()];
                }

                if (is_object($listener[0])) {
                    $listener[0] = get_class($listener[0]);
                }

                $executedListeners[] = $listener;
            }

            $this->collector[] = [
                'event' => $event->getName(),
                'listeners' => $executedListeners,
            ];
        }

        return $event;
    }
628696
protected function makeEventAndListeners($event, $listeners)
    {
        if (! Str::contains($event, '\\')) {
            return;
        }

        $this->call('generate:event', ['name' => $event]);

        $this->makeListeners($event, $listeners);
    }
481784
public function initialize(Listener $listener)
    {
        $this->tracker->initialize($listener);
        $this->wathedPaths = $listener->getPaths();
    }
524668
public function addListener(Listener $listener) : void {
        $this->listeners = $this->listeners->add($listener);
    }
1298064
public function on ($type, callable $listener)
    {
        $args = array_slice(func_get_args(), 2);
        
        $this->emitter->addListener(
            $type,
            function (EventInterface $event, Input\Input $input, Reply\Handler $replyHandler) use ($args, $listener) {
                call_user_func_array($listener, array_merge([$input, $replyHandler], $args));
            }
        );
    }
1305733
private function addEvent($event)
    {
        if (is_string($event)) {
            $event = Event::named($event);
        }

        if (!$event instanceof EventInterface) {
            throw new \InvalidArgumentException(
                'Events should be provides as Event instances or string, received type: ' . gettype($event)
            );
        }

        $this->events[] = $event;

        return $event;
    }
1094386
protected function ensureListener($listener)
    {
        if ($listener instanceof ListenerInterface) {
            return $listener;
        }

        if (is_callable($listener)) {
            return CallbackListener::fromCallable($listener);
        }

        throw new InvalidArgumentException('Listeners should be be ListenerInterface, Closure or callable. Received type: '.gettype($listener));
    }
388189
public function addListener($listener, $event, $prior = null)
    {
        if (!is_callable($event)) {
            return false;
        }

        if (!array_key_exists($listener, $this->listeners)) {
            $this->listeners[$listener] = [];
        }

        if (!$prior) {
            $this->listeners[$listener][] = $event;
        } else {
            array_splice($this->listeners[$listener], $prior, 0, $event);
        }

        return true;
    }
864866
public static function addListener($name, $listener)
    {
        if (!isset(static::$listeners[$name])) {
            static::$listeners[$name] = [];
        }
        if(!is_array($listener)){
            static::$listeners[$name][] = $listener;
        }else{
            static::$listeners[$name] = array_merge(static::$listeners[$name], $listener);
        }

    }
1164308
public function onAfter($event, callable $listener)
    {

        if (strpos($event, '*') !== false) {
            $this->afterAnyListeners[] = $this->toWildcardListener($event, $listener);
            return $this;
        }

        self::parentOnAfter($event, $listener);

        return $this;
    }
281214
public function it_should_only_call_listeners_for_event_until_one_of_them_handles_the_event(
        MockListener $listener,
        Event $event
    ) {
        $event->hasBeenHandled()
              ->willReturn(\true)
              ->shouldBeCalled();
        $listener->method2($event, 'test1', $this)
                 ->willReturn($event);
        $this->addListener('test1', [$listener, 'method2']);
        $this->addListener('test1', [$listener, 'method1'], 'last');
        $this->getListeners()
             ->shouldHaveKey('test1');
        $expected = [0 => [[$listener, 'method2']], -1 => [[$listener, 'method1']]];
        $this->getListeners('test1')
             ->shouldReturn($expected);
        $listener->method2($event, 'test1', $this)
                 ->shouldBeCalled();
        $listener->method1($event, 'test1', $this)
                 ->shouldNotBeCalled();
        $this->trigger('test1', $event);
    }
594617
public function on($event, callable $listener)
    {

        if (strpos($event, '*') !== false) {
            $this->anyListeners[] = $this->toWildcardListener($event, $listener);
            return $this;
        }

        self::parentOn($event, $listener);

        return $this;
    }
899713
public function once($event, callable $listener)
    {
        parent::once($event, $listener);
        $this->stderr->once($event, $listener);
    }
876532
public static function listen($event, $callback)
    {
        if (is_array($event)) {
            foreach ($event as $e) {
                self::listen($e, $callback);
            }
            return;
        }

        if ($event !== '*' and !self::eventName($event)) {
            throw new \InvalidArgumentException('Event "'.$event.'" is not a valid event');
        }

        if (!isset(self::$events[$event])) {
            self::$events[$event] = array();
        }

        self::$events[$event][] = $callback;
    }
1036853
public function addEventListener($events, $listener)
    {
        if (\is_string($listener)) {
            if ($this->initialized) {
                throw new \RuntimeException('Adding lazy-loading listeners after construction is not supported.');
            }

            $hash = '_service_'.$listener;
        } else {
            // Picks the hash code related to that listener
            $hash = spl_object_hash($listener);
        }

        foreach ((array) $events as $event) {
            // Overrides listener if a previous one was associated already
            // Prevents duplicate listeners on same event (same instance only)
            $this->listeners[$event][$hash] = $listener;
        }
    }