‪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;
22 use Symfony\Component\Finder\Finder;
33 
38 {
44  abstract protected static function ‪getPackagePath(): string;
45 
51  abstract protected static function ‪getPackageName(): string;
52 
53  abstract public function ‪getFactories(): array;
54 
55  public function ‪getExtensions(): array
56  {
57  return [
58  'middlewares' => [ static::class, 'configureMiddlewares' ],
59  'backend.routes' => [ static::class, 'configureBackendRoutes' ],
60  'backend.modules' => [ static::class, 'configureBackendModules' ],
61  'content.security.policies' => [ static::class, 'configureContentSecurityPolicies' ],
62  'icons' => [ static::class, 'configureIcons' ],
63  SetCollector::class => [ static::class, 'configureSetCollector' ],
64  ];
65  }
66 
70  public static function ‪configureMiddlewares(ContainerInterface $container, \ArrayObject $middlewares, string $path = null): \ArrayObject
71  {
72  $packageConfiguration = ($path ?? static::getPackagePath()) . 'Configuration/RequestMiddlewares.php';
73  if (file_exists($packageConfiguration)) {
74  $middlewaresInPackage = require $packageConfiguration;
75  if (is_array($middlewaresInPackage)) {
76  $middlewares->exchangeArray(array_replace_recursive($middlewares->getArrayCopy(), $middlewaresInPackage));
77  }
78  }
79 
80  return $middlewares;
81  }
82 
87  public static function ‪configureBackendRoutes(ContainerInterface $container, \ArrayObject $routes, string $path = null, string $packageName = null): \ArrayObject
88  {
89  $path = $path ?? static::getPackagePath();
90  $packageName = $packageName ?? static::getPackageName();
91  $routesFileNameForPackage = $path . 'Configuration/Backend/Routes.php';
92  if (file_exists($routesFileNameForPackage)) {
93  $definedRoutesInPackage = require $routesFileNameForPackage;
94  if (is_array($definedRoutesInPackage)) {
95  array_walk($definedRoutesInPackage, static function (array &$options) use ($packageName, $path): void {
96  // Add packageName and absolutePackagePath to all routes
97  $options['packageName'] = $packageName;
98  $options['absolutePackagePath'] = $path;
99  });
100  $routes->exchangeArray(array_merge($routes->getArrayCopy(), $definedRoutesInPackage));
101  }
102  }
103  $routesFileNameForPackage = $path . 'Configuration/Backend/AjaxRoutes.php';
104  if (file_exists($routesFileNameForPackage)) {
105  $definedRoutesInPackage = require $routesFileNameForPackage;
106  if (is_array($definedRoutesInPackage)) {
107  foreach ($definedRoutesInPackage as $routeIdentifier => $routeOptions) {
108  // prefix the route with "ajax_" as "namespace"
109  $routeOptions['path'] = '/ajax' . $routeOptions['path'];
110  $routeOptions['packageName'] = $packageName;
111  $routeOptions['absolutePackagePath'] = $path;
112  $routes['ajax_' . $routeIdentifier] = $routeOptions;
113  $routes['ajax_' . $routeIdentifier]['ajax'] = true;
114  }
115  }
116  }
117 
118  return $routes;
119  }
120 
125  public static function ‪configureBackendModules(ContainerInterface $container, \ArrayObject $modules, string $path = null, string $packageName = null): \ArrayObject
126  {
127  $path = $path ?? static::getPackagePath();
128  $packageName = $packageName ?? static::getPackageName();
129  $modulesFileNameForPackage = $path . 'Configuration/Backend/Modules.php';
130  if (file_exists($modulesFileNameForPackage)) {
131  $definedModulesInPackage = require $modulesFileNameForPackage;
132  if (is_array($definedModulesInPackage)) {
133  array_walk($definedModulesInPackage, static function (array &$module) use ($packageName, $path): void {
134  // Add packageName and absolutePackagePath to all modules
135  $module['packageName'] = $packageName;
136  $module['absolutePackagePath'] = $path;
137  });
138  $modules->exchangeArray(array_merge($modules->getArrayCopy(), $definedModulesInPackage));
139  }
140  }
141  return $modules;
142  }
143 
148  public static function configureContentSecurityPolicies(ContainerInterface $container, ‪Map $mutations, string $path = null, string $packageName = null): ‪Map
149  {
150  $path = $path ?? static::getPackagePath();
151  $packageName = $packageName ?? static::getPackageName();
152  $fileName = $path . 'Configuration/ContentSecurityPolicies.php';
153  if (file_exists($fileName)) {
155  $mutationsInPackage = require $fileName;
156  foreach ($mutationsInPackage as $scope => $mutation) {
157  if (!isset($mutations[$scope])) {
158  $mutations[$scope] = new ‪Map();
159  }
160  $origin = new MutationOrigin(MutationOriginType::package, $packageName);
161  $mutations[$scope][$origin] = $mutation;
162  }
163  }
164  return $mutations;
165  }
166 
167  public static function configureIcons(ContainerInterface $container, \ArrayObject ‪$icons, string $path = null): \ArrayObject
168  {
169  $path = $path ?? static::getPackagePath();
170  $iconsFileNameForPackage = $path . 'Configuration/Icons.php';
171  if (file_exists($iconsFileNameForPackage)) {
172  $definedIconsInPackage = require $iconsFileNameForPackage;
173  if (is_array($definedIconsInPackage)) {
174  ‪$icons->exchangeArray(array_merge(‪$icons->getArrayCopy(), $definedIconsInPackage));
175  }
176  }
177  return ‪$icons;
178  }
179 
180  public static function ‪configureSetCollector(ContainerInterface $container, ‪SetCollector $setCollector, string $path = null): ‪SetCollector
181  {
182  $path = $path ?? static::getPackagePath();
183  $setPath = $path . 'Configuration/Sets';
184 
185  try {
186  ‪$finder = Finder::create()
187  ->files()
188  ->sortByName()
189  ->depth(1)
190  ->name('config.yaml')
191  ->in($setPath);
192  } catch (\InvalidArgumentException) {
193  // No such directory in this package
194  return $setCollector;
195  }
196 
197  $setProvider = new ‪YamlSetDefinitionProvider();
198  foreach (‪$finder as $fileInfo) {
199  try {
200  $setCollector->‪add($setProvider->get($fileInfo));
201  } catch (\RuntimeException $e) {
202  $logger = $container->get(LogManager::class)->getLogger(self::class);
203  $logger->error('Invalid set in {file}: {reason}', [
204  'file' => $fileInfo->getPathname(),
205  'reason' => $e->getMessage(),
206  ]);
207  }
208  }
209 
210  return $setCollector;
211  }
212 
220  protected static function new(ContainerInterface $container, string $className, array $constructorArguments = [])
221  {
222  // Support $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'] (xclasses) and class alias maps
223  $instance = GeneralUtility::makeInstanceForDi($className, ...$constructorArguments);
224 
225  if ($instance instanceof LoggerAwareInterface) {
226  $instance->setLogger($container->get(LogManager::class)->getLogger($className));
227  }
228  return $instance;
229  }
230 }
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureMiddlewares
‪static configureMiddlewares(ContainerInterface $container, \ArrayObject $middlewares, string $path=null)
Definition: AbstractServiceProvider.php:70
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getPackagePath
‪static getPackagePath()
‪TYPO3\CMS\Core\Package\AbstractServiceProvider
Definition: AbstractServiceProvider.php:38
‪$finder
‪if(PHP_SAPI !=='cli') $finder
Definition: header-comment.php:22
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureSetCollector
‪static configureSetCollector(ContainerInterface $container, SetCollector $setCollector, string $path=null)
Definition: AbstractServiceProvider.php:180
‪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:177
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureBackendRoutes
‪static configureBackendRoutes(ContainerInterface $container, \ArrayObject $routes, string $path=null, string $packageName=null)
Definition: AbstractServiceProvider.php:87
‪TYPO3\CMS\Core\Site\Set\SetCollector\add
‪add(SetDefinition $set)
Definition: SetCollector.php:36
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureBackendModules
‪static configureBackendModules(ContainerInterface $container, \ArrayObject $modules, string $path=null, string $packageName=null)
Definition: AbstractServiceProvider.php:125
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getExtensions
‪getExtensions()
Definition: AbstractServiceProvider.php:55
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope
Definition: Scope.php:30
‪TYPO3\CMS\Core\Site\Set\YamlSetDefinitionProvider
Definition: YamlSetDefinitionProvider.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\Site\Set\SetCollector
Definition: SetCollector.php:24
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationOriginType
‪MutationOriginType
Definition: MutationOriginType.php:21
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\getPackageName
‪static getPackageName()