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