/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $request = $event->getRequest(); $response = $event->getResponse(); if ($response instanceof BinaryFileResponse) { return; } if ($request->isXmlHttpRequest()) { //handle with ajax $debugBar = $this->container['debug_bar.debug_bar']; $response->headers->add($debugBar->getDataAsHeaders()); } else { //replace http response if ($response->headers->get('content-type') && $response->headers->get('content-type') != 'text/html') { return; } $debugBar = $this->container['debug_bar.debug_bar']; $renderer = $debugBar->getJavascriptRenderer(); /* @var $renderer \DebugBar\JavascriptRenderer */ $renderer->setBaseUrl('/public/debugbar'); $body = $event->getResponse()->getContent(); $body = str_ireplace(array('</head>', '</body>'), array($renderer->renderHead() . PHP_EOL . '</head>', $renderer->render() . PHP_EOL . '</body>'), $body); $response->setContent($body); } }
/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $exception = $event->getData(); if ($exception instanceof CmsException) { $response = new SupraJsonResponse(null); $response->setStatus(0); $response->setErrorMessage($exception->getMessageKey() ? '{#' . $exception->getMessageKey() . '#}' : $exception->getMessage()); $event->setResponse($response); } }
public function response(RequestResponseEvent $event) { $request = $event->getResponse(); if ($request instanceof Request) { $this->getTimeCollector()->stopMeasure('request_' . spl_object_hash($request)); } }
/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $request = $event->getRequest(); $cmsPrefix = $this->container->getParameter('cms.prefix'); if (strpos($request->getPathInfo(), $cmsPrefix) === 0) { //in any way we should try to extract data from session $session = $this->container->getSession(); $tokenParameter = $this->container->getParameter('cms_authentication.session.storage_key'); $securityContext = $this->container->getSecurityContext(); if ($session->has($tokenParameter)) { $securityContext->setToken($session->get($tokenParameter)); $this->container->getEventDispatcher()->dispatch(AuthController::TOKEN_CHANGE_EVENT, new DataAgnosticEvent()); } //non-authorized users that are not on anonymous paths are getting redirected to login if ((!$securityContext->getToken() || !$securityContext->getToken()->getUser()) && !in_array($request->getPathInfo(), $this->container->getParameter('cms_authentication.paths.anonymous'))) { if ($request->isXmlHttpRequest()) { $event->setResponse(new Response(AuthController::EMPTY_BODY, AuthController::FAILURE_STATUS)); } else { $event->setResponse(new RedirectResponse($this->container->getRouter()->generate('cms_authentication_login'))); } $event->stopPropagation(); } //authorized users on login path are redirected to dashboard if ($securityContext->getToken() && $securityContext->getToken()->getUser() && strpos($request->getPathInfo(), $this->container->getParameter('cms_authentication.paths.login')) === 0) { $event->setResponse(new RedirectResponse($cmsPrefix)); $event->stopPropagation(); } } }
/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { if ($event->hasResponse()) { // do nothing if response exists already return; } $request = $event->getRequest(); $pageController = $this->container['cms.pages.controller']; /* @var $pageController \Supra\Package\Cms\Controller\PageController */ $request->attributes->set('path', '404'); $pageRequest = new PageRequestView($request); $pageRequest->setContainer($this->container); try { $event->setResponse($pageController->execute($pageRequest)); } catch (ResourceNotFoundException $e) { } // ignore silently }
/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $path = $event->getRequest()->getPathInfo(); $parts = pathinfo($path); $parts = array_merge(array('dirname' => '', 'basename' => '', 'extension' => '', 'filename'), $parts); switch (strtolower($parts['extension'])) { case 'css': //possible it's not yet compiled less file? $lessFile = $this->container->getApplication()->getWebRoot() . $path . '.less'; if (is_file($lessFile)) { $asset = new FileAsset($lessFile); $asset->ensureFilter(new LessphpFilter()); $content = $this->container->getCache()->fetch('assets_404', $path, function () use($asset) { return $asset->dump(); }, $asset->getLastModified(), 0, true); $event->setResponse(new Response($content, 200, array('Content-Type' => 'text/css'))); $event->stopPropagation(); return; } break; } }
public function handle(Request $request) { try { $requestEvent = new RequestResponseEvent(); $requestEvent->setRequest($request); $this->container->getEventDispatcher()->dispatch(KernelEvent::REQUEST, $requestEvent); //here event can be overridden by any listener, so check if we have event if ($requestEvent->hasResponse()) { return $requestEvent->getResponse(); } if ($request->attributes->has('_controller') && $request->attributes->has('_action')) { $controllerName = $request->attributes->get('_controller'); $action = $request->attributes->get('_action'); $controllerObject = new $controllerName(); $controllerObject->setContainer($this->container); $response = $controllerObject->{$action}($request); } else { $router = $this->container->getRouter(); $configuration = $router->match($request); //@todo: recall correctly how symfony deals with that $request->attributes = new ParameterBag($configuration); //@todo: do not execute controller that ugly $controllerDefinition = $this->container->getApplication()->parseControllerName($configuration['controller']); //probably there should be a better implementation of a package setting $controllerObject = new $controllerDefinition['controller'](); $controllerObject->setContainer($this->container); $action = $controllerDefinition['action']; $controllerEvent = new ControllerEvent(); $controllerEvent->setController($controllerObject); $controllerEvent->setAction($action); $this->container->getEventDispatcher()->dispatch(KernelEvent::CONTROLLER_START, $controllerEvent); $response = $controllerObject->{$action}($request); $controllerEvent->setResponse($response); $this->container->getEventDispatcher()->dispatch(KernelEvent::CONTROLLER_END, $controllerEvent); $response = $controllerEvent->getResponse(); } $responseEvent = new RequestResponseEvent(); $responseEvent->setRequest($request); $responseEvent->setResponse($response); $this->container->getEventDispatcher()->dispatch(KernelEvent::RESPONSE, $responseEvent); if (!$response instanceof Response) { throw new \Exception('Response returned by your controller is not an instance of HttpFoundation\\Response'); } return $response; } catch (\Exception $e) { //generic exception handler $exceptionEvent = new RequestResponseEvent(); $exceptionEvent->setRequest($request); $exceptionEvent->setData($e); $this->container->getEventDispatcher()->dispatch(KernelEvent::EXCEPTION, $exceptionEvent); if ($exceptionEvent->hasResponse()) { $this->container->getEventDispatcher()->dispatch(KernelEvent::RESPONSE, $exceptionEvent); return $exceptionEvent->getResponse(); } //process 404 exceptions if ($e instanceof ResourceNotFoundException) { $notFoundEvent = new RequestResponseEvent(); $notFoundEvent->setRequest($request); $this->container->getEventDispatcher()->dispatch(KernelEvent::ERROR404, $notFoundEvent); if ($notFoundEvent->hasResponse()) { $this->container->getEventDispatcher()->dispatch(KernelEvent::RESPONSE, $notFoundEvent); return $notFoundEvent->getResponse(); } if ($this->container->getParameter('debug')) { //in debug env 404 errors are just thrown throw $e; } else { return $this->container['exception.controller']->exception404Action($e); } } //process all other exceptions if ($this->container->getParameter('debug')) { throw $e; } else { return $this->container['exception.controller']->exception500Action($e); } } }