1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeVersion; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
10
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
11
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
12
|
|
|
use Symfony\Component\Templating\EngineInterface; |
13
|
|
|
|
14
|
|
|
class RenderContextListener |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EngineInterface |
18
|
|
|
*/ |
19
|
|
|
protected $templating; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityManagerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $em; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param EngineInterface $templating |
|
|
|
|
28
|
|
|
* @param EntityManagerInterface $em |
29
|
|
|
*/ |
30
|
|
|
public function __construct(/* EngineInterface|EntityManagerInterface */ $em, EntityManagerInterface $emOld = null) |
31
|
|
|
{ |
32
|
|
|
if ($em instanceof EngineInterface) { |
33
|
|
|
// NEXT_MAJOR Also remove the symfony/templating dependency as it is unused after the removal of the templating parameter. |
34
|
|
|
@trigger_error(sprintf('Passing a templating engine as the first argument of "%s" is deprecated since KunstmaanNodeBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0. Remove the template engine service argument.', __METHOD__), E_USER_DEPRECATED); |
35
|
|
|
|
36
|
|
|
$this->templating = $em; |
37
|
|
|
$this->em = $emOld; |
38
|
|
|
|
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->em = $em; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param GetResponseForControllerResultEvent|ViewEvent $event |
47
|
|
|
*/ |
48
|
|
|
public function onKernelView($event) |
49
|
|
|
{ |
50
|
|
View Code Duplication |
if (!$event instanceof GetResponseForControllerResultEvent && !$event instanceof ViewEvent) { |
51
|
|
|
throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(RequestEvent::class) ? ViewEvent::class : GetResponseForControllerResultEvent::class, \is_object($event) ? \get_class($event) : \gettype($event))); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$response = $event->getControllerResult(); |
55
|
|
|
if ($response instanceof Response) { |
56
|
|
|
// If it's a response, just continue |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$request = $event->getRequest(); |
61
|
|
|
if ($request->attributes->has('_template')) { //template is already set |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$nodeTranslation = $request->attributes->get('_nodeTranslation'); |
66
|
|
|
if ($nodeTranslation) { |
67
|
|
|
$entity = $request->attributes->get('_entity'); |
68
|
|
|
$url = $request->attributes->get('url'); |
69
|
|
|
$nodeMenu = $request->attributes->get('_nodeMenu'); |
70
|
|
|
$parameters = $request->attributes->get('_renderContext'); |
71
|
|
|
|
72
|
|
|
if ($request->get('preview') === true) { |
73
|
|
|
$version = $request->get('version'); |
74
|
|
|
if (!empty($version) && is_numeric($version)) { |
75
|
|
|
$nodeVersion = $this->em->getRepository(NodeVersion::class)->find($version); |
76
|
|
|
if (!\is_null($nodeVersion)) { |
77
|
|
|
$entity = $nodeVersion->getRef($this->em); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$renderContext = array( |
83
|
|
|
'nodetranslation' => $nodeTranslation, |
84
|
|
|
'slug' => $url, |
85
|
|
|
'page' => $entity, |
86
|
|
|
'resource' => $entity, |
87
|
|
|
'nodemenu' => $nodeMenu, |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
if (\is_array($parameters) || $parameters instanceof \ArrayObject) { |
91
|
|
|
$parameters = array_merge($renderContext, (array) $parameters); |
92
|
|
|
} else { |
93
|
|
|
$parameters = $renderContext; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (\is_array($response)) { |
97
|
|
|
// If the response is an array, merge with rendercontext |
98
|
|
|
$parameters = array_merge($parameters, $response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
//set the rendercontext with all params as response, plus the template in the request attribs |
102
|
|
|
//the SensioFrameworkExtraBundle kernel.view will handle everything else |
103
|
|
|
$event->setControllerResult((array) $parameters); |
104
|
|
|
|
105
|
|
|
$template = new Template(array()); |
106
|
|
|
$template->setTemplate($entity->getDefaultView()); |
107
|
|
|
|
108
|
|
|
$controllerInfo = $this->getControllerInfo($request->attributes->get('_controller')); |
109
|
|
|
$template->setOwner($controllerInfo); |
110
|
|
|
|
111
|
|
|
$request->attributes->set('_template', $template); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* BC check to return correct controller/action information. |
117
|
|
|
* |
118
|
|
|
* @param string $controllerString |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
private function getControllerInfo($controllerString) |
123
|
|
|
{ |
124
|
|
|
if (strpos($controllerString, '::') !== false) { |
125
|
|
|
$controllerBits = explode('::', $controllerString); |
126
|
|
|
$action = array_pop($controllerBits); |
127
|
|
|
|
128
|
|
|
return [$controllerBits, $action]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// NEXT_MAJOR: Remove BC check when we drop sf 3.4 support |
132
|
|
|
$controllerBits = explode(':', $controllerString); |
133
|
|
|
$action = array_pop($controllerBits); |
134
|
|
|
|
135
|
|
|
return [implode(':', $controllerBits), $action]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.