‪TYPO3CMS  ‪main
ServiceProvider.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\Backend;
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\EventDispatcher\EventDispatcherInterface;
46 
51 {
52  protected static function ‪getPackagePath(): string
53  {
54  return __DIR__ . '/../';
55  }
56 
57  protected static function ‪getPackageName(): string
58  {
59  return 'typo3/cms-backend';
60  }
61 
62  public function ‪getFactories(): array
63  {
64  return [
65  Application::class => ‪self::getApplication(...),
66  RequestHandler::class => ‪self::getRequestHandler(...),
67  RouteDispatcher::class => ‪self::getRouteDispatcher(...),
68  UriBuilder::class => ‪self::getUriBuilder(...),
69  ModuleProvider::class => ‪self::getModuleProvider(...),
70  ModuleFactory::class => ‪self::getModuleFactory(...),
71  ModuleRegistry::class => ‪self::getModuleRegistry(...),
72  'backend.middlewares' => ‪self::getBackendMiddlewares(...),
73  'backend.routes' => ‪self::getBackendRoutes(...),
74  'backend.routes.warmer' => ‪self::getBackendRoutesWarmer(...),
75  'backend.modules' => ‪self::getBackendModules(...),
76  'backend.modules.warmer' => ‪self::getBackendModulesWarmer(...),
77  ];
78  }
79 
80  public function ‪getExtensions(): array
81  {
82  return [
83  Router::class => ‪self::configureBackendRouter(...),
84  ListenerProvider::class => ‪self::addEventListeners(...),
85  ] + parent::getExtensions();
86  }
87 
88  public static function ‪getApplication(ContainerInterface $container): ‪Application
89  {
90  $requestHandler = new ‪MiddlewareDispatcher(
91  $container->get(RequestHandler::class),
92  $container->get('backend.middlewares'),
93  $container
94  );
95  return new ‪Application(
96  $requestHandler,
97  $container->get(Context::class),
98  );
99  }
100 
101  public static function ‪getRequestHandler(ContainerInterface $container): ‪RequestHandler
102  {
103  return new ‪RequestHandler(
104  $container->get(RouteDispatcher::class),
105  $container->get(UriBuilder::class),
106  $container->get(ListenerProvider::class)
107  );
108  }
109 
110  public static function ‪getRouteDispatcher(ContainerInterface $container): ‪RouteDispatcher
111  {
112  return ‪self::new($container, RouteDispatcher::class, [
113  $container->get(FormProtectionFactory::class),
114  $container->get(AccessFactory::class),
115  $container->get(AccessStorage::class),
116  $container,
117  ]);
118  }
119 
120  public static function ‪getUriBuilder(ContainerInterface $container): ‪UriBuilder
121  {
122  return ‪self::new($container, UriBuilder::class, [
123  $container->get(Router::class),
124  $container->get(FormProtectionFactory::class),
125  $container->get(RequestContextFactory::class),
126  ]);
127  }
128 
129  public static function ‪getModuleProvider(ContainerInterface $container): ‪ModuleProvider
130  {
131  return ‪self::new($container, ModuleProvider::class, [
132  $container->get(ModuleRegistry::class),
133  ]);
134  }
135 
136  public static function ‪getModuleFactory(ContainerInterface $container): ‪ModuleFactory
137  {
138  return ‪self::new($container, ModuleFactory::class, [
139  $container->get(IconRegistry::class),
140  $container->get(EventDispatcherInterface::class),
141  ]);
142  }
143 
144  public static function ‪getModuleRegistry(ContainerInterface $container): ‪ModuleRegistry
145  {
146  $moduleFactory = $container->get(ModuleFactory::class);
147  $cache = $container->get('cache.core');
148  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class)->withPrefix('BackendModules')->toString();
149  $modulesFromPackages = $cache->require($cacheIdentifier);
150  if ($modulesFromPackages === false) {
151  $modulesFromPackages = $container->get('backend.modules')->getArrayCopy();
152  $modulesFromPackages = $moduleFactory->adaptAliasMappingFromModuleConfiguration($modulesFromPackages);
153  $cache->set($cacheIdentifier, 'return ' . var_export($modulesFromPackages, true) . ';');
154  }
155 
156  foreach ($modulesFromPackages as ‪$identifier => $configuration) {
157  $modulesFromPackages[‪$identifier] = $moduleFactory->createModule(‪$identifier, $configuration);
158  }
159 
160  return ‪self::new($container, ModuleRegistry::class, [$modulesFromPackages]);
161  }
162 
167  public static function ‪getBackendMiddlewares(ContainerInterface $container): \ArrayObject
168  {
169  return new \ArrayObject($container->get(MiddlewareStackResolver::class)->resolve('backend'));
170  }
171 
172  public static function ‪configureBackendRouter(ContainerInterface $container, ‪Router $router = null): ‪Router
173  {
174  $router = $router ?? ‪self::new($container, Router::class, [
175  $container->get(RequestContextFactory::class),
176  $container->get(BackendEntryPointResolver::class),
177  ]);
178  $cache = $container->get('cache.core');
179 
180  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class)->withPrefix('BackendRoutes')->toString();
181  $routesFromPackages = $cache->require($cacheIdentifier);
182  if ($routesFromPackages === false) {
183  $routesFromPackages = $container->get('backend.routes')->getArrayCopy();
184  $cache->set($cacheIdentifier, 'return ' . var_export($routesFromPackages, true) . ';');
185  }
186 
187  foreach ($routesFromPackages as $name => $options) {
188  $path = $options['path'];
189  $methods = $options['methods'] ?? [];
190  $aliases = $options['aliases'] ?? [];
191  unset($options['path'], $options['methods'], $options['aliases']);
192  $route = new ‪Route($path, $options);
193  if (count($methods) > 0) {
194  $route->setMethods($methods);
195  }
196  $router->addRoute($name, $route, $aliases);
197  }
198 
199  // Add routes from all modules
200  $container->get(ModuleRegistry::class)->registerRoutesForModules($router);
201 
202  return $router;
203  }
204 
205  public static function ‪getBackendRoutes(ContainerInterface $container): \ArrayObject
206  {
207  return new \ArrayObject();
208  }
209 
210  public static function ‪getBackendRoutesWarmer(ContainerInterface $container): \Closure
211  {
212  return static function (‪CacheWarmupEvent $event) use ($container) {
213  if ($event->hasGroup('system')) {
214  $cache = $container->get('cache.core');
215  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class)->withPrefix('BackendRoutes')->toString();
216  $routesFromPackages = $container->get('backend.routes')->getArrayCopy();
217  $cache->set($cacheIdentifier, 'return ' . var_export($routesFromPackages, true) . ';');
218  }
219  };
220  }
221 
222  public static function ‪getBackendModules(ContainerInterface $container): \ArrayObject
223  {
224  return new \ArrayObject();
225  }
226 
227  public static function ‪getBackendModulesWarmer(ContainerInterface $container): \Closure
228  {
229  return static function (‪CacheWarmupEvent $event) use ($container) {
230  if ($event->hasGroup('system')) {
231  $cache = $container->get('cache.core');
232  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class)->withPrefix('BackendModules')->toString();
233  $modulesFromPackages = $container->get('backend.modules')->getArrayCopy();
234  $cache->set($cacheIdentifier, 'return ' . var_export($modulesFromPackages, true) . ';');
235  }
236  };
237  }
238 
239  public static function ‪addEventListeners(ContainerInterface $container, ‪ListenerProvider $listenerProvider): ‪ListenerProvider
240  {
241  $listenerProvider->‪addListener(CacheWarmupEvent::class, 'backend.routes.warmer');
242  $listenerProvider->‪addListener(CacheWarmupEvent::class, 'backend.modules.warmer');
243 
244  return $listenerProvider;
245  }
246 }
‪TYPO3\CMS\Backend\Http\RouteDispatcher
Definition: RouteDispatcher.php:41
‪TYPO3\CMS\Backend\ServiceProvider\getRouteDispatcher
‪static getRouteDispatcher(ContainerInterface $container)
Definition: ServiceProvider.php:110
‪TYPO3\CMS\Core\Package\AbstractServiceProvider
Definition: AbstractServiceProvider.php:35
‪TYPO3\CMS\Backend\Http\Application
Definition: Application.php:33
‪TYPO3\CMS\Backend\Module\ModuleRegistry
Definition: ModuleRegistry.php:28
‪TYPO3\CMS\Backend\Module\ModuleFactory
Definition: ModuleFactory.php:29
‪TYPO3\CMS\Backend\Security\SudoMode\Access\AccessStorage
Definition: AccessStorage.php:30
‪TYPO3\CMS\Backend\ServiceProvider\addEventListeners
‪static addEventListeners(ContainerInterface $container, ListenerProvider $listenerProvider)
Definition: ServiceProvider.php:239
‪TYPO3\CMS\Backend
‪TYPO3\CMS\Backend\ServiceProvider\getBackendRoutesWarmer
‪static getBackendRoutesWarmer(ContainerInterface $container)
Definition: ServiceProvider.php:210
‪TYPO3\CMS\Backend\ServiceProvider\getBackendModulesWarmer
‪static getBackendModulesWarmer(ContainerInterface $container)
Definition: ServiceProvider.php:227
‪TYPO3\CMS\Core\Package\Cache\PackageDependentCacheIdentifier
Definition: PackageDependentCacheIdentifier.php:30
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Backend\ServiceProvider\getApplication
‪static getApplication(ContainerInterface $container)
Definition: ServiceProvider.php:88
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver
Definition: MiddlewareStackResolver.php:31
‪TYPO3\CMS\Backend\ServiceProvider
Definition: ServiceProvider.php:51
‪TYPO3\CMS\Backend\ServiceProvider\getUriBuilder
‪static getUriBuilder(ContainerInterface $container)
Definition: ServiceProvider.php:120
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\new
‪static mixed new(ContainerInterface $container, string $className, array $constructorArguments=[])
Definition: AbstractServiceProvider.php:183
‪TYPO3\CMS\Backend\ServiceProvider\getFactories
‪getFactories()
Definition: ServiceProvider.php:62
‪TYPO3\CMS\Backend\ServiceProvider\getBackendMiddlewares
‪static getBackendMiddlewares(ContainerInterface $container)
Definition: ServiceProvider.php:167
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Module\ModuleProvider
Definition: ModuleProvider.php:29
‪TYPO3\CMS\Core\Cache\Event\CacheWarmupEvent
Definition: CacheWarmupEvent.php:24
‪TYPO3\CMS\Backend\ServiceProvider\getPackageName
‪static getPackageName()
Definition: ServiceProvider.php:57
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Backend\ServiceProvider\getPackagePath
‪static getPackagePath()
Definition: ServiceProvider.php:52
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher
Definition: MiddlewareDispatcher.php:35
‪TYPO3\CMS\Backend\ServiceProvider\getModuleFactory
‪static getModuleFactory(ContainerInterface $container)
Definition: ServiceProvider.php:136
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\addListener
‪addListener(string $event, string $service, string $method=null, string $identifier=null)
Definition: ListenerProvider.php:50
‪TYPO3\CMS\Backend\Security\SudoMode\Access\AccessFactory
Definition: AccessFactory.php:30
‪TYPO3\CMS\Backend\ServiceProvider\configureBackendRouter
‪static configureBackendRouter(ContainerInterface $container, Router $router=null)
Definition: ServiceProvider.php:172
‪TYPO3\CMS\Backend\ServiceProvider\getRequestHandler
‪static getRequestHandler(ContainerInterface $container)
Definition: ServiceProvider.php:101
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\Http\RequestHandler
Definition: RequestHandler.php:43
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:23
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:32
‪TYPO3\CMS\Backend\ServiceProvider\getBackendModules
‪static getBackendModules(ContainerInterface $container)
Definition: ServiceProvider.php:222
‪TYPO3\CMS\Backend\ServiceProvider\getModuleRegistry
‪static getModuleRegistry(ContainerInterface $container)
Definition: ServiceProvider.php:144
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:43
‪TYPO3\CMS\Backend\ServiceProvider\getBackendRoutes
‪static getBackendRoutes(ContainerInterface $container)
Definition: ServiceProvider.php:205
‪TYPO3\CMS\Backend\ServiceProvider\getModuleProvider
‪static getModuleProvider(ContainerInterface $container)
Definition: ServiceProvider.php:129
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:40
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver
Definition: BackendEntryPointResolver.php:29
‪TYPO3\CMS\Backend\ServiceProvider\getExtensions
‪getExtensions()
Definition: ServiceProvider.php:80
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30