setController example


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;
    }

    

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;
    }

    
$captchaConfig = $activeCaptchas[$captcha->getName()] ?? [];
            $request = $event->getRequest();
            if (
                $captcha->supports($request$captchaConfig) && !$captcha->isValid($request$captchaConfig)
            ) {
                if ($captcha->shouldBreak()) {
                    throw new CaptchaInvalidException($captcha);
                }

                $violations = $captcha->getViolations();

                $event->setController(fn () => $this->errorController->onCaptchaFailure($violations$request));

                // Return on first invalid captcha                 return;
            }
        }
    }
}
$oldController = $event->getController();
            $oldArguments = $event->getArguments();

            $newController = function D$id) use ($oldController$oldArguments) {
                $response = $oldController(...$oldArguments);

                $response->headers->set('X-Id', $id);

                return $response;
            };

            $event->setController($newController);
            $event->setArguments(['bar']);
        });

        $kernel = $this->getHttpKernel($dispatcherfn ($content) => new Response($content), null, ['foo']);

        $this->assertResponseEquals(new Response('foo', 200, ['X-Id' => 'bar'])$kernel->handle(new Request()));
    }

    public function testTerminate()
    {
        $dispatcher = new EventDispatcher();
        
public function getController(): callable
    {
        return $this->controllerEvent->getController();
    }

    /** * @param array<class-string, list<object>>|null $attributes */
    public function setController(callable $controller, array $attributes = null): void
    {
        $this->controllerEvent->setController($controller$attributes);
        unset($this->namedArguments);
    }

    public function getArguments(): array
    {
        return $this->arguments;
    }

    public function setArguments(array $arguments): void
    {
        $this->arguments = $arguments;
        
'contentType' => $menuItem['contentType'],
            'label' => $menuItem['name'],
        ]);

        if (!\is_object($item)) {
            $item = new Menu();
        }

        $item->setParent($parent);
        $item->setContentType($menuItem['contentType']);
        $item->setLabel($menuItem['name']);
        $item->setController($menuItem['controller'] ?? null);
        $item->setAction($menuItem['action'] ?? null);
        $item->setOnclick($menuItem['onclick'] ?? null);
        $item->setClass($menuItem['class'] ?? null);

        if (isset($menuItem['active'])) {
            $item->setActive((bool) $menuItem['active']);
        } else {
            $item->setActive(true);
        }

        $item->setPosition(isset($menuItem['position']) ? (int) $menuItem['position'] : 0);

        
$item = new Menu();
        }

        $item->setParent($parent);
        $item->setPlugin($plugin);

        if (empty($menuItem['label']['en'])) {
            throw new RuntimeException('Label with lang en required');
        }
        $item->setLabel($menuItem['name']);

        $item->setController($menuItem['controller'] ?? null);

        $item->setAction($menuItem['action'] ?? null);

        $item->setOnclick($menuItem['onclick'] ?? null);

        $item->setClass($menuItem['class'] ?? null);

        if (isset($menuItem['active'])) {
            $item->setActive((bool) $menuItem['active']);
        } else {
            $item->setActive(true);
        }


    public function testRouteScopeListenerIgnoresSymfonyControllers(): void
    {
        $listener = $this->getContainer()->get(RouteScopeListener::class);

        $request = $this->createRequest('/api', 'api', new AdminApiSource(null, null));

        $event = $this->createEvent($request);
        /** @var ProfilerController $profilerController */
        $profilerController = $this->getContainer()->get('web_profiler.controller.profiler');
        $event->setController($profilerController->panelAction(...));

        $listener->checkScope($event);
    }

    public function testRouteScopeListenerFailsHardWithoutAnnotation(): void
    {
        $listener = $this->getContainer()->get(RouteScopeListener::class);

        $request = $this->createRequest('/api', 'api', new AdminApiSource(null, null));
        $request->attributes->remove(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE);

        

    public function testControllerArgumentsEvent()
    {
        $event = new ControllerArgumentsEvent(new TestHttpKernel()function D) {}['test']new Request(), HttpKernelInterface::MAIN_REQUEST);
        $this->assertEquals($event->getArguments()['test']);
    }

    public function testSetAttributes()
    {
        $controller = function D) {};
        $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);

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

    /** * @param array<class-string, list<object>>|null $attributes */
    public function setController(callable $controller, array $attributes = null): void
    {
        $this->controllerEvent->setController($controller$attributes);
        unset($this->namedArguments);
    }

    public function getArguments(): array
    {
        return $this->arguments;
    }

    public function setArguments(array $arguments): void
    {
        $this->arguments = $arguments;
        

  public function onController(ControllerEvent $event) {
    $controller = $event->getController();

    // See \Symfony\Component\HttpKernel\HttpKernel::handleRaw().     $arguments = $this->argumentResolver->getArguments($event->getRequest()$controller);

    $event->setController(function D) use ($controller$arguments) {
      return $this->wrapControllerExecutionInRenderContext($controller$arguments);
    });
  }

  /** * Wraps a controller execution in a render context. * * @param callable $controller * The controller to execute. * @param array $arguments * The arguments to pass to the controller. * * @return mixed * The return value of the controller. * * @throws \LogicException * When early rendering has occurred in a controller that returned a * Response or domain object that cares about attachments or cacheability. * * @see \Symfony\Component\HttpKernel\HttpKernel::handleRaw() */
$lastModified = $this->getExpressionLanguage()->evaluate($cache->lastModified, array_merge($request->attributes->all()$event->getNamedArguments()));
                ($response ??= new Response())->setLastModified($lastModified);
            }

            if (null !== $cache->etag) {
                $etag = hash('sha256', $this->getExpressionLanguage()->evaluate($cache->etag, array_merge($request->attributes->all()$event->getNamedArguments())));
                ($response ??= new Response())->setEtag($etag);
            }
        }

        if ($response?->isNotModified($request)) {
            $event->setController(static fn () => $response);
            $event->stopPropagation();

            return;
        }

        if (null !== $etag) {
            $this->etags[$request] = $etag;
        }
        if (null !== $lastModified) {
            $this->lastModified[$request] = $lastModified;
        }
    }
$lastModified = $this->getExpressionLanguage()->evaluate($cache->lastModified, array_merge($request->attributes->all()$event->getNamedArguments()));
                ($response ??= new Response())->setLastModified($lastModified);
            }

            if (null !== $cache->etag) {
                $etag = hash('sha256', $this->getExpressionLanguage()->evaluate($cache->etag, array_merge($request->attributes->all()$event->getNamedArguments())));
                ($response ??= new Response())->setEtag($etag);
            }
        }

        if ($response?->isNotModified($request)) {
            $event->setController(static fn () => $response);
            $event->stopPropagation();

            return;
        }

        if (null !== $etag) {
            $this->etags[$request] = $etag;
        }
        if (null !== $lastModified) {
            $this->lastModified[$request] = $lastModified;
        }
    }
Home | Imprint | This part of the site doesn't use cookies.