/** * 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; }
/** * {@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); }
public function getArguments(Request $request, $controller) { return $this->resolver->getArguments($request, $controller); }
/** * @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); }
/** * @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); }
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); }
public function getArguments(Request $request, $controller) { return $this->delegate->getArguments($request, $controller); }