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

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