vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/LegacyTemplateListener.php line 83

Open in your IDE?
  1. <?php
  2. namespace Pimcore\Bundle\CoreBundle\EventListener;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  4. use Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\StreamedResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  8. use Symfony\Component\HttpKernel\Event\KernelEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Templating\EngineInterface;
  11. /**
  12.  * @deprecated
  13.  * Provides backward compatibility for PHP templates
  14.  */
  15. class LegacyTemplateListener extends TemplateListener
  16. {
  17.     /**
  18.      * @var EngineInterface
  19.      */
  20.     private $templateEngine;
  21.     /**
  22.      * @return EngineInterface
  23.      */
  24.     public function getTemplateEngine(): EngineInterface
  25.     {
  26.         return $this->templateEngine;
  27.     }
  28.     /**
  29.      * @param EngineInterface $templateEngine
  30.      */
  31.     public function setTemplateEngine(EngineInterface $templateEngine): void
  32.     {
  33.         $this->templateEngine $templateEngine;
  34.     }
  35.     /**
  36.      * @inheritdoc
  37.      */
  38.     public function onKernelView(KernelEvent $event)
  39.     {
  40.         /* @var Template $template */
  41.         $request $event->getRequest();
  42.         $template $request->attributes->get('_template');
  43.         if (!$template instanceof Template) {
  44.             return;
  45.         }
  46.         if (!$event instanceof GetResponseForControllerResultEvent) {
  47.             return;
  48.         }
  49.         $parameters $event->getControllerResult();
  50.         $owner $template->getOwner();
  51.         list($controller$action) = $owner;
  52.         // when the annotation declares no default vars and the action returns
  53.         // null, all action method arguments are used as default vars
  54.         if (null === $parameters) {
  55.             $parameters $this->resolveDefaultParameters($request$template$controller$action);
  56.         }
  57.         // attempt to render the actual response
  58.         $templating $this->getTemplateEngine();
  59.         if ($template->isStreamable()) {
  60.             $callback = function () use ($templating$template$parameters) {
  61.                 return $templating->stream($template->getTemplate(), $parameters);
  62.             };
  63.             $event->setResponse(new StreamedResponse($callback));
  64.         }
  65.         // make sure the owner (controller+dependencies) is not cached or stored elsewhere
  66.         $template->setOwner([]);
  67.         $event->setResponse($templating->renderResponse($template->getTemplate(), $parameters));
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             KernelEvents::CONTROLLER => ['onKernelController', -128],
  76.             KernelEvents::VIEW => 'onKernelView',
  77.         ];
  78.     }
  79.     private function resolveDefaultParameters(Request $requestTemplate $template$controller$action)
  80.     {
  81.         $parameters = [];
  82.         $arguments $template->getVars();
  83.         if (=== \count($arguments)) {
  84.             $r = new \ReflectionObject($controller);
  85.             $arguments = [];
  86.             foreach ($r->getMethod($action)->getParameters() as $param) {
  87.                 $arguments[] = $param;
  88.             }
  89.         }
  90.         // fetch the arguments of @Template.vars or everything if desired
  91.         // and assign them to the designated template
  92.         foreach ($arguments as $argument) {
  93.             if ($argument instanceof \ReflectionParameter) {
  94.                 $parameters[$name $argument->getName()] = !$request->attributes->has($name) && $argument->isDefaultValueAvailable() ? $argument->getDefaultValue() : $request->attributes->get($name);
  95.             } else {
  96.                 $parameters[$argument] = $request->attributes->get($argument);
  97.             }
  98.         }
  99.         return $parameters;
  100.     }
  101. }