‪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\UriInterface;
19 use Symfony\Component\Routing\Generator\UrlGenerator;
20 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21 use Symfony\Component\Routing\RequestContext;
32 
42 {
46  public const ‪ABSOLUTE_URL = 'url';
47 
51  public const ‪ABSOLUTE_PATH = 'absolute';
52 
56  public const ‪SHAREABLE_URL = 'share';
57 
61  protected array ‪$generated = [];
62 
63  protected RequestContext|null ‪$requestContext = null;
67  public function ‪__construct(
68  protected readonly ‪Router $router,
69  protected readonly ‪FormProtectionFactory $formProtectionFactory,
70  protected readonly ‪RequestContextFactory $requestContextFactory
71  ) {
72  }
73 
77  public function ‪setRequestContext(RequestContext ‪$requestContext): void
78  {
79  $this->requestContext = ‪$requestContext;
80  }
81 
94  public function ‪buildUriFromRoutePath($pathInfo, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
95  {
96  $route = $this->router->match($pathInfo);
97  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
98  }
99 
108  public function ‪buildUriWithRedirect(string $name, array $parameters = [], ‪RouteRedirect $redirect = null, string $referenceType = self::ABSOLUTE_PATH): UriInterface
109  {
110  if ($redirect === null) {
111  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
112  }
113 
114  try {
115  $redirect->resolve($this->router);
117  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
118  }
119  $parameters['redirect'] = $redirect->getName();
120  if ($redirect->hasParameters()) {
121  $parameters['redirectParams'] = $redirect->getFormattedParameters();
122  }
123  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
124  }
125 
138  public function ‪buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
139  {
140  $cacheIdentifier = 'route' . $name . serialize($parameters) . $referenceType;
141  if (isset($this->generated[$cacheIdentifier])) {
142  return $this->generated[$cacheIdentifier];
143  }
144 
145  $route = $this->router->getRoute((string)$name);
146  if ($route === null) {
147  throw new ‪RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
148  }
149 
150  $parameters = array_merge(
151  $route->getOption('parameters') ?? [],
152  $parameters
153  );
154 
155  // If the route is not shareable and doesn't have the "public" option set, a token must be generated.
156  if ($referenceType !== self::SHAREABLE_URL && (!$route->hasOption('access') || $route->getOption('access') !== 'public')) {
157  $parameters = [
158  'token' => $this->formProtectionFactory->createForType('backend')->generateToken('route', $name),
159  ] + $parameters;
160  }
161 
162  $this->generated[$cacheIdentifier] = $this->‪buildUri($name, $parameters, (string)$referenceType);
163  return $this->generated[$cacheIdentifier];
164  }
165 
173  protected function ‪buildUri(string $routeName, array $parameters, string $referenceType): UriInterface
174  {
175  if (isset($this->requestContext)) {
177  } elseif (isset(‪$GLOBALS['TYPO3_REQUEST'])) {
178  ‪$requestContext = $this->requestContextFactory->fromBackendRequest(‪$GLOBALS['TYPO3_REQUEST']);
179  } elseif (!‪Environment::isCli()) {
180  ‪$requestContext = $this->requestContextFactory->fromBackendRequest(‪ServerRequestFactory::fromGlobals()->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE));
181  } else {
182  ‪$requestContext = new RequestContext('/typo3/');
183  }
184  $urlGenerator = new UrlGenerator($this->router->getRouteCollection(), ‪$requestContext);
185  ‪$url = $urlGenerator->generate($routeName, $parameters, $referenceType === self::ABSOLUTE_PATH ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
186  return new ‪Uri(‪$url);
187  }
188 }
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪UriInterface buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:138
‪TYPO3\CMS\Backend\Routing\UriBuilder\__construct
‪__construct(protected readonly Router $router, protected readonly FormProtectionFactory $formProtectionFactory, protected readonly RequestContextFactory $requestContextFactory)
Definition: UriBuilder.php:67
‪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:77
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUri
‪buildUri(string $routeName, array $parameters, string $referenceType)
Definition: UriBuilder.php:173
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Backend\Routing\UriBuilder\SHAREABLE_URL
‪const SHAREABLE_URL
Definition: UriBuilder.php:56
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:22
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_PATH
‪const ABSOLUTE_PATH
Definition: UriBuilder.php:51
‪TYPO3\CMS\Backend\Routing\UriBuilder\$generated
‪array $generated
Definition: UriBuilder.php:61
‪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:94
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:42
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriWithRedirect
‪buildUriWithRedirect(string $name, array $parameters=[], RouteRedirect $redirect=null, string $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:108
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:34
‪TYPO3\CMS\Backend\Routing\UriBuilder\$requestContext
‪RequestContext null $requestContext
Definition: UriBuilder.php:63
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪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:46
‪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