Author: Bernhard Schussek (bernhard.schussek@symfony.com)
Inheritance: extends Symfony\Component\EventDispatcher\Event
 /**
  * Check if the user is logged in when requesting an account route, if not
  * redirect to the homepage.
  *
  * @param  Event  $event
  */
 public function checkLoggedIn(KernelEvent $event)
 {
     $user = $this->get('user.current');
     if ($user instanceof AnonymousUser and is_array($event->getRequest()->get('_route_collections')) and in_array('ms.user.account', $event->getRequest()->get('_route_collections'))) {
         $event->setResponse(new RedirectResponse('/'));
     }
 }
 /**
  * Loads all non-admin routes right before the actual page is rendered.
  *
  * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
  *   The event to process.
  */
 public function onRequest(KernelEvent $event)
 {
     // Just preload on normal HTML pages, as they will display menu links.
     if ($this->negotiation->getContentType($event->getRequest()) == 'html') {
         $this->loadNonAdminRoutes();
     }
 }
Ejemplo n.º 3
0
 /**
  * Loads all non-admin routes right before the actual page is rendered.
  *
  * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
  *   The event to process.
  */
 public function onRequest(KernelEvent $event)
 {
     // Only preload on normal HTML pages, as they will display menu links.
     if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()->getRequestFormat() == 'html') {
         if ($routes = $this->state->get('routing.non_admin_routes', [])) {
             // Preload all the non-admin routes at once.
             $this->routeProvider->preLoadRoutes($routes);
         }
     }
 }
 /**
  * Returns the scope from the event request.
  *
  * @param KernelEvent $event The event object
  *
  * @return string|null The scope name
  */
 private function getScopeFromEvent(KernelEvent $event)
 {
     if (!$event->getRequest()->attributes->has('_scope')) {
         return null;
     }
     $scope = $event->getRequest()->attributes->get('_scope');
     if (!$this->container->hasScope($scope)) {
         return null;
     }
     return $scope;
 }
 /**
  * Replace flash messages with their translation
  *
  * @param KernelEvent $event
  */
 public function translate(KernelEvent $event)
 {
     $bag = $event->getRequest()->getSession()->getFlashBag();
     $messages = [];
     foreach ($bag->all() as $type => $flashes) {
         foreach ($flashes as $flash) {
             if ($flash instanceof Message) {
                 $flash = $this->translator->trans($flash->getTemplate(), $flash->getParameters());
             }
             $messages[$type][] = $flash;
         }
     }
     $bag->setAll($messages);
 }
Ejemplo n.º 6
0
 public function onKernelController(KernelEvent $event)
 {
     $request = $event->getRequest();
     if (!$request->attributes->has('repository')) {
         return;
     }
     $repository = $request->attributes->get('repository');
     if (!isset($this->repositories[$repository])) {
         throw new HttpException(404, sprintf('Repository "%s" does not exist', $repository));
     }
     $this->requestContext->setParameter('repository', $repository);
     $repositoryObject = $this->repositories[$repository];
     $request->attributes->set('repository', $repositoryObject);
     $this->twig->addGlobal('repository', $repositoryObject);
 }
