vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/TemplateControllerListener.php line 64

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\Controller\TemplateControllerInterface;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  20. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * Handles the attributes set by TemplateControllerInterface and injects them into the Template annotation which is
  24.  * then processed by SensioFrameworkExtraBundle. This allows us to add view auto-rendering without depending on annotations.
  25.  */
  26. class TemplateControllerListener implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var ContainerInterface
  30.      */
  31.     protected $container;
  32.     /**
  33.      * @var string
  34.      */
  35.     protected $defaultEngine;
  36.     /**
  37.      * @param ContainerInterface $container
  38.      * @param string $defaultEngine
  39.      */
  40.     public function __construct(ContainerInterface $container$defaultEngine 'twig')
  41.     {
  42.         $this->container $container;
  43.         $this->defaultEngine $defaultEngine;
  44.     }
  45.     /**
  46.      * @inheritDoc
  47.      */
  48.     public static function getSubscribedEvents()
  49.     {
  50.         // the view event needs to run before the SensioFrameworkExtraBundle TemplateListener
  51.         // handles the Template annotation
  52.         return [
  53.             KernelEvents::CONTROLLER => 'onKernelController',
  54.             KernelEvents::VIEW => ['onKernelView'32]
  55.         ];
  56.     }
  57.     public function onKernelController(FilterControllerEvent $event)
  58.     {
  59.         $request $event->getRequest();
  60.         $callable $event->getController();
  61.         // if controller implements TemplateControllerInterface, register it as attribute as we need it later in onKernelView
  62.         $templateController false;
  63.         if (is_object($callable) && $callable instanceof TemplateControllerInterface) {
  64.             $templateController true;
  65.         } elseif (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof TemplateControllerInterface) {
  66.             $templateController true;
  67.         }
  68.         if ($templateController) {
  69.             $request->attributes->set(TemplateControllerInterface::ATTRIBUTE_TEMPLATE_CONTROLLER$callable);
  70.         }
  71.     }
  72.     public function onKernelView(GetResponseForControllerResultEvent $event)
  73.     {
  74.         $request $event->getRequest();
  75.         // don't do anything if there's already a Template annotation in place
  76.         if ($request->attributes->has('_template')) {
  77.             return;
  78.         }
  79.         if (!$request->attributes->has(TemplateControllerInterface::ATTRIBUTE_TEMPLATE_CONTROLLER)) {
  80.             return;
  81.         }
  82.         if ($request->attributes->has(TemplateControllerInterface::ATTRIBUTE_AUTO_RENDER)) {
  83.             $controller $request->attributes->get(TemplateControllerInterface::ATTRIBUTE_TEMPLATE_CONTROLLER);
  84.             $engine $this->defaultEngine;
  85.             if ($request->attributes->has(TemplateControllerInterface::ATTRIBUTE_AUTO_RENDER_ENGINE)) {
  86.                 $engine $request->attributes->get(TemplateControllerInterface::ATTRIBUTE_AUTO_RENDER_ENGINE);
  87.             }
  88.             $guesser $this->container->get('sensio_framework_extra.view.guesser');
  89.             $template = new Template([]);
  90.             $template->setOwner($controller);
  91.             $templateReference $guesser->guessTemplateName($controller$request$engine);
  92.             $template->setTemplate($templateReference);
  93.             // inject Template annotation into the request - will be used by SensioFrameworkExtraBundle
  94.             $request->attributes->set('_template'$template);
  95.         }
  96.     }
  97. }