‪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 
78  public function ‪setRequestContext(RequestContext ‪$requestContext): void
79  {
80  $this->requestContext = ‪$requestContext;
81  }
82 
95  public function ‪buildUriFromRoutePath($pathInfo, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
96  {
97  $route = $this->router->match($pathInfo);
98  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
99  }
100 
109  public function ‪buildUriWithRedirect(string $name, array $parameters = [], ‪RouteRedirect $redirect = null, string $referenceType = self::ABSOLUTE_PATH): UriInterface
110  {
111  if ($redirect === null) {
112  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
113  }
114 
115  try {
116  $redirect->resolve($this->router);
118  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
119  }
120  $parameters['redirect'] = $redirect->getName();
121  if ($redirect->hasParameters()) {
122  $parameters['redirectParams'] = $redirect->getFormattedParameters();
123  }
124  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
125  }
126 
131  public function ‪buildUriFromRequest(ServerRequestInterface $request, array $parameters = [], string $referenceType = self::ABSOLUTE_PATH): UriInterface
132  {
133  $route = $request->getAttribute('route');
134  if (!$route instanceof ‪Route && !$route instanceof SymfonyRoute) {
135  throw new ‪RouteNotFoundException('No route object was given inside the request object', 1691423325);
136  }
137  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
138  }
139 
152  public function ‪buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
153  {
154  $cacheIdentifier = 'route' . $name . serialize($parameters) . $referenceType;
155  if (isset($this->generated[$cacheIdentifier])) {
156  return $this->generated[$cacheIdentifier];
157  }
158 
159  $route = $this->router->getRoute((string)$name);
160  if ($route === null) {
161  throw new ‪RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
162  }
163 
164  $parameters = array_merge(
165  $route->getOption('parameters') ?? [],
166  $parameters
167  );
168 
169  // If the route is not shareable and doesn't have the "public" option set, a token must be generated.
170  if ($referenceType !== self::SHAREABLE_URL && (!$route->hasOption('access') || $route->getOption('access') !== 'public')) {
171  $parameters = [
172  'token' => $this->formProtectionFactory->createForType('backend')->generateToken('route', $name),
173  ] + $parameters;
174  }
175 
176  $this->generated[$cacheIdentifier] = $this->‪buildUri($name, $parameters, (string)$referenceType);
177  return $this->generated[$cacheIdentifier];
178  }
179 
187  protected function ‪buildUri(string $routeName, array $parameters, string $referenceType): UriInterface
188  {
189  if (isset($this->requestContext)) {
191  } elseif (isset(‪$GLOBALS['TYPO3_REQUEST'])) {
192  ‪$requestContext = $this->requestContextFactory->fromBackendRequest(‪$GLOBALS['TYPO3_REQUEST']);
193  } elseif (!‪Environment::isCli()) {
194  ‪$requestContext = $this->requestContextFactory->fromBackendRequest(‪ServerRequestFactory::fromGlobals()->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE));
195  } else {
196  ‪$requestContext = new RequestContext('/typo3/');
197  }
198  $urlGenerator = new UrlGenerator($this->router->getRouteCollection(), ‪$requestContext);
199  ‪$url = $urlGenerator->generate($routeName, $parameters, $referenceType === self::ABSOLUTE_PATH ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
200  return new ‪Uri(‪$url);
201  }
202 }
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪UriInterface buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:152
‪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:23
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Routing\UriBuilder\setRequestContext
‪setRequestContext(RequestContext $requestContext)
Definition: UriBuilder.php:78
‪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:187
‪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:21
‪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:95
‪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:109
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:35
‪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:131
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:43
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$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:25
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:48
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:40
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30