Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ControllerEvent.php

class ControllerEvent

Allows filtering of a controller callable.

You can call getController() to retrieve the current controller. With setController() you can set a new controller that is used in the processing of the request.

Controllers should be callables.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Contracts\EventDispatcher\Event implements \Psr\EventDispatcher\StoppableEventInterface
    • class \Symfony\Component\HttpKernel\Event\KernelEvent extends \Symfony\Contracts\EventDispatcher\Event
      • class \Symfony\Component\HttpKernel\Event\ControllerEvent extends \Symfony\Component\HttpKernel\Event\KernelEvent

Expanded class hierarchy of ControllerEvent

6 files declare their use of ControllerEvent
EarlyRenderingControllerWrapperSubscriber.php in core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php
HttpKernel.php in vendor/symfony/http-kernel/HttpKernel.php
KernelEvents.php in vendor/symfony/http-kernel/KernelEvents.php
PathAliasSubscriber.php in core/modules/path_alias/src/EventSubscriber/PathAliasSubscriber.php
RequestDataCollector.php in vendor/symfony/http-kernel/DataCollector/RequestDataCollector.php

... See full list

File

vendor/symfony/http-kernel/Event/ControllerEvent.php, line 28

Namespace

Symfony\Component\HttpKernel\Event
View source
final class ControllerEvent extends KernelEvent {
    private string|array|object $controller;
    private \ReflectionFunctionAbstract $controllerReflector;
    private array $attributes;
    public function __construct(HttpKernelInterface $kernel, callable $controller, Request $request, ?int $requestType) {
        parent::__construct($kernel, $request, $requestType);
        $this->setController($controller);
    }
    public function getController() : callable {
        return $this->controller;
    }
    public function getControllerReflector() : \ReflectionFunctionAbstract {
        return $this->controllerReflector;
    }
    
    /**
     * @param array<class-string, list<object>>|null $attributes
     */
    public function setController(callable $controller, ?array $attributes = null) : void {
        if (null !== $attributes) {
            $this->attributes = $attributes;
        }
        if (isset($this->controller) && ($controller instanceof \Closure ? $controller == $this->controller : $controller === $this->controller)) {
            $this->controller = $controller;
            return;
        }
        if (null === $attributes) {
            unset($this->attributes);
        }
        if (\is_array($controller) && method_exists(...$controller)) {
            $this->controllerReflector = new \ReflectionMethod(...$controller);
        }
        elseif (\is_string($controller) && str_contains($controller, '::')) {
            $this->controllerReflector = new \ReflectionMethod(...explode('::', $controller, 2));
        }
        else {
            $this->controllerReflector = new \ReflectionFunction($controller(...));
        }
        $this->controller = $controller;
    }
    
    /**
     * @template T of class-string|null
     *
     * @param T $className
     *
     * @return array<class-string, list<object>>|list<object>
     *
     * @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
     */
    public function getAttributes(?string $className = null) : array {
        if (isset($this->attributes)) {
            return null === $className ? $this->attributes : $this->attributes[$className] ?? [];
        }
        if (\is_array($this->controller) && method_exists(...$this->controller)) {
            $class = new \ReflectionClass($this->controller[0]);
        }
        elseif (\is_string($this->controller) && false !== ($i = strpos($this->controller, '::'))) {
            $class = new \ReflectionClass(substr($this->controller, 0, $i));
        }
        else {
            $class = $this->controllerReflector instanceof \ReflectionFunction && $this->controllerReflector
                ->isAnonymous() ? null : $this->controllerReflector
                ->getClosureCalledClass();
        }
        $this->attributes = [];
        foreach (array_merge($class?->getAttributes() ?? [], $this->controllerReflector
            ->getAttributes()) as $attribute) {
            if (class_exists($attribute->getName())) {
                $this->attributes[$attribute->getName()][] = $attribute->newInstance();
            }
        }
        return null === $className ? $this->attributes : $this->attributes[$className] ?? [];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ControllerEvent::$attributes private property
ControllerEvent::$controller private property
ControllerEvent::$controllerReflector private property
ControllerEvent::getAttributes public function @template T of class-string|null
ControllerEvent::getController public function
ControllerEvent::getControllerReflector public function
ControllerEvent::setController public function
ControllerEvent::__construct public function Overrides KernelEvent::__construct
Event::$propagationStopped private property
Event::isPropagationStopped public function Is propagation stopped? Overrides StoppableEventInterface::isPropagationStopped
Event::stopPropagation public function Stops the propagation of the event to further event listeners. 1
KernelEvent::getKernel public function Returns the kernel in which this event was thrown.
KernelEvent::getRequest public function Returns the request the kernel is currently processing.
KernelEvent::getRequestType public function Returns the request type the kernel is currently processing.
KernelEvent::isMainRequest public function Checks if this is the main request.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal