getController example

$stopwatch->method('start')->willReturn($stopwatchEvent);

        $resolver = new class() implements ControllerResolverInterface {
            public function getController(Request $request): callable|false
            {
                throw new \Exception();
            }
        };

        $traceableResolver = new TraceableControllerResolver($resolver$stopwatch);
        try {
            $traceableResolver->getController(new Request());
        } catch (\Exception $ex) {
        }
    }
}
if ($pathAllowed && $requestAllowed) {
                return;
            }
        }

        throw new InvalidRouteScopeException($masterRequest->attributes->get('_route'));
    }

    private function extractControllerClass(ControllerEvent $event): ?string
    {
        $controllerCallable = \Closure::fromCallable($event->getController());
        $controllerCallable = new \ReflectionFunction($controllerCallable);

        $controller = $controllerCallable->getClosureThis();

        if (!$controller) {
            return null;
        }

        return $controller::class;
    }

    

    public function testControllerInspection($name$callable$expected)
    {
        $c = new RequestDataCollector();
        $request = $this->createRequest();
        $response = $this->createResponse();
        $this->injectController($c$callable$request);
        $c->collect($request$response);
        $c->lateCollect();

        $this->assertSame($expected$c->getController()->getValue(true)sprintf('Testing: %s', $name));
    }

    public static function provideControllerCallables(): array
    {
        // make sure we always match the line number         $controller = new DummyController();

        $r1 = new \ReflectionMethod($controller, 'regularCallable');
        $r2 = new \ReflectionMethod($controller, 'staticControllerMethod');
        $r3 = new \ReflectionClass($controller);

        

        return 'n/a';
    }

    /** * Remembers the controller associated to each request. * * @return void */
    public function onKernelController(ControllerEvent $event)
    {
        $this->controllers[$event->getRequest()] = $event->getController();
    }

    /** * @return bool Whether this request will result in a redirect */
    public function getRedirect(): bool
    {
        return $this->data['redirect'];
    }

    public function getTargetUrl(): ?string
    {
return;
        }

        /** @var list<string> $scope */
        $scope = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);

        if (!\in_array(StorefrontRouteScope::ID, $scope, true)) {
            return;
        }

        /** @var callable(): Response $controller */
        $controller = $event->getController();

        // happens if Controller is a closure         if (!\is_array($controller)) {
            return;
        }

        $isAllowed = $event->getRequest()->attributes->getBoolean('XmlHttpRequest');

        if ($isAllowed) {
            return;
        }

        
public function __construct(ControllerResolverInterface $resolver, Stopwatch $stopwatch)
    {
        $this->resolver = $resolver;
        $this->stopwatch = $stopwatch;
    }

    public function getController(Request $request): callable|false
    {
        $e = $this->stopwatch->start('controller.get_callable');

        try {
            return $this->resolver->getController($request);
        } finally {
            $e->stop();
        }
    }
}

        return $this->data['redirect'] ?? false;
    }

    public function getForwardToken(): ?string
    {
        return $this->data['forward_token'] ?? null;
    }

    public function onKernelController(ControllerEvent $event): void
    {
        $this->controllers[$event->getRequest()] = $event->getController();
    }

    public function onKernelResponse(ResponseEvent $event): void
    {
        if (!$event->isMainRequest()) {
            return;
        }

        if ($event->getRequest()->cookies->has('sf_redirect')) {
            $event->getRequest()->attributes->set('_redirected', true);
        }
    }
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();
    }

    /** * @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);
    }

    
use Symfony\Component\HttpKernel\Controller\ControllerResolver;

class ControllerResolverTest extends TestCase
{
    public function testGetControllerWithoutControllerParameter()
    {
        $logger = $this->createMock(LoggerInterface::class);
        $logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing.');
        $resolver = $this->createControllerResolver($logger);

        $request = Request::create('/');
        $this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
    }

    public function testGetControllerWithLambda()
    {
        $resolver = $this->createControllerResolver();

        $request = Request::create('/');
        $request->attributes->set('_controller', $lambda = function D) {});
        $controller = $resolver->getController($request);
        $this->assertSame($lambda$controller);
    }

    

        $entity = new TestEntity();

        $request = $this->createRequest(new Cache(lastModified: $expression));
        $request->attributes->set('date', new \DateTimeImmutable('Fri, 23 Aug 2013 00:00:00 GMT'));
        $request->headers->add(['If-Modified-Since' => 'Fri, 23 Aug 2013 00:00:00 GMT']);

        $listener = new CacheAttributeListener();
        $controllerArgumentsEvent = new ControllerArgumentsEvent($this->getKernel()fn (TestEntity $test) => new Response()[$entity]$request, null);

        $listener->onKernelControllerArguments($controllerArgumentsEvent);
        $response = $controllerArgumentsEvent->getController()($entity);

        $this->assertSame(304, $response->getStatusCode());
    }

    /** * @testWith ["test.getDate()"] * ["date"] */
    public function testLastModifiedHeader(string $expression)
    {
        $entity = new TestEntity();

        


        $kernel = $this->getHttpKernel($dispatcherfn ($content) => new Response($content));

        $this->assertResponseEquals(new Response('foo')$kernel->handle(new Request()));
    }

    public function testHandleAllowChangingControllerAndArguments()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function D$event) {
            $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);
            
/** * @return void */
    public function onControllerArguments(ControllerArgumentsEvent $event)
    {
        $e = $event->getRequest()->attributes->get('exception');

        if (!$e instanceof \Throwable || false === $k = array_search($e$event->getArguments(), true)) {
            return;
        }

        $r = new \ReflectionFunction($event->getController()(...));
        $r = $r->getParameters()[$k] ?? null;

        if ($r && (!($r = $r->getType()) instanceof \ReflectionNamedType || FlattenException::class === $r->getName())) {
            $arguments = $event->getArguments();
            $arguments[$k] = FlattenException::createFromThrowable($e);
            $event->setArguments($arguments);
        }
    }

    public static function getSubscribedEvents(): array
    {
        
$this->expectException(\LogicException::class);
    $this->controllerResolver->getControllerFromDefinition('ClassWithoutMethod');
  }

  /** * Tests getController(). * * @dataProvider providerTestGetController */
  public function testGetController($attributes$class$output = NULL) {
    $request = new Request([][]$attributes);
    $result = $this->controllerResolver->getController($request);
    if ($class) {
      $this->assertCallableController($result$class$output);
    }
    else {
      $this->assertFalse($result);
    }
  }

  /** * Provides test data for testGetController(). */
  
public function __construct(ControllerResolverInterface $resolver, Stopwatch $stopwatch)
    {
        $this->resolver = $resolver;
        $this->stopwatch = $stopwatch;
    }

    public function getController(Request $request): callable|false
    {
        $e = $this->stopwatch->start('controller.get_callable');

        try {
            return $this->resolver->getController($request);
        } finally {
            $e->stop();
        }
    }
}
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();
    }

    /** * @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);
    }

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