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

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