Ejemplo n.º 7
0
 /**
  * Loads all non-admin routes right before the actual page is rendered.
  *
  * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
  *   The event to process.
  */
 public function onRequest(KernelEvent $event)
 {
     // Only preload on normal HTML pages, as they will display menu links.
     if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()->getRequestFormat() == 'html') {
         // Ensure that the state query is cached to skip the database query, if
         // possible.
         $key = 'routing.non_admin_routes';
         if ($cache = $this->cache->get($key)) {
             $routes = $cache->data;
         } else {
             $routes = $this->state->get($key, []);
             $this->cache->set($key, $routes, Cache::PERMANENT, ['routes']);
         }
         if ($routes) {
             // Preload all the non-admin routes at once.
             $this->routeProvider->preLoadRoutes($routes);
         }
     }
 }
 /**
  * Inject custom Asset package to Kernel assets helper
  *
  * @param KernelEvent $event
  */
 public function onKernelRequest(KernelEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     // retrieve needed params
     $kernel = $this->container->get('kernel');
     $environment = $kernel->getEnvironment();
     $nodeEnvironment = $this->container->getParameter('grunt.environment');
     // cachebusting shouldn't be applied in node environment
     // BrowserSync delivers files from app/Resources
     if ($environment != $nodeEnvironment) {
         // defaulrts to web folder
         $rootDir = $this->container->getParameter('grunt.filerev.root_dir');
         $package = new FilerevPackage();
         $package->setRoot($rootDir);
         // catalogue holds rev data from grunt-filerev
         // generated by the command `grunt rev` which is called during the build process
         $this->initializeCacheCatalogue();
         $package->setSummary($this->catalogue);
         $assetsHelper = $this->container->get('templating.helper.assets');
         $assetsHelper->setDefaultPackage($package);
     }
 }
 /**
  * Method called on kernel.request event. Handle only "subrequest"
  * @param KernelEvent $event The received event
  *
  * @return void
  */
 public function onKernelRequest(KernelEvent $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST == $event->getRequestType() || null === $this->cacheService) {
         return;
     }
     $request = $event->getRequest();
     if ($request->attributes->has('server_cache')) {
         $cacheKey = $this->getRequestCacheKey($request);
         $request->attributes->set('cache_key', $cacheKey);
         $controller = $request->attributes->get('controllerName');
         $fromCache = false;
         $responseContent = $this->cacheService->get($cacheKey);
         if ($responseContent || $responseContent === '' && !$request->attributes->get('ignore_errors')) {
             $response = new Response($responseContent);
             $response->headers->set('server_cached', 1);
             $event->setResponse($response);
             if ($this->debug) {
                 $this->decorateResponse($request, $response, $controller, true);
             }
             $fromCache = true;
         }
         $this->cachedBlocks[$controller . ' - key : ' . $cacheKey] = $fromCache;
     }
 }
Ejemplo n.º 10
0
 public function onKernelRequest(KernelEvent $event)
 {
     if (strtoupper($event->getRequest()->getMethod()) !== 'LINK' && strtoupper($event->getRequest()->getMethod()) !== 'UNLINK') {
         return;
     }
     if (!$event->getRequest()->headers->has('link')) {
         throw new BadRequestHttpException('Please specify at least one Link.');
     }
     $requestMethod = $this->urlMatcher->getContext()->getMethod();
     $this->urlMatcher->getContext()->setMethod('GET');
     $links = [];
     /*
      * 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
      */
     foreach (explode(',', $event->getRequest()->headers->get('link')) as $header) {
         $header = trim($header);
         $link = new LinkHeader($header);
         try {
             if ($urlParameters = $this->urlMatcher->match($link->getValue())) {
                 $link->setUrlParameters($urlParameters);
             }
         } catch (ResourceNotFoundException $exception) {
         }
         try {
             $link->setResource($this->resourceTransformer->getResourceProxy($link->getValue()));
         } catch (InvalidArgumentException $e) {
         }
         $links[] = $link;
     }
     $this->urlMatcher->getContext()->setMethod($requestMethod);
     $event->getRequest()->attributes->set('links', $links);
 }
Ejemplo n.º 11
0
 public function __construct(HttpKernelInterface $kernel, Request $request, Response $response)
 {
     parent::__construct($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
     $this->response = $response;
 }
Ejemplo n.º 12
0
 /**
  * Handles onKernelRequest events.
  */
 public function onKernelRequest(KernelEvent $event)
 {
     $this['request'] = $event->getRequest();
     $this['request_context'] = new RequestContext($this['request']->getBaseUrl(), $this['request']->getMethod(), $this['request']->getHost(), $this['request']->getScheme(), !$this['request']->isSecure() ? $this['request']->getPort() : $this['request.http_port'], $this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port']);
     $this['controllers']->flush();
     $matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']);
     try {
         $attributes = $matcher->match($this['request']->getPathInfo());
         $this['request']->attributes->add($attributes);
     } catch (RoutingException $e) {
         // make sure onSilexBefore event is dispatched
         $this['dispatcher']->dispatch(SilexEvents::BEFORE);
         if ($e instanceof ResourceNotFoundException) {
             $message = sprintf('No route found for "%s %s"', $this['request']->getMethod(), $this['request']->getPathInfo());
             throw new NotFoundHttpException($message, $e);
         } else {
             if ($e instanceof MethodNotAllowedException) {
                 $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $this['request']->getMethod(), $this['request']->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods())));
                 throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
             }
         }
         throw $e;
     }
     $this['dispatcher']->dispatch(SilexEvents::BEFORE);
 }
