public function getController(Request $request) { $this->request = $request; try { $controller = $this->resolver->getController($request); } catch (\Exception $ex) { $controller = parent::getController($request); } return $controller; }
public function getController(Request $request) { $controller = $this->delegate->getController($request); if (is_array($controller) && is_object($controller[0])) { if ($this->kernel->isClassInModule(get_class($controller[0])) && !$this->kernel->isClassInActiveModule(get_class($controller[0]))) { $controller = false; } } return $controller; }
public function getController(Request $request) { $controller = $this->resolver->getController($request); if (!$controller instanceof \Closure) { $instance = is_array($controller) ? reset($controller) : $controller; if (is_object($instance)) { $this->container->injectOn($instance); } } return $controller; }
/** * Detects if there is a custom controller to use to render a Block. * * @param FilterControllerEvent $event * * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException */ public function getController(FilterControllerEvent $event) { $request = $event->getRequest(); // Only taking page related controller (i.e. ez_page:viewBlock or ez_page:viewBlockById) if (strpos($request->attributes->get('_controller'), 'ez_page:') === false) { return; } try { if ($request->attributes->has('id')) { $valueObject = $this->pageService->loadBlock($request->attributes->get('id')); $request->attributes->set('block', $valueObject); } elseif ($request->attributes->get('block') instanceof Block) { $valueObject = $request->attributes->get('block'); $request->attributes->set('id', $valueObject->id); } } catch (UnauthorizedException $e) { throw new AccessDeniedException(); } if (!isset($valueObject)) { $this->logger->error('Could not resolve a page controller, invalid value object to match.'); return; } $controllerReference = $this->controllerManager->getControllerReference($valueObject, 'block'); if (!$controllerReference instanceof ControllerReference) { return; } $request->attributes->set('_controller', $controllerReference->controller); $event->setController($this->controllerResolver->getController($request)); }
/** * Detects if there is a custom controller to use to render a Location/Content. * * @param FilterControllerEvent $event * * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException */ public function getController(FilterControllerEvent $event) { $request = $event->getRequest(); // Only taking content related controller (i.e. ez_content:viewLocation or ez_content:viewContent) if (strpos($request->attributes->get('_controller'), 'ez_content:') === false) { return; } try { if ($request->attributes->has('locationId')) { $valueObject = $this->repository->getLocationService()->loadLocation($request->attributes->get('locationId')); } elseif ($request->attributes->get('location') instanceof Location) { $valueObject = $request->attributes->get('location'); $request->attributes->set('locationId', $valueObject->id); } elseif ($request->attributes->has('contentId')) { $valueObject = $this->repository->sudo(function (Repository $repository) use($request) { return $repository->getContentService()->loadContentInfo($request->attributes->get('contentId')); }); } elseif ($request->attributes->get('contentInfo') instanceof ContentInfo) { $valueObject = $request->attributes->get('contentInfo'); $request->attributes->set('contentId', $valueObject->id); } } catch (UnauthorizedException $e) { throw new AccessDeniedException(); } if (!isset($valueObject)) { $this->logger->error('Could not resolver a view controller, invalid value object to match.'); return; } $controllerReference = $this->controllerManager->getControllerReference($valueObject, $request->attributes->get('viewType')); if (!$controllerReference instanceof ControllerReference) { return; } $request->attributes->set('_controller', $controllerReference->controller); $event->setController($this->controllerResolver->getController($request)); }
public function onKernelController(FilterControllerEvent $event) { $route = $event->getRequest()->attributes->get('_route'); if (!$event->isMasterRequest() || in_array($route, ['comunidad_select', 'comunidad_new'])) { return; } $user = $this->token ? $this->token->getUser() : null; if (!$user instanceof Usuario) { return; } $comunidad = $this->comunidadProvider->get(); if (!$comunidad instanceof Comunidad) { $fakeRequest = $event->getRequest()->duplicate(null, null, array('_controller' => 'AppBundle:Comunidad:index')); $controller = $this->resolver->getController($fakeRequest); $event->setController($controller); } }
/** * Configures the View for eZ View controllers. * * @param FilterControllerEvent $event * * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException */ public function getController(FilterControllerEvent $event) { $request = $event->getRequest(); if (($viewBuilder = $this->viewBuilderRegistry->getFromRegistry($request->attributes->get('_controller'))) === null) { return; } $parameterEvent = new FilterViewBuilderParametersEvent(clone $request); $this->eventDispatcher->dispatch(ViewEvents::FILTER_BUILDER_PARAMETERS, $parameterEvent); $view = $viewBuilder->buildView($parameterEvent->getParameters()->all()); $request->attributes->set('view', $view); // View parameters are added as request attributes so that they are available as controller action parameters $request->attributes->add($view->getParameters()); if (($controllerReference = $view->getControllerReference()) instanceof ControllerReference) { $request->attributes->set('_controller', $controllerReference->controller); $event->setController($this->controllerResolver->getController($request)); } }
/** * **Return the Controller instance associated with a Request.** * * As several resolvers can exist for a single application, a resolver must * return false when it is not able to determine the controller. * * The resolver must only throw an exception when it should be able to load * controller but cannot because of some errors made by the developer. * * @param Request $request A Request instance * * @return callable|false A PHP callable representing the Controller, * or false if this resolver is not able to determine the controller. */ public function getController(Request $request) : callable { $controller = $request->attributes->get('_controller', NULL); if (!$this->callbackResolver->isValid($controller)) { return $this->controllerResolver->getController($request); } return $this->callbackResolver->convertCallback($controller); }
public function testApplicationAwareShouldSetApp() { $this->app['some_service'] = Support\ApplicationAwareController::__CLASS; $req = Request::create('/'); $req->attributes->set('_controller', 'some_service'); $req->attributes->set('action', 'example'); $resolved = $this->resolver->getController($req); $controller = reset($resolved); $controller instanceof Support\ApplicationAwareController; $this->assertSame($this->app, $controller->app); }
protected function validateDefaultsController(ErrorElement $errorElement, $object) { $defaults = $object->getDefaults(); $controller = $defaults['_controller']; $request = new Request(array(), array(), array('_controller' => $controller)); try { $this->controllerResolver->getController($request); } catch (\LogicException $e) { $errorElement->with('defaults')->addViolation($e->getMessage())->end(); } }
/** * renders content with the real website controller. * * @param PageBridge $content * @param bool $partial * * @return string */ public function render(PageBridge $content, $partial = false) { // set active theme $webspace = $this->webspaceManager->findWebspaceByKey($content->getWebspaceKey()); $this->activeTheme->setName($webspace->getTheme()->getKey()); // get controller and invoke action $request = new Request(); $request->attributes->set('_controller', $content->getController()); $controller = $this->controllerResolver->getController($request); // prepare locale for translator and request $request->setLocale($content->getLanguageCode()); $localeBefore = $this->translator->getLocale(); $this->translator->setLocale($content->getLanguageCode()); $this->requestStack->push($request); /** @var Response $response */ $response = $controller[0]->{$controller[1]}($content, true, $partial); // roll back $this->requestStack->pop(); $this->translator->setLocale($localeBefore); return $response->getContent(); }
/** * Exchange default admin controller by custom entity admin controller. * * @param FilterControllerEvent $event */ public function onKernelController(FilterControllerEvent $event) { $request = $event->getRequest(); if ('easyadmin' !== $request->attributes->get('_route')) { return; } $currentController = $event->getController(); // if the controller is defined in a class, $currentController is an array // otherwise do nothing because it's a Closure (rare but possible in Symfony) if (!is_array($currentController)) { return; } // this condition happens when accessing the backend homepage, which // then redirects to the 'list' action of the first configured entity. if (null === ($entityName = $request->query->get('entity'))) { return; } $entity = $this->configManager->getEntityConfig($entityName); // if the entity doesn't define a custom controller, do nothing if (!isset($entity['controller'])) { return; } $customController = $entity['controller']; $controllerMethod = $currentController[1]; // build the full controller name depending on its type if (class_exists($customController)) { // 'class::method' syntax for normal controllers $customController .= '::' . $controllerMethod; } else { // 'service:method' syntax for controllers as services $customController .= ':' . $controllerMethod; } $request->attributes->set('_controller', $customController); $newController = $this->resolver->getController($request); if (false === $newController) { throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". Check the "controller" configuration of the "%s" entity in your EasyAdmin backend.', $request->getPathInfo(), $entityName)); } $event->setController($newController); }
/** * Cache action. * * @param Request $request * * @return mixed */ public function cacheAction(Request $request) { $parameters = $request->get('parameters', array()); if ($request->get('token') != $this->computeHash($parameters)) { throw new AccessDeniedHttpException('Invalid token'); } $subRequest = Request::create('', 'get', $parameters, $request->cookies->all(), array(), $request->server->all()); $controller = $this->resolver->getController($subRequest); $subRequest->attributes->add(array('_controller' => $parameters['controller'])); $subRequest->attributes->add($parameters['parameters']); $arguments = $this->resolver->getArguments($subRequest, $controller); // call controller return call_user_func_array($controller, $arguments); }
/** * * * @param $href - The resource link * @return ResourceInterface */ public function resolveLink($href) { $stubRequest = Request::create($href); // External url if ($this->urlMatcher->getContext()->getHost() !== $stubRequest->getHost() || ($stubRequest->isSecure() ? $this->urlMatcher->getContext()->getHttpsPort() !== $stubRequest->getPort() : $this->urlMatcher->getContext()->getHttpPort() !== $stubRequest->getPort()) || $this->urlMatcher->getContext()->getBaseUrl() !== $stubRequest->getBaseUrl()) { throw new InvalidLinkException($href); } $path = $stubRequest->getPathInfo(); $requestMethod = $this->urlMatcher->getContext()->getMethod(); // Force the GET method to avoid the use of the // previous method (LINK/UNLINK) $this->urlMatcher->getContext()->setMethod('GET'); try { $route = $this->urlMatcher->match($path); } catch (ResourceNotFoundException $e) { throw new InvalidLinkException($href); } // Set back to original method $this->urlMatcher->getContext()->setMethod($requestMethod); foreach ($route as $key => $attr) { $stubRequest->attributes->set($key, $attr); } if (false === ($controller = $this->resolver->getController($stubRequest))) { throw new InvalidLinkException($href); } // Make sure @ParamConverter and friends are handled $subEvent = new FilterControllerEvent($this->kernel, $controller, $stubRequest, HttpKernelInterface::MASTER_REQUEST); $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $subEvent); $controller = $subEvent->getController(); $arguments = $this->resolver->getArguments($stubRequest, $controller); $result = call_user_func_array($controller, $arguments); if ($result instanceof View) { $result = $result->getData(); } return $result; }
/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if (!$event->getRequest()->headers->has('link')) { return; } $links = array(); $header = $event->getRequest()->headers->get('link'); /* * Due to limitations, multiple same-name headers are sent as comma * separated values. * * This breaks those headers into Link headers following the format * http://tools.ietf.org/html/rfc2068#section-19.6.2.4 */ while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) { $header = trim(substr($header, strlen($matches[0]))); $links[] = $matches[1]; } if ($header) { $links[] = $header; } $requestMethod = $this->urlMatcher->getContext()->getMethod(); // The controller resolver needs a request to resolve the controller. $stubRequest = new Request(); foreach ($links as $idx => $link) { // Force the GET method to avoid the use of the // previous method (LINK/UNLINK) $this->urlMatcher->getContext()->setMethod('GET'); $linkParams = explode(';', trim($link)); $resourceType = null; if (count($linkParams) > 1) { $resourceType = trim(preg_replace('/<|>/', '', $linkParams[1])); $resourceType = str_replace("\"", "", str_replace("rel=", "", $resourceType)); } $resource = array_shift($linkParams); $resource = preg_replace('/<|>/', '', $resource); $tempRequest = Request::create($resource); try { $route = $this->urlMatcher->match($tempRequest->getRequestUri()); } catch (\Exception $e) { // If we don't have a matching route we return the original Link header continue; } if (strpos($route['_route'], 'newscoop_gimme_') === false) { return; } $stubRequest->attributes->replace($route); $stubRequest->server = $event->getRequest()->server; if (false === ($controller = $this->resolver->getController($stubRequest))) { continue; } $subEvent = new FilterControllerEvent($event->getKernel(), $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST); $kernelSubEvent = new GetResponseEvent($event->getKernel(), $stubRequest, HttpKernelInterface::SUB_REQUEST); $event->getDispatcher()->dispatch(KernelEvents::REQUEST, $kernelSubEvent); $event->getDispatcher()->dispatch(KernelEvents::CONTROLLER, $subEvent); $controller = $subEvent->getController(); $arguments = $this->resolver->getArguments($stubRequest, $controller); try { $result = call_user_func_array($controller, $arguments); // Our api returns objects for single resources if (!is_object($result)) { continue; } $links[$idx] = array('object' => $result, 'resourceType' => $resourceType); } catch (\Exception $e) { $links[$idx] = array('object' => $e, 'resourceType' => 'exception'); continue; } } $event->getRequest()->attributes->set('links', $links); $this->urlMatcher->getContext()->setMethod($requestMethod); }
/** * {@inheritdoc} */ public function getTitle(LocalActionInterface $local_action) { $controller = array($local_action, 'getTitle'); $arguments = $this->controllerResolver->getArguments($this->requestStack->getCurrentRequest(), $controller); return call_user_func_array($controller, $arguments); }
/** * @param FilterControllerEvent $event A FilterControllerEvent instance */ public function onKernelController(FilterControllerEvent $event) { if (!$event->getRequest()->headers->has('link')) { return; } $links = array(); $header = $event->getRequest()->headers->get('link'); /* * Due to limitations, multiple same-name headers are sent as comma * separated values. * * This breaks those headers into Link headers following the format * http://tools.ietf.org/html/rfc2068#section-19.6.2.4 */ while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) { $header = trim(substr($header, strlen($matches[0]))); $links[] = $matches[1]; } if ($header) { $links[] = $header; } $requestMethod = $this->urlMatcher->getContext()->getMethod(); // Force the GET method to avoid the use of the // previous method (LINK/UNLINK) $this->urlMatcher->getContext()->setMethod('GET'); // The controller resolver needs a request to resolve the controller. $stubRequest = new Request(); foreach ($links as $idx => $link) { $linkParams = explode(';', trim($link)); $resource = array_shift($linkParams); $resource = preg_replace('/<|>/', '', $resource); if (preg_match('#^/|https?://#', $resource) === 0) { $resource = '/' . $resource; } try { $route = $this->urlMatcher->match($resource); } catch (\Exception $e) { // If we don't have a matching route we return // the original Link header continue; } $stubRequest->attributes->replace($route); if (false === ($controller = $this->resolver->getController($stubRequest))) { continue; } try { $stubEvent = new FilterControllerEvent($this->httpKernel, $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST); $this->eventDispatcher->dispatch(KernelEvents::CONTROLLER, $stubEvent); $arguments = $this->resolver->getArguments($stubRequest, $controller); $result = call_user_func_array($controller, $arguments); // By convention the controller action must return an array if (!is_array($result)) { continue; } // The key of first item is discarded $links[$idx] = current($result); } catch (\Exception $e) { continue; } } $event->getRequest()->attributes->set('link', $links); $this->urlMatcher->getContext()->setMethod($requestMethod); }
public function onKernelRequest(GetResponseEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } if (!$event->getRequest()->headers->has('link')) { return; } $links = array(); $header = $event->getRequest()->headers->get('link'); /* * Due to limitations, multiple same-name headers are sent as comma * separated values. * * This breaks those headers into Link headers following the format * http://tools.ietf.org/html/rfc2068#section-19.6.2.4 */ while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) { $header = trim(substr($header, strlen($matches[0]))); $links[] = $matches[1]; } if ($header) { $links[] = $header; } $requestMethod = $this->urlMatcher->getContext()->getMethod(); // Force the GET method to avoid the use of the // previous method (LINK/UNLINK) $this->urlMatcher->getContext()->setMethod('GET'); // The controller resolver needs a request to resolve the controller. $stubRequest = new Request(); foreach ($links as $idx => $link) { $linkHeader = $this->parseLinkHeader($link); $resource = $this->parseResource($linkHeader, $event->getRequest()); try { $route = $this->urlMatcher->match($resource); } catch (\Exception $e) { // If we don't have a matching route we return // the original Link header continue; } $stubRequest->attributes->replace($route); if (false === ($controller = $this->resolver->getController($stubRequest))) { continue; } // Make sure @ParamConverter and some other annotations are called $subEvent = new FilterControllerEvent($event->getKernel(), $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST); $event->getDispatcher()->dispatch(KernelEvents::CONTROLLER, $subEvent); $controller = $subEvent->getController(); $arguments = $this->resolver->getArguments($stubRequest, $controller); try { $result = call_user_func_array($controller, $arguments); $value = is_array($result) ? current($result) : $result; if ($linkHeader->hasRel()) { unset($links[$idx]); $links[$linkHeader->getRel()][] = $value; } else { $links[$idx] = $value; } } catch (\Exception $e) { continue; } } $event->getRequest()->attributes->set('links', $links); $this->urlMatcher->getContext()->setMethod($requestMethod); }
/** * {@inheritdoc} */ public function getArguments(Request $request, $controller) { return $this->resolver->getArguments($request, $controller); }