pimcore/lib/Pimcore/Bundle/CoreBundle/EventListener/PimcoreContextListener.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener;
  15. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Model\Document;
  18. use Psr\Log\LoggerAwareInterface;
  19. use Psr\Log\LoggerAwareTrait;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. class PimcoreContextListener implements EventSubscriberInterfaceLoggerAwareInterface
  25. {
  26.     use LoggerAwareTrait;
  27.     /**
  28.      * @var PimcoreContextResolver
  29.      */
  30.     protected $resolver;
  31.     /**
  32.      * @var RequestStack
  33.      */
  34.     protected $requestStack;
  35.     /**
  36.      * @param PimcoreContextResolver $resolver
  37.      * @param RequestStack $requestStack
  38.      */
  39.     public function __construct(
  40.         PimcoreContextResolver $resolver,
  41.         RequestStack $requestStack
  42.     ) {
  43.         $this->resolver     $resolver;
  44.         $this->requestStack $requestStack;
  45.     }
  46.     /**
  47.      * @inheritDoc
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             // run after router to be able to match the _route attribute
  53.             // TODO check if this is early enough
  54.             KernelEvents::REQUEST => ['onKernelRequest'24]
  55.         ];
  56.     }
  57.     public function onKernelRequest(GetResponseEvent $event)
  58.     {
  59.         $request $event->getRequest();
  60.         if ($event->isMasterRequest()) {
  61.             $context $this->resolver->getPimcoreContext($request);
  62.             if ($context) {
  63.                 $this->logger->debug('Resolved pimcore context for path {path} to {context}', [
  64.                     'path'    => $request->getPathInfo(),
  65.                     'context' => $context
  66.                 ]);
  67.             } else {
  68.                 $this->logger->debug('Could not resolve a pimcore context for path {path}', [
  69.                     'path' => $request->getPathInfo()
  70.                 ]);
  71.             }
  72.             $this->initializeContext($context);
  73.         }
  74.     }
  75.     /**
  76.      * Do context specific initialization
  77.      *
  78.      * @param $context
  79.      */
  80.     protected function initializeContext($context)
  81.     {
  82.         if ($context == PimcoreContextResolver::CONTEXT_ADMIN) {
  83.             \Pimcore::setAdminMode();
  84.             Document::setHideUnpublished(false);
  85.             DataObject\AbstractObject::setHideUnpublished(false);
  86.             DataObject\AbstractObject::setGetInheritedValues(false);
  87.             DataObject\Localizedfield::setGetFallbackValues(false);
  88.         } else {
  89.             \Pimcore::unsetAdminMode();
  90.             Document::setHideUnpublished(true);
  91.             DataObject\AbstractObject::setHideUnpublished(true);
  92.             DataObject\AbstractObject::setGetInheritedValues(true);
  93.             DataObject\Localizedfield::setGetFallbackValues(true);
  94.         }
  95.     }
  96. }