Ejemplo n.º 13
0
 /**
  * Checks whether the request is a Contao front end master request.
  *
  * @param KernelEvent $event
  *
  * @return bool
  */
 protected function isFrontendMasterRequest(KernelEvent $event)
 {
     return $event->isMasterRequest() && $this->isFrontendScope();
 }
 /**
  * @param KernelEvent $event
  */
 public function onRequest(KernelEvent $event)
 {
     if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
         $this->uriProvider->setRequest($event->getRequest());
     }
 }
Ejemplo n.º 15
0
 public function __construct(HttpKernelInterface $kernel, Request $request, $requestType, Page $page)
 {
     parent::__construct($kernel, $request, $requestType);
     $this->page = $page;
 }
 /**
  * @param KernelEvent $event
  * @return bool
  */
 protected function eventRequestMatches(KernelEvent $event)
 {
     return $this->requestMatcher->matches($event->getRequest(), $event->getRequestType());
 }
 public function __construct(HttpKernelInterface $kernel, Request $request, $requestType, Response $response)
 {
     parent::__construct($kernel, $request, $requestType);
     $this->setResponse($response);
 }
 /**
  * @param KernelEvent $event
  * @return bool
  */
 protected function isActive(KernelEvent $event)
 {
     $request = $event->getRequest();
     if ($this->active === true) {
         return true;
     }
     $this->active = false;
     if ($event instanceof FilterControllerEvent) {
         if (!$event->isMasterRequest()) {
             return false;
         }
         if ($event->getController()[0] instanceof RestApiControllerInterface) {
             return $this->active = true;
         }
     }
     foreach ($this->whitelist as $whitelist) {
         if (preg_match($whitelist, $request->getPathInfo())) {
             return $this->active = true;
         }
     }
     foreach ($this->blacklist as $blacklist) {
         if (preg_match($blacklist, $request->getPathInfo())) {
             $this->active = false;
         }
     }
     return $this->active;
 }
Ejemplo n.º 19
0
 /**
  * Returns the scope from the event request.
  *
  * @param KernelEvent $event The event object
  *
  * @return string|null The scope name
  */
 private function getScopeFromEvent(KernelEvent $event)
 {
     return $event->getRequest()->attributes->get('_scope');
 }
Ejemplo n.º 20
0
 /**
  * Handles onKernelRequest events.
  */
 public function onKernelRequest(KernelEvent $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
         $this->beforeDispatched = true;
         $this['dispatcher']->dispatch(SilexEvents::BEFORE, $event);
     }
 }
Ejemplo n.º 21
0
 /**
  * @inheritdoc
  */
 public function onKernelRequest(KernelEvent $event)
 {
     if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
         $this->channel = $this->channelResolver->resolve($event->getRequest()->getHost());
     }
 }
 /**
  * Check event status.
  *
  *   - HttpKernelInterface::MASTER_REQUEST === 1
  *   - HttpKernelInterface::SUB_REQUEST === 2
  *
  * @param KernelEvent $event The KernelEvent
  *
  * @return void
  *
  * @api
  */
 public function onCoreController(KernelEvent $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
         $this->controller = $event->getController();
         if (isset($this->controller[0])) {
             $this->controller = $this->controller[0];
             if (method_exists($this->controller, 'preExecute')) {
                 $this->controller->preExecute();
             }
         }
     }
 }
 /**
  * @param KernelEvent $event
  *
  * @return bool
  */
 protected function isEventEligible(KernelEvent $event)
 {
     return $event->isMasterRequest();
 }
Ejemplo n.º 24
0
 public function __construct(HttpKernelInterface $kernel, $controller, Request $request, $requestType)
 {
     parent::__construct($kernel, $request, $requestType);
     $this->setController($controller);
 }
 /**
  * @param KernelEvent $event
  *
  * @return bool
  */
 private function isMasterRequest(KernelEvent $event)
 {
     return HttpKernelInterface::MASTER_REQUEST === $event->getRequestType();
 }