Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

UrlWildcardRouter.php
Go to the documentation of this file.
1 <?php
2
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
27 private $wildcardService;
28
30 private $generator;
31
33 private $requestContext;
34
36 private $logger;
37
43 public function __construct(
44 URLWildcardService $wildcardService,
45 UrlAliasGenerator $generator,
46 RequestContext $requestContext
47 ) {
48 $this->wildcardService = $wildcardService;
49 $this->generator = $generator;
50 $this->requestContext = $requestContext;
51 }
52
56 public function setLogger($logger)
57 {
58 $this->logger = $logger;
59 }
60
68 public function matchRequest(Request $request): array
69 {
70 $requestedPath = $request->attributes->get('semanticPathinfo', $request->getPathInfo());
71
72 try {
73 $urlWildcardTranslationResult = $this->wildcardService->translate($requestedPath);
74 } catch (NotFoundException $e) {
75 throw new ResourceNotFoundException($e->getMessage(), $e->getCode(), $e);
76 }
77
78 if ($this->logger !== null) {
79 $this->logger->info("UrlWildcard matched. Destination URL: {$urlWildcardTranslationResult->uri}");
80 }
81
82 // set translated path for the next router
83 $request->attributes->set('semanticPathinfo', $urlWildcardTranslationResult->uri);
84 $request->attributes->set('needsRedirect', (bool) $urlWildcardTranslationResult->forward);
85
86 // and throw Exception to pass processing to the next router
87 throw new ResourceNotFoundException();
88 }
89
93 public function getRouteCollection(): RouteCollection
94 {
95 return new RouteCollection();
96 }
97
105 public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
106 {
107 throw new RouteNotFoundException('Could not match route');
108 }
109
113 public function setContext(RequestContext $context): void
114 {
115 $this->requestContext = $context;
116 $this->generator->setRequestContext($context);
117 }
118
122 public function getContext(): RequestContext
123 {
124 return $this->requestContext;
125 }
126
136 public function match($pathinfo): array
137 {
138 throw new \RuntimeException("The UrlWildcardRouter doesn't support the match() method. Please use matchRequest() instead.");
139 }
140
152 public function supports($name): bool
153 {
154 return $name === self::URL_ALIAS_ROUTE_NAME;
155 }
156
160 public function getRouteDebugMessage($name, array $parameters = []): string
161 {
162 if ($name instanceof RouteObjectInterface) {
163 return 'Route with key ' . $name->getRouteKey();
164 }
165
166 if ($name instanceof SymfonyRoute) {
167 return 'Route with pattern ' . $name->getPath();
168 }
169
170 return $name;
171 }
172 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator
URL generator for UrlAlias based links.
Definition: UrlAliasGenerator.php:21
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter
Definition: UrlWildcardRouter.php:23
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\$logger
$logger
Definition: UrlWildcardRouter.php:36
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\__construct
__construct(URLWildcardService $wildcardService, UrlAliasGenerator $generator, RequestContext $requestContext)
Definition: UrlWildcardRouter.php:43
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\$generator
$generator
Definition: UrlWildcardRouter.php:30
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\setContext
setContext(RequestContext $context)
Definition: UrlWildcardRouter.php:113
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\$wildcardService
$wildcardService
Definition: UrlWildcardRouter.php:27
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\matchRequest
matchRequest(Request $request)
Definition: UrlWildcardRouter.php:68
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\$requestContext
$requestContext
Definition: UrlWildcardRouter.php:33
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\match
match($pathinfo)
Not supported.
Definition: UrlWildcardRouter.php:136
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\getRouteDebugMessage
getRouteDebugMessage($name, array $parameters=[])
Definition: UrlWildcardRouter.php:160
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\setLogger
setLogger($logger)
Definition: UrlWildcardRouter.php:56
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\getContext
getContext()
Definition: UrlWildcardRouter.php:122
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\supports
supports($name)
Whether the router supports the thing in $name to generate a route.
Definition: UrlWildcardRouter.php:152
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\getRouteCollection
getRouteCollection()
Definition: UrlWildcardRouter.php:93
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\generate
generate($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UrlWildcardRouter.php:105
eZ\Publish\Core\MVC\Symfony\Routing\UrlWildcardRouter\URL_ALIAS_ROUTE_NAME
const URL_ALIAS_ROUTE_NAME
Definition: UrlWildcardRouter.php:24
eZ\Publish\API\Repository\URLWildcardService
Definition: API/Repository/URLWildcardService.php:17
eZ\Publish\Core\MVC\Symfony\Routing
Definition: ChainRouter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.