-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
TemplateControllerListener.php
114 lines (97 loc) · 4.13 KB
/
TemplateControllerListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace Pimcore\Bundle\CoreBundle\EventListener;
use Pimcore\Controller\TemplateControllerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Handles the attributes set by TemplateControllerInterface and injects them into the Template annotation which is
* then processed by SensioFrameworkExtraBundle. This allows us to add view auto-rendering without depending on annotations.
*/
class TemplateControllerListener implements EventSubscriberInterface
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var string
*/
protected $defaultEngine;
/**
* @param ContainerInterface $container
* @param string $defaultEngine
*/
public function __construct(ContainerInterface $container, $defaultEngine = 'twig')
{
$this->container = $container;
$this->defaultEngine = $defaultEngine;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
// the view event needs to run before the SensioFrameworkExtraBundle TemplateListener
// handles the Template annotation
return [
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::VIEW => ['onKernelView', 32]
];
}
public function onKernelController(FilterControllerEvent $event)
{
$request = $event->getRequest();
$callable = $event->getController();
// if controller implements TemplateControllerInterface, register it as attribute as we need it later in onKernelView
$templateController = false;
if (is_object($callable) && $callable instanceof TemplateControllerInterface) {
$templateController = true;
} elseif (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof TemplateControllerInterface) {
$templateController = true;
}
if ($templateController) {
$request->attributes->set(TemplateControllerInterface::ATTRIBUTE_TEMPLATE_CONTROLLER, $callable);
}
}
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
// don't do anything if there's already a Template annotation in place
if ($request->attributes->has('_template')) {
return;
}
if (!$request->attributes->has(TemplateControllerInterface::ATTRIBUTE_TEMPLATE_CONTROLLER)) {
return;
}
if ($request->attributes->has(TemplateControllerInterface::ATTRIBUTE_AUTO_RENDER)) {
$controller = $request->attributes->get(TemplateControllerInterface::ATTRIBUTE_TEMPLATE_CONTROLLER);
$engine = $this->defaultEngine;
if ($request->attributes->has(TemplateControllerInterface::ATTRIBUTE_AUTO_RENDER_ENGINE)) {
$engine = $request->attributes->get(TemplateControllerInterface::ATTRIBUTE_AUTO_RENDER_ENGINE);
}
$guesser = $this->container->get('sensio_framework_extra.view.guesser');
$template = new Template([]);
$template->setOwner($controller);
$templateReference = $guesser->guessTemplateName($controller, $request, $engine);
$template->setTemplate($templateReference);
// inject Template annotation into the request - will be used by SensioFrameworkExtraBundle
$request->attributes->set('_template', $template);
}
}
}