ControllerEvent example

$event = new ControllerArgumentsEvent(new TestHttpKernel()$controller['test']new Request(), HttpKernelInterface::MAIN_REQUEST);
        $event->setController($controller[]);

        $this->assertSame([]$event->getAttributes());
    }

    public function testGetAttributes()
    {
        $controller = new AttributeController();
        $request = new Request();

        $controllerEvent = new ControllerEvent(new TestHttpKernel()$controller$request, HttpKernelInterface::MAIN_REQUEST);

        $event = new ControllerArgumentsEvent(new TestHttpKernel()$controllerEvent['test']new Request(), HttpKernelInterface::MAIN_REQUEST);

        $expected = [
            Bar::class => [
                new Bar('class'),
                new Bar('method'),
            ],
            Baz::class => [
                new Baz(),
            ],
        ];
final class ControllerArgumentsEvent extends KernelEvent
{
    private ControllerEvent $controllerEvent;
    private array $arguments;
    private array $namedArguments;

    public function __construct(HttpKernelInterface $kernel, callable|ControllerEvent $controller, array $arguments, Request $request, ?int $requestType)
    {
        parent::__construct($kernel$request$requestType);

        if (!$controller instanceof ControllerEvent) {
            $controller = new ControllerEvent($kernel$controller$request$requestType);
        }

        $this->controllerEvent = $controller;
        $this->arguments = $arguments;
    }

    public function getController(): callable
    {
        return $this->controllerEvent->getController();
    }

    
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);

        if ($event->hasResponse()) {
            return $this->filterResponse($event->getResponse()$request$type);
        }

        // load controller         if (false === $controller = $this->resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
        }

        $event = new ControllerEvent($this$controller$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
        $controller = $event->getController();

        // controller arguments         $arguments = $this->argumentResolver->getArguments($request$controller$event->getControllerReflector());

        $event = new ControllerArgumentsEvent($this$event$arguments$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
        $controller = $event->getController();
        $arguments = $event->getArguments();

        
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);

        if ($event->hasResponse()) {
            return $this->filterResponse($event->getResponse()$request$type);
        }

        // load controller         if (false === $controller = $this->resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
        }

        $event = new ControllerEvent($this$controller$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
        $controller = $event->getController();

        // controller arguments         $arguments = $this->argumentResolver->getArguments($request$controller$event->getControllerReflector());

        $event = new ControllerArgumentsEvent($this$event$arguments$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
        $controller = $event->getController();
        $arguments = $event->getArguments();

        
return $response;
    }

    /** * Inject the given controller callable into the data collector. */
    protected function injectController($collector$controller$request)
    {
        $resolver = $this->createMock(ControllerResolverInterface::class);
        $httpKernel = new HttpKernel(new EventDispatcher()$resolver, null, $this->createMock(ArgumentResolverInterface::class));
        $event = new ControllerEvent($httpKernel$controller$request, HttpKernelInterface::MAIN_REQUEST);
        $collector->onKernelController($event);
    }

    private function getCookieByName(Response $response$name)
    {
        foreach ($response->headers->getCookies() as $cookie) {
            if ($cookie->getName() == $name) {
                return $cookie;
            }
        }

        
final class ControllerArgumentsEvent extends KernelEvent
{
    private ControllerEvent $controllerEvent;
    private array $arguments;
    private array $namedArguments;

    public function __construct(HttpKernelInterface $kernel, callable|ControllerEvent $controller, array $arguments, Request $request, ?int $requestType)
    {
        parent::__construct($kernel$request$requestType);

        if (!$controller instanceof ControllerEvent) {
            $controller = new ControllerEvent($kernel$controller$request$requestType);
        }

        $this->controllerEvent = $controller;
        $this->arguments = $arguments;
    }

    public function getController(): callable
    {
        return $this->controllerEvent->getController();
    }

    
public function testGetName()
    {
        $collector = new RouterDataCollector();

        $this->assertEquals('router', $collector->getName());
    }

    protected function createControllerEvent(Request $request): ControllerEvent
    {
        $kernel = $this->createMock(KernelInterface::class);

        return new ControllerEvent($kernelfunction D) {}$request, null);
    }
}
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

class ControllerEventTest extends TestCase
{
    /** * @dataProvider provideGetAttributes */
    public function testGetAttributes(callable $controller)
    {
        $event = new ControllerEvent(new TestHttpKernel()$controllernew Request(), HttpKernelInterface::MAIN_REQUEST);

        $expected = [
            Bar::class => [
                new Bar('class'),
                new Bar('method'),
            ],
            Baz::class => [
                new Baz(),
            ],
        ];

        
Home | Imprint | This part of the site doesn't use cookies.