‪TYPO3CMS  ‪main
AbstractServiceProvider.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 
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\Log\LoggerAwareInterface;
30 
35 {
41  abstract protected static function ‪getPackagePath(): string;
42 
48  abstract protected static function ‪getPackageName(): string;
49 
50  abstract public function ‪getFactories(): array;
51 
52  public function ‪getExtensions(): array
53  {
54  return [
55  'middlewares' => [ static::class, 'configureMiddlewares' ],
56  'backend.routes' => [ static::class, 'configureBackendRoutes' ],
57  'backend.modules' => [ static::class, 'configureBackendModules' ],
58  'content.security.policies' => [static::class, 'configureContentSecurityPolicies'],
59  'icons' => [ static::class, 'configureIcons' ],
60  ];
61  }
62 
66  public static function ‪configureMiddlewares(ContainerInterface $container, \ArrayObject $middlewares, string $path = null): \ArrayObject
67  {
68  $packageConfiguration = ($path ?? static::getPackagePath()) . 'Configuration/RequestMiddlewares.php';
69  if (file_exists($packageConfiguration)) {
70  $middlewaresInPackage = require $packageConfiguration;
71  if (is_array($middlewaresInPackage)) {
72  $middlewares->exchangeArray(array_replace_recursive($middlewares->getArrayCopy(), $middlewaresInPackage));
73  }
74  }
75 
76  return $middlewares;
77  }
78 
83  public static function ‪configureBackendRoutes(ContainerInterface $container, \ArrayObject $routes, string $path = null, string $packageName = null): \ArrayObject
84  {
85  $path = $path ?? static::getPackagePath();
86  $packageName = $packageName ?? static::getPackageName();
87  $routesFileNameForPackage = $path . 'Configuration/Backend/Routes.php';
88  if (file_exists($routesFileNameForPackage)) {
89  $definedRoutesInPackage = require $routesFileNameForPackage;
90  if (is_array($definedRoutesInPackage)) {
91  array_walk($definedRoutesInPackage, static function (&$options) use ($packageName, $path) {
92  // Add packageName and absolutePackagePath to all routes
93  $options['packageName'] = $packageName;
94  $options['absolutePackagePath'] = $path;
95  });
96  $routes->exchangeArray(array_merge($routes->getArrayCopy(), $definedRoutesInPackage));
97  }
98  }
99  $routesFileNameForPackage = $path . 'Configuration/Backend/AjaxRoutes.php';
100  if (file_exists($routesFileNameForPackage)) {
101  $definedRoutesInPackage = require $routesFileNameForPackage;
102  if (is_array($definedRoutesInPackage)) {
103  foreach ($definedRoutesInPackage as $routeIdentifier => $routeOptions) {
104  // prefix the route with "ajax_" as "namespace"
105  $routeOptions['path'] = '/ajax' . $routeOptions['path'];
106  $routeOptions['packageName'] = $packageName;
107  $routeOptions['absolutePackagePath'] = $path;
108  $routes['ajax_' . $routeIdentifier] = $routeOptions;
109  $routes['ajax_' . $routeIdentifier]['ajax'] = true;
110  }
111  }
112  }
113 
114  return $routes;
115  }
116 
121  public static function ‪configureBackendModules(ContainerInterface $container, \ArrayObject $modules, string $path = null, string $packageName = null): \ArrayObject
122  {
123  $path = $path ?? static::getPackagePath();
124  $packageName = $packageName ?? static::getPackageName();
125  $modulesFileNameForPackage = $path . 'Configuration/Backend/Modules.php';
126  if (file_exists($modulesFileNameForPackage)) {
127  $definedModulesInPackage = require $modulesFileNameForPackage;
128  if (is_array($definedModulesInPackage)) {
129  array_walk($definedModulesInPackage, static function (&$module) use ($packageName, $path) {
130  // Add packageName and absolutePackagePath to all modules
131  $module['packageName'] = $packageName;
132  $module['absolutePackagePath'] = $path;
133  });
134  $modules->exchangeArray(array_merge($modules->getArrayCopy(), $definedModulesInPackage));
135  }
136  }
137  return $modules;
138  }
139 
144  public static function configureContentSecurityPolicies(ContainerInterface $container, ‪Map $mutations, string $path = null, string $packageName = null): ‪Map
145  {
146  $path = $path ?? static::getPackagePath();
147  $packageName = $packageName ?? static::getPackageName();
148  $fileName = $path . 'Configuration/ContentSecurityPolicies.php';
149  if (file_exists($fileName)) {
151  $mutationsInPackage = require $fileName;
152  foreach ($mutationsInPackage as $scope => $mutation) {
153  if (!isset($mutations[$scope])) {
154  $mutations[$scope] = new ‪Map();
155  }
156  $origin = new MutationOrigin(MutationOriginType::package, $packageName);
157  $mutations[$scope][$origin] = $mutation;
158  }
159  }
160  return $mutations;
161  }
162 
163  public static function configureIcons(ContainerInterface $container, \ArrayObject ‪$icons, string $path = null): \ArrayObject
164  {
165  $path = $path ?? static::getPackagePath();
166  $iconsFileNameForPackage = $path . 'Configuration/Icons.php';
167  if (file_exists($iconsFileNameForPackage)) {
168  $definedIconsInPackage = require $iconsFileNameForPackage;
169  if (is_array($definedIconsInPackage)) {
170  ‪$icons->exchangeArray(array_merge(‪$icons->getArrayCopy(), $definedIconsInPackage));
171  }
172  }
173  return ‪$icons;
174  }
175 
183  protected static function new(ContainerInterface $container, string $className, array $constructorArguments = [])
184  {
185  // Support $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'] (xclasses) and class alias maps
186  $instance = GeneralUtility::makeInstanceForDi($className, ...$constructorArguments);
187 
188  if ($instance instanceof LoggerAwareInterface) {
189  $instance->setLogger($container->get(LogManager::class)->getLogger($className));
190  }
191  return $instance;
192  }
193 }
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureMiddlewares
‪static configureMiddlewares(ContainerInterface $container, \ArrayObject $middlewares, string $path=null)
Definition: AbstractServiceProvider.php:66
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getPackagePath
‪static getPackagePath()
‪TYPO3\CMS\Core\Package\AbstractServiceProvider
Definition: AbstractServiceProvider.php:35
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getFactories
‪getFactories()
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationOrigin
Definition: MutationOrigin.php:25
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\$icons
‪return $icons
Definition: AbstractServiceProvider.php:173
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureBackendRoutes
‪static configureBackendRoutes(ContainerInterface $container, \ArrayObject $routes, string $path=null, string $packageName=null)
Definition: AbstractServiceProvider.php:83
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureBackendModules
‪static configureBackendModules(ContainerInterface $container, \ArrayObject $modules, string $path=null, string $packageName=null)
Definition: AbstractServiceProvider.php:121
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getExtensions
‪getExtensions()
Definition: AbstractServiceProvider.php:52
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope
Definition: Scope.php:30
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderInterface
Definition: ServiceProviderInterface.php:26
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Package
Definition: AbstractServiceProvider.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationCollection
Definition: MutationCollection.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Type\Map
Definition: Map.php:47
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationOriginType
‪MutationOriginType
Definition: MutationOriginType.php:21
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getPackageName
‪static getPackageName()