‪TYPO3CMS  ‪main
UriBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ServerRequestInterface;
19 use Psr\Http\Message\UriInterface;
20 use Symfony\Component\Routing\Generator\UrlGenerator;
21 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22 use Symfony\Component\Routing\RequestContext;
23 use Symfony\Component\Routing\Route as SymfonyRoute;
34 
44 {
48  public const ‪ABSOLUTE_URL = 'url';
49 
53  public const ‪ABSOLUTE_PATH = 'absolute';
54 
58  public const ‪SHAREABLE_URL = 'share';
59 
63  protected array ‪$generated = [];
64 
65  protected RequestContext|null ‪$requestContext = null;
69  public function ‪__construct(
70  protected readonly ‪Router $router,
71  protected readonly ‪FormProtectionFactory $formProtectionFactory,
72  protected readonly ‪RequestContextFactory $requestContextFactory
73  ) {
74  }
75 
79  public function ‪setRequestContext(RequestContext ‪$requestContext): void
80  {
81  $this->requestContext = ‪$requestContext;
82  }
83 
96  public function ‪buildUriFromRoutePath($pathInfo, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
97  {
98  $route = $this->router->match($pathInfo);
99  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
100  }
101 
110  public function ‪buildUriWithRedirect(string $name, array $parameters = [], ‪RouteRedirect $redirect = null, string $referenceType = self::ABSOLUTE_PATH): UriInterface
111  {
112  if ($redirect === null) {
113  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
114  }
115 
116  try {
117  $redirect->resolve($this->router);
119  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
120  }
121  $parameters['redirect'] = $redirect->getName();
122  if ($redirect->hasParameters()) {
123  $parameters['redirectParams'] = $redirect->getFormattedParameters();
124  }
125  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
126  }
127 
132  public function ‪buildUriFromRequest(ServerRequestInterface $request, array $parameters = [], string $referenceType = self::ABSOLUTE_PATH): UriInterface
133  {
134  $route = $request->getAttribute('route');
135  if (!$route instanceof ‪Route && !$route instanceof SymfonyRoute) {
136  throw new ‪RouteNotFoundException('No route object was given inside the request object', 1691423325);
137  }
138  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
139  }
140 
153  public function ‪buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
154  {
155  $cacheIdentifier = 'route' . $name . serialize($parameters) . $referenceType;
156  if (isset($this->generated[$cacheIdentifier])) {
157  return $this->generated[$cacheIdentifier];
158  }
159 
160  $route = $this->router->getRoute((string)$name);
161  if ($route === null) {
162  throw new ‪RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
163  }
164 
165  $parameters = array_merge(
166  $route->getOption('parameters') ?? [],
167  $parameters
168  );
169 
170  // If the route is not shareable and doesn't have the "public" option set, a token must be generated.
171  if ($referenceType !== self::SHAREABLE_URL && (!$route->hasOption('access') || $route->getOption('access') !== 'public')) {
172  $parameters = [
173  'token' => $this->formProtectionFactory->createForType('backend')->generateToken('route', $name),
174  ] + $parameters;
175  }
176 
177  $this->generated[$cacheIdentifier] = $this->‪buildUri($name, $parameters, (string)$referenceType);
178  return $this->generated[$cacheIdentifier];
179  }
180 
188  protected function ‪buildUri(string $routeName, array $parameters, string $referenceType): UriInterface
189  {
190  if (isset($this->requestContext)) {
192  } elseif (isset(‪$GLOBALS['TYPO3_REQUEST'])) {
193  ‪$requestContext = $this->requestContextFactory->fromBackendRequest(‪$GLOBALS['TYPO3_REQUEST']);
194  } elseif (!‪Environment::isCli()) {
195  ‪$requestContext = $this->requestContextFactory->fromBackendRequest(‪ServerRequestFactory::fromGlobals()->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE));
196  } else {
197  ‪$requestContext = new RequestContext('/typo3/');
198  }
199  $urlGenerator = new UrlGenerator($this->router->getRouteCollection(), ‪$requestContext);
200  ‪$url = $urlGenerator->generate($routeName, $parameters, $referenceType === self::ABSOLUTE_PATH ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
201  return new ‪Uri(‪$url);
202  }
203 }
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪UriInterface buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:153
‪TYPO3\CMS\Backend\Routing\UriBuilder\__construct
‪__construct(protected readonly Router $router, protected readonly FormProtectionFactory $formProtectionFactory, protected readonly RequestContextFactory $requestContextFactory)
Definition: UriBuilder.php:69
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:24
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Routing\UriBuilder\setRequestContext
‪setRequestContext(RequestContext $requestContext)
Definition: UriBuilder.php:79
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUri
‪buildUri(string $routeName, array $parameters, string $referenceType)
Definition: UriBuilder.php:188
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Backend\Routing\UriBuilder\SHAREABLE_URL
‪const SHAREABLE_URL
Definition: UriBuilder.php:58
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:22
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_PATH
‪const ABSOLUTE_PATH
Definition: UriBuilder.php:53
‪TYPO3\CMS\Backend\Routing\UriBuilder\$generated
‪array $generated
Definition: UriBuilder.php:63
‪TYPO3\CMS\Backend\Routing\RouteRedirect
Definition: RouteRedirect.php:30
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoutePath
‪UriInterface buildUriFromRoutePath($pathInfo, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:96
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriWithRedirect
‪buildUriWithRedirect(string $name, array $parameters=[], RouteRedirect $redirect=null, string $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:110
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:34
‪TYPO3\CMS\Backend\Routing\UriBuilder\$requestContext
‪RequestContext null $requestContext
Definition: UriBuilder.php:65
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRequest
‪buildUriFromRequest(ServerRequestInterface $request, array $parameters=[], string $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:132
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:44
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Backend\Routing\Exception\RouteTypeNotAllowedException
Definition: RouteTypeNotAllowedException.php:26
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:48
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:58
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30