Symfony\Component\Routing\Router::setContext PHP Method

setContext() public method

Sets the request context.
public setContext ( Symfony\Component\Routing\RequestContext $context )
$context Symfony\Component\Routing\RequestContext The context
    public function setContext(RequestContext $context)
    {
        $this->context = $context;

        $this->getMatcher()->setContext($context);
        $this->getGenerator()->setContext($context);
    }

Usage Example

 /**
  * @param ApiDoc $annotation
  * @param array $annotations
  * @param Route $route
  * @param \ReflectionMethod $method
  */
 public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
 {
     if (!($resource = $this->getResource($route))) {
         return;
     }
     $context = new RequestContext('', $resource->getActions()->getActionForRoute($route)->getMethods()[0]);
     $this->router->setContext($context);
     $routeName = $this->router->match($route->getPath())['_route'];
     $section = $this->getSection($routeName, $resource);
     foreach ($annotations as $annot) {
         if ($annot instanceof GenerateApiDoc) {
             $annotation->setSection($section);
             if ($this->returnsEntity($route)) {
                 $this->setOutput($annotation, $resource);
             }
             if ($this->expectsInput($route)) {
                 if ($resource->getFormTypeClass() == DynamicFormType::class) {
                     $entityClass = $resource->getEntityClass();
                     $handler = new DynamicFormSubscriber($this->em, new $entityClass());
                     foreach ($handler->getFields() as $field) {
                         $annotation->addParameter($field, ['dataType' => 'string', 'required' => false]);
                     }
                 } else {
                     $this->setInput($annotation, $resource);
                 }
             }
             if ($roles = $route->getDefault('_roles')) {
                 $annotation->setAuthentication(true);
                 $annotation->setAuthenticationRoles($roles);
             }
             $annotation->setDescription($this->getDescription($resource, $route));
             $annotation->setDocumentation($this->getDescription($resource, $route));
             if ($resource->getActions()->getActionForRoute($route) instanceof Index) {
                 $this->addFilter($annotation, $resource);
                 $this->addPagination($annotation, $resource);
             }
         }
     }
 }