[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Package / AbstractServiceProvider.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Package;
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 Psr\Container\ContainerInterface;
19 use Psr\Log\LoggerAwareInterface;
20 use TYPO3\CMS\Core\DependencyInjection\ServiceProviderInterface;
21 use TYPO3\CMS\Core\Log\LogManager;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * @internal
26 */
27 abstract class AbstractServiceProvider implements ServiceProviderInterface
28 {
29 /**
30 * Return the path to the package location, including trailing slash
31 * should return the value of: __DIR__ . '/../'
32 * for ServiceProviders located in the Classes/ directory
33 *
34 * @return string
35 */
36 abstract protected static function getPackagePath(): string;
37
38 /**
39 * @return array
40 */
41 abstract public function getFactories(): array;
42
43 /**
44 * @return array
45 */
46 public function getExtensions(): array
47 {
48 return [
49 'middlewares' => [ static::class, 'configureMiddlewares' ],
50 ];
51 }
52
53 /**
54 * @param ContainerInterface $container
55 * @param array $middlewares
56 * @param string $path supplied when invoked internally through PseudoServiceProvider
57 * @return array
58 */
59 public static function configureMiddlewares(ContainerInterface $container, array $middlewares, string $path = null): array
60 {
61 $packageConfiguration = ($path ?? static::getPackagePath()) . 'Configuration/RequestMiddlewares.php';
62 if (file_exists($packageConfiguration)) {
63 $middlewaresInPackage = require $packageConfiguration;
64 if (is_array($middlewaresInPackage)) {
65 $middlewares = array_replace_recursive($middlewares, $middlewaresInPackage);
66 }
67 }
68
69 return $middlewares;
70 }
71
72 /**
73 * Create an instance of a class. Supports auto injection of the logger.
74 *
75 * @param ContainerInterface $container
76 * @param string $className name of the class to instantiate, must not be empty and not start with a backslash
77 * @param array $constructorArguments Arguments for the constructor
78 * @return mixed
79 */
80 protected static function new(ContainerInterface $container, string $className, array $constructorArguments = [])
81 {
82 // Support $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'] (xclasses) and class alias maps
83 $instance = GeneralUtility::makeInstanceForDi($className, ...$constructorArguments);
84
85 if ($instance instanceof LoggerAwareInterface) {
86 $instance->setLogger($container->get(LogManager::class)->getLogger($className));
87 }
88 return $instance;
89 }
90 }