‪TYPO3CMS  10.4
MiddlewareStackResolver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use ArrayObject;
21 use Psr\Container\ContainerInterface;
22 use ‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend as PhpFrontendCache;
26 
33 {
37  protected ‪$container;
38 
43 
47  protected ‪$cache;
48 
49  public function ‪__construct(
50  ContainerInterface ‪$container,
52  PhpFrontendCache ‪$cache
53  ) {
54  $this->container = ‪$container;
55  $this->dependencyOrderingService = ‪$dependencyOrderingService;
56  $this->cache = ‪$cache;
57  }
58 
68  public function ‪resolve(string $stackName): array
69  {
70  // Check if the registered middlewares from all active packages have already been cached
71  $cacheIdentifier = $this->‪getCacheIdentifier($stackName);
72  if ($this->cache->has($cacheIdentifier)) {
73  return $this->cache->require($cacheIdentifier);
74  }
75 
76  $allMiddlewares = $this->‪loadConfiguration();
77  $middlewares = $this->‪sanitizeMiddlewares($allMiddlewares);
78 
79  // Ensure that we create a cache for $stackName, even if the stack is empty
80  if (!isset($middlewares[$stackName])) {
81  $middlewares[$stackName] = [];
82  }
83 
84  foreach ($middlewares as $stack => $middlewaresOfStack) {
85  $this->cache->set($this->‪getCacheIdentifier($stack), 'return ' . var_export($middlewaresOfStack, true) . ';');
86  }
87 
88  return $middlewares[$stackName];
89  }
90 
96  protected function ‪loadConfiguration(): ArrayObject
97  {
98  return $this->container->get('middlewares');
99  }
100 
107  protected function ‪sanitizeMiddlewares(ArrayObject $allMiddlewares): array
108  {
109  $middlewares = [];
110 
111  foreach ($allMiddlewares as $stack => $middlewaresOfStack) {
112  $middlewaresOfStack = $this->dependencyOrderingService->orderByDependencies($middlewaresOfStack);
113 
114  $sanitizedMiddlewares = [];
115  foreach ($middlewaresOfStack as $name => $middleware) {
116  if (isset($middleware['disabled']) && $middleware['disabled'] === true) {
117  // Skip this middleware if disabled by configuration
118  continue;
119  }
120  $sanitizedMiddlewares[$name] = $middleware['target'];
121  }
122 
123  // Order reverse, MiddlewareDispatcher executes the last middleware in the array first (last in, first out).
124  $middlewares[$stack] = array_reverse($sanitizedMiddlewares);
125  }
126 
127  return $middlewares;
128  }
129 
134  protected function ‪getCacheIdentifier(string $stackName): string
135  {
136  return 'middlewares_' . $stackName . '_' . sha1((string)(new ‪Typo3Version()) . ‪Environment::getProjectPath());
137  }
138 }
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\$container
‪ContainerInterface $container
Definition: MiddlewareStackResolver.php:36
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\resolve
‪array resolve(string $stackName)
Definition: MiddlewareStackResolver.php:65
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver
Definition: MiddlewareStackResolver.php:33
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\sanitizeMiddlewares
‪array sanitizeMiddlewares(ArrayObject $allMiddlewares)
Definition: MiddlewareStackResolver.php:104
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\$cache
‪PhpFrontendCache $cache
Definition: MiddlewareStackResolver.php:44
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\$dependencyOrderingService
‪DependencyOrderingService $dependencyOrderingService
Definition: MiddlewareStackResolver.php:40
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\getCacheIdentifier
‪string getCacheIdentifier(string $stackName)
Definition: MiddlewareStackResolver.php:131
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\__construct
‪__construct(ContainerInterface $container, DependencyOrderingService $dependencyOrderingService, PhpFrontendCache $cache)
Definition: MiddlewareStackResolver.php:46
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\loadConfiguration
‪ArrayObject loadConfiguration()
Definition: MiddlewareStackResolver.php:93
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18