1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use eZ\Bundle\EzPublishCoreBundle\EventListener\ViewControllerListener; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
15
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
16
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\View; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
21
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
23
|
|
|
use Twig_Environment; |
24
|
|
|
use Twig_Extension; |
25
|
|
|
use Twig_SimpleFunction; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Twig extension for content fields/fieldDefinitions rendering (view and edit). |
29
|
|
|
*/ |
30
|
|
|
class ContentRenderingExtension extends Twig_Extension |
31
|
|
|
{ |
32
|
|
|
/** @var \eZ\Bundle\EzPublishCoreBundle\EventListener\ViewControllerListener */ |
33
|
|
|
private $controllerListener; |
34
|
|
|
|
35
|
|
|
/** @var \Symfony\Component\HttpKernel\Kernel */ |
36
|
|
|
private $kernel; |
37
|
|
|
|
38
|
|
|
/** @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface */ |
39
|
|
|
private $controllerResolver; |
40
|
|
|
|
41
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\View\Renderer */ |
42
|
|
|
private $viewRenderer; |
43
|
|
|
|
44
|
|
|
/** @var \Twig_Environment */ |
45
|
|
|
private $twig; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ContentRenderingExtension constructor. |
49
|
|
|
* |
50
|
|
|
* @param \eZ\Bundle\EzPublishCoreBundle\EventListener\ViewControllerListener $controllerListener |
51
|
|
|
* @param \Symfony\Component\HttpKernel\Kernel $kernel |
52
|
|
|
* @param \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface $controllerResolver |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
ViewControllerListener $controllerListener, |
56
|
|
|
Kernel $kernel, |
57
|
|
|
ControllerResolverInterface $controllerResolver |
58
|
|
|
) { |
59
|
|
|
$this->controllerListener = $controllerListener; |
60
|
|
|
$this->kernel = $kernel; |
61
|
|
|
$this->controllerResolver = $controllerResolver; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getName() |
65
|
|
|
{ |
66
|
|
|
return 'ezpublish.content_rendering'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function initRuntime(Twig_Environment $environment) |
70
|
|
|
{ |
71
|
|
|
$this->twig = $environment; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getFunctions() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
new Twig_SimpleFunction( |
78
|
|
|
'ez_render_content', |
79
|
|
|
[$this, 'renderContent'], |
80
|
|
|
['is_safe' => ['html']] |
81
|
|
|
), |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Renders the HTML for a given content. |
87
|
|
|
* |
88
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Content $content |
89
|
|
|
* @param array $params An array of parameters to pass to the field view |
90
|
|
|
* |
91
|
|
|
* @return string The HTML markup |
92
|
|
|
* |
93
|
|
|
* @throws InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
public function renderContent(Content $content, array $params = []) |
96
|
|
|
{ |
97
|
|
|
$request = new Request(); |
98
|
|
|
$request->attributes->add([ |
99
|
|
|
'_controller' => 'ez_content:viewAction', |
100
|
|
|
'contentId' => $content->id, |
101
|
|
|
'content' => $content, |
102
|
|
|
'viewType' => 'embed', |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$controller = $this->controllerResolver->getController($request); |
106
|
|
|
|
107
|
|
|
if (isset($params['inline']) && $params['inline'] === true) { |
108
|
|
|
$this->controllerListener->getController( |
109
|
|
|
$event = new FilterControllerEvent( |
110
|
|
|
$this->kernel, |
111
|
|
|
$controller, |
112
|
|
|
$request, |
113
|
|
|
HttpKernelInterface::SUB_REQUEST |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$arguments = $this->controllerResolver->getArguments($request, $event->getController()); |
118
|
|
|
$response = call_user_func_array($event->getController(), $arguments); |
119
|
|
|
if ($response instanceof View) { |
120
|
|
|
return $this->twig->render($response->getTemplateIdentifier(), $response->getParameters()); |
|
|
|
|
121
|
|
|
} elseif ($response instanceof Response) { |
122
|
|
|
return $response->getContent(); |
123
|
|
|
} |
124
|
|
|
} else { |
125
|
|
|
$response = $this->kernel->handle($request); |
126
|
|
|
|
127
|
|
|
return $response->getContent(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.