|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the RoutingExtension class. |
|
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
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
|
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
|
13
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
|
14
|
|
|
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface; |
|
15
|
|
|
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference; |
|
16
|
|
|
use eZ\Publish\SPI\Persistence\Content\ContentInfo; |
|
17
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
18
|
|
|
use Twig\Extension\AbstractExtension; |
|
19
|
|
|
use Twig\Node\Expression\ArrayExpression; |
|
20
|
|
|
use Twig\Node\Expression\ConstantExpression; |
|
21
|
|
|
use Twig\Node\Node; |
|
22
|
|
|
use Twig\TwigFunction; |
|
23
|
|
|
|
|
24
|
|
|
class RoutingExtension extends AbstractExtension |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface */ |
|
27
|
|
|
private $routeReferenceGenerator; |
|
28
|
|
|
|
|
29
|
|
|
/** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */ |
|
30
|
|
|
private $urlGenerator; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
RouteReferenceGeneratorInterface $routeReferenceGenerator, |
|
34
|
|
|
UrlGeneratorInterface $urlGenerator |
|
35
|
|
|
) { |
|
36
|
|
|
$this->routeReferenceGenerator = $routeReferenceGenerator; |
|
37
|
|
|
$this->urlGenerator = $urlGenerator; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getFunctions() |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
new TwigFunction( |
|
44
|
|
|
'ez_route', |
|
45
|
|
|
[$this, 'getRouteReference'] |
|
46
|
|
|
), |
|
47
|
|
|
new TwigFunction( |
|
48
|
|
|
'ez_path', |
|
49
|
|
|
[$this, 'getPath'], |
|
50
|
|
|
['is_safe_callback' => [$this, 'isUrlGenerationSafe']] |
|
51
|
|
|
), |
|
52
|
|
|
new TwigFunction( |
|
53
|
|
|
'ez_url', |
|
54
|
|
|
[$this, 'getUrl'], |
|
55
|
|
|
['is_safe_callback' => [$this, 'isUrlGenerationSafe']] |
|
56
|
|
|
), |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getName() |
|
61
|
|
|
{ |
|
62
|
|
|
return 'ezpublish.routing'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $resource |
|
67
|
|
|
* @param array $params |
|
68
|
|
|
* |
|
69
|
|
|
* @return \eZ\Publish\Core\MVC\Symfony\Routing\RouteReference |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getRouteReference($resource = null, $params = []) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->routeReferenceGenerator->generate($resource, $params); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
View Code Duplication |
public function getPath($name, array $parameters = [], bool $relative = false): string |
|
77
|
|
|
{ |
|
78
|
|
|
$referenceType = $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH; |
|
79
|
|
|
if (!is_string($name)) { |
|
80
|
|
|
return $this->generateUrlForObject($name, $parameters, $referenceType); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->urlGenerator->generate($name, $parameters, $referenceType); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
View Code Duplication |
public function getUrl($name, array $parameters = [], bool $schemeRelative = false): string |
|
87
|
|
|
{ |
|
88
|
|
|
$referenceType = $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL; |
|
89
|
|
|
if (!is_string($name)) { |
|
90
|
|
|
return $this->generateUrlForObject($name, $parameters, $referenceType); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->urlGenerator->generate($name, $parameters, $referenceType); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
98
|
|
|
*/ |
|
99
|
|
|
private function generateUrlForObject(object $object, array $parameters, int $referenceType): string |
|
100
|
|
|
{ |
|
101
|
|
|
if ($object instanceof Location) { |
|
102
|
|
|
return $this->urlGenerator->generate('ez_urlalias', $parameters + [ |
|
103
|
|
|
'locationId' => $object->id, |
|
104
|
|
|
], $referenceType); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($object instanceof Content || $object instanceof ContentInfo) { |
|
108
|
|
|
return $this->urlGenerator->generate('ez_urlalias', $parameters + [ |
|
109
|
|
|
'contentId' => $object->id, |
|
110
|
|
|
], $referenceType); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($object instanceof RouteReference) { |
|
114
|
|
|
return $this->urlGenerator->generate($object->getRoute(), $parameters + $object->getParams(), $referenceType); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
throw new InvalidArgumentException('$object', 'Unsupported class: ' . get_class($object)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* FIXME: copy of \Symfony\Bridge\Twig\Extension\RoutingExtension::isUrlGenerationSafe. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function isUrlGenerationSafe(Node $argsNode): array |
|
124
|
|
|
{ |
|
125
|
|
|
// support named arguments |
|
126
|
|
|
$paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : ( |
|
127
|
|
|
$argsNode->hasNode(1) ? $argsNode->getNode(1) : null |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
if (null === $paramsNode || $paramsNode instanceof ArrayExpression && \count($paramsNode) <= 2 && |
|
131
|
|
|
(!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof ConstantExpression) |
|
132
|
|
|
) { |
|
133
|
|
|
return ['html']; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return []; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|