vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/PimcoreContextListener.php line 66

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\Request;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. class PimcoreContextListener implements EventSubscriberInterfaceLoggerAwareInterface
  26. {
  27.     use LoggerAwareTrait;
  28.     /**
  29.      * @var PimcoreContextResolver
  30.      */
  31.     protected $resolver;
  32.     /**
  33.      * @var RequestStack
  34.      */
  35.     protected $requestStack;
  36.     /**
  37.      * @param PimcoreContextResolver $resolver
  38.      * @param RequestStack $requestStack
  39.      */
  40.     public function __construct(
  41.         PimcoreContextResolver $resolver,
  42.         RequestStack $requestStack
  43.     ) {
  44.         $this->resolver $resolver;
  45.         $this->requestStack $requestStack;
  46.     }
  47.     /**
  48.      * @inheritDoc
  49.      */
  50.     public static function getSubscribedEvents()
  51.     {
  52.         return [
  53.             // run after router to be able to match the _route attribute
  54.             // TODO check if this is early enough
  55.             KernelEvents::REQUEST => ['onKernelRequest'24]
  56.         ];
  57.     }
  58.     public function onKernelRequest(GetResponseEvent $event)
  59.     {
  60.         $request $event->getRequest();
  61.         if ($event->isMasterRequest()) {
  62.             $context $this->resolver->getPimcoreContext($request);
  63.             if ($context) {
  64.                 $this->logger->debug('Resolved pimcore context for path {path} to {context}', [
  65.                     'path' => $request->getPathInfo(),
  66.                     'context' => $context
  67.                 ]);
  68.             } else {
  69.                 $this->logger->debug('Could not resolve a pimcore context for path {path}', [
  70.                     'path' => $request->getPathInfo()
  71.                 ]);
  72.             }
  73.             $this->initializeContext($context$request);
  74.         }
  75.     }
  76.     /**
  77.      * Do context specific initialization
  78.      *
  79.      * @param string $context
  80.      * @param Request $request
  81.      */
  82.     protected function initializeContext($context$request)
  83.     {
  84.         if ($context == PimcoreContextResolver::CONTEXT_ADMIN || $context == PimcoreContextResolver::CONTEXT_WEBSERVICE) {
  85.             \Pimcore::setAdminMode();
  86.             Document::setHideUnpublished(false);
  87.             DataObject\AbstractObject::setHideUnpublished(false);
  88.             if ($context == PimcoreContextResolver::CONTEXT_WEBSERVICE) {
  89.                 DataObject\AbstractObject::setGetInheritedValues(filter_var($request->get('inheritance'), FILTER_VALIDATE_BOOLEAN));
  90.             }
  91.             DataObject\Localizedfield::setGetFallbackValues(false);
  92.         } else {
  93.             \Pimcore::unsetAdminMode();
  94.             Document::setHideUnpublished(true);
  95.             DataObject\AbstractObject::setHideUnpublished(true);
  96.             DataObject\AbstractObject::setGetInheritedValues(true);
  97.             DataObject\Localizedfield::setGetFallbackValues(true);
  98.         }
  99.     }
  100. }