‪TYPO3CMS  9.5
MiddlewareStackResolver.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 namespace ‪TYPO3\CMS\Core\Http;
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 use ‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend as PhpFrontendCache;
20 use TYPO3\CMS\Core\Package\PackageManager;
22 
29 {
33  protected ‪$packageManager;
34 
39 
43  protected ‪$cache;
44 
45  public function ‪__construct(
46  PackageManager ‪$packageManager,
48  PhpFrontendCache ‪$cache
49  ) {
50  $this->packageManager = ‪$packageManager;
51  $this->dependencyOrderingService = ‪$dependencyOrderingService;
52  $this->cache = ‪$cache;
53  }
54 
64  public function ‪resolve(string $stackName): array
65  {
66  // Check if the registered middlewares from all active packages have already been cached
67  $cacheIdentifier = $this->‪getCacheIdentifier($stackName);
68  if ($this->cache->has($cacheIdentifier)) {
69  return $this->cache->require($cacheIdentifier);
70  }
71 
72  $allMiddlewares = $this->‪loadConfiguration();
73  $middlewares = $this->‪sanitizeMiddlewares($allMiddlewares);
74 
75  // Ensure that we create a cache for $stackName, even if the stack is empty
76  if (!isset($middlewares[$stackName])) {
77  $middlewares[$stackName] = [];
78  }
79 
80  foreach ($middlewares as $stack => $middlewaresOfStack) {
81  $this->cache->set($this->‪getCacheIdentifier($stack), 'return ' . var_export($middlewaresOfStack, true) . ';');
82  }
83 
84  return $middlewares[$stackName];
85  }
86 
92  protected function ‪loadConfiguration(): array
93  {
94  $packages = $this->packageManager->getActivePackages();
95  $allMiddlewares = [[]];
96  foreach ($packages as $package) {
97  $packageConfiguration = $package->getPackagePath() . 'Configuration/RequestMiddlewares.php';
98  if (file_exists($packageConfiguration)) {
99  $middlewaresInPackage = require $packageConfiguration;
100  if (is_array($middlewaresInPackage)) {
101  $allMiddlewares[] = $middlewaresInPackage;
102  }
103  }
104  }
105  return array_replace_recursive(...$allMiddlewares);
106  }
107 
114  protected function ‪sanitizeMiddlewares(array $allMiddlewares): array
115  {
116  $middlewares = [];
117 
118  foreach ($allMiddlewares as $stack => $middlewaresOfStack) {
119  $middlewaresOfStack = $this->dependencyOrderingService->orderByDependencies($middlewaresOfStack);
120 
121  $sanitizedMiddlewares = [];
122  foreach ($middlewaresOfStack as $name => $middleware) {
123  if (isset($middleware['disabled']) && $middleware['disabled'] === true) {
124  // Skip this middleware if disabled by configuration
125  continue;
126  }
127  $sanitizedMiddlewares[$name] = $middleware['target'];
128  }
129 
130  // Order reverse, MiddlewareDispatcher executes the last middleware in the array first (last in, first out).
131  $middlewares[$stack] = array_reverse($sanitizedMiddlewares);
132  }
133 
134  return $middlewares;
135  }
136 
141  protected function ‪getCacheIdentifier(string $stackName): string
142  {
143  return 'middlewares_' . $stackName . '_' . sha1(TYPO3_version . ‪Environment::getProjectPath());
144  }
145 }
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\loadConfiguration
‪array loadConfiguration()
Definition: MiddlewareStackResolver.php:89
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\__construct
‪__construct(PackageManager $packageManager, DependencyOrderingService $dependencyOrderingService, PhpFrontendCache $cache)
Definition: MiddlewareStackResolver.php:42
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:24
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\resolve
‪array resolve(string $stackName)
Definition: MiddlewareStackResolver.php:61
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver
Definition: MiddlewareStackResolver.php:29
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\$cache
‪PhpFrontendCache $cache
Definition: MiddlewareStackResolver.php:40
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:142
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\$dependencyOrderingService
‪DependencyOrderingService $dependencyOrderingService
Definition: MiddlewareStackResolver.php:36
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\sanitizeMiddlewares
‪array sanitizeMiddlewares(array $allMiddlewares)
Definition: MiddlewareStackResolver.php:111
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:31
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\getCacheIdentifier
‪string getCacheIdentifier(string $stackName)
Definition: MiddlewareStackResolver.php:138
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver\$packageManager
‪PackageManager $packageManager
Definition: MiddlewareStackResolver.php:32
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:3