|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Routing; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
|
10
|
|
|
use eZ\Publish\API\Repository\URLWildcardService; |
|
11
|
|
|
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator; |
|
12
|
|
|
use Symfony\Cmf\Component\Routing\ChainedRouterInterface; |
|
13
|
|
|
use Symfony\Cmf\Component\Routing\RouteObjectInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
16
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
|
17
|
|
|
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
|
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
19
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
20
|
|
|
use Symfony\Component\Routing\Route as SymfonyRoute; |
|
21
|
|
|
|
|
22
|
|
|
class UrlWildcardRouter implements ChainedRouterInterface, RequestMatcherInterface |
|
23
|
|
|
{ |
|
24
|
|
|
public const URL_ALIAS_ROUTE_NAME = 'ez_urlalias'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \eZ\Publish\API\Repository\URLWildcardService |
|
28
|
|
|
*/ |
|
29
|
|
|
private $wildcardService; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator |
|
33
|
|
|
*/ |
|
34
|
|
|
private $generator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Symfony\Component\Routing\RequestContext |
|
38
|
|
|
*/ |
|
39
|
|
|
private $requestContext; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Psr\Log\LoggerInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $logger; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \eZ\Publish\API\Repository\URLWildcardService $wildcardService |
|
48
|
|
|
* @param \eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator $generator |
|
49
|
|
|
* @param \Symfony\Component\Routing\RequestContext $requestContext |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
URLWildcardService $wildcardService, |
|
53
|
|
|
UrlAliasGenerator $generator, |
|
54
|
|
|
RequestContext $requestContext |
|
55
|
|
|
) { |
|
56
|
|
|
$this->wildcardService = $wildcardService; |
|
57
|
|
|
$this->generator = $generator; |
|
58
|
|
|
$this->requestContext = $requestContext; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setLogger($logger) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->logger = $logger; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
71
|
|
|
* |
|
72
|
|
|
* @return array An array of parameters |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function matchRequest(Request $request): array |
|
77
|
|
|
{ |
|
78
|
|
|
$requestedPath = $request->attributes->get('semanticPathinfo', $request->getPathInfo()); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$urlWildcardTranslationResult = $this->wildcardService->translate($requestedPath); |
|
82
|
|
|
} catch (NotFoundException $e) { |
|
83
|
|
|
throw new ResourceNotFoundException($e->getMessage(), $e->getCode(), $e); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($this->logger !== null) { |
|
87
|
|
|
$this->logger->info("UrlWildcard matched. Destination URL: {$urlWildcardTranslationResult->uri}"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// set translated path for the next router |
|
91
|
|
|
$request->attributes->set('semanticPathinfo', $urlWildcardTranslationResult->uri); |
|
92
|
|
|
$request->attributes->set('needsRedirect', (bool) $urlWildcardTranslationResult->forward); |
|
93
|
|
|
|
|
94
|
|
|
// and throw Exception to pass processing to the next router |
|
95
|
|
|
throw new ResourceNotFoundException(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return \Symfony\Component\Routing\RouteCollection |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getRouteCollection(): RouteCollection |
|
102
|
|
|
{ |
|
103
|
|
|
return new RouteCollection(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
* @param array $parameters |
|
109
|
|
|
* @param int $referenceType |
|
110
|
|
|
* |
|
111
|
|
|
* @return string|void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) |
|
114
|
|
|
{ |
|
115
|
|
|
throw new RouteNotFoundException('Could not match route'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param \Symfony\Component\Routing\RequestContext $context |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setContext(RequestContext $context): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->requestContext = $context; |
|
124
|
|
|
$this->generator->setRequestContext($context); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return \Symfony\Component\Routing\RequestContext |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getContext(): RequestContext |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->requestContext; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Not supported. Please use matchRequest() instead. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $pathinfo |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
* |
|
142
|
|
|
* @throws \RuntimeException |
|
143
|
|
|
*/ |
|
144
|
|
|
public function match($pathinfo): array |
|
145
|
|
|
{ |
|
146
|
|
|
throw new \RuntimeException("The UrlWildcardRouter doesn't support the match() method. Please use matchRequest() instead."); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Whether the router supports the thing in $name to generate a route. |
|
151
|
|
|
* |
|
152
|
|
|
* This check does not need to look if the specific instance can be |
|
153
|
|
|
* resolved to a route, only whether the router can generate routes from |
|
154
|
|
|
* objects of this class. |
|
155
|
|
|
* |
|
156
|
|
|
* @param mixed $name The route name or route object |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
|
|
public function supports($name): bool |
|
161
|
|
|
{ |
|
162
|
|
|
return $name === self::URL_ALIAS_ROUTE_NAME; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @see Symfony\Cmf\Component\Routing\VersatileGeneratorInterface::getRouteDebugMessage() |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getRouteDebugMessage($name, array $parameters = array()): string |
|
169
|
|
|
{ |
|
170
|
|
|
if ($name instanceof RouteObjectInterface) { |
|
171
|
|
|
return 'Route with key ' . $name->getRouteKey(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($name instanceof SymfonyRoute) { |
|
175
|
|
|
return 'Route with pattern ' . $name->getPath(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $name; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|