‪TYPO3CMS  10.4
ContainerBuilder.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 Symfony\Component\Config\FileLocator;
22 use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
23 use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
24 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
30 use TYPO3\CMS\Core\Package\PackageManager;
31 
35 class ContainerBuilder
36 {
40  protected $cacheIdentifiers;
41 
45  protected $defaultServices;
46 
50  protected $serviceProviderRegistryServiceName = 'service_provider_registry';
51 
55  public function __construct(array $earlyInstances)
56  {
57  $this->defaultServices = $earlyInstances + [ self::class => $this ];
58  }
59 
66  public function createDependencyInjectionContainer(PackageManager $packageManager, FrontendInterface $cache, bool $failsafe = false): ContainerInterface
67  {
68  if (!$cache instanceof PhpFrontend) {
69  throw new \RuntimeException('Cache must be instance of PhpFrontend', 1582022226);
70  }
71  $serviceProviderRegistry = new ServiceProviderRegistry($packageManager, $failsafe);
72 
73  if ($failsafe) {
74  return new FailsafeContainer($serviceProviderRegistry, $this->defaultServices);
75  }
76 
77  $container = null;
78 
79  $cacheIdentifier = $this->getCacheIdentifier($packageManager);
80  $containerClassName = $cacheIdentifier;
81 
82  $hasCache = $cache->requireOnce($cacheIdentifier) !== false;
83  if (!$hasCache) {
84  $containerBuilder = $this->buildContainer($packageManager, $serviceProviderRegistry);
85  $this->dumpContainer($containerBuilder, $cache, $cacheIdentifier);
86  $cache->requireOnce($cacheIdentifier);
87  }
88  $container = new $containerClassName();
89 
90  foreach ($this->defaultServices as $id => $service) {
91  $container->set('_early.' . $id, $service);
92  }
93 
94  $container->set($this->serviceProviderRegistryServiceName, $serviceProviderRegistry);
95 
96  return $container;
97  }
98 
104  protected function buildContainer(PackageManager $packageManager, ServiceProviderRegistry $registry): SymfonyContainerBuilder
105  {
106  $containerBuilder = new SymfonyContainerBuilder();
107 
108  $containerBuilder->addCompilerPass(new ServiceProviderCompilationPass($registry, $this->serviceProviderRegistryServiceName));
109 
110  $packages = $packageManager->getActivePackages();
111  foreach ($packages as $package) {
112  $diConfigDir = $package->getPackagePath() . 'Configuration/';
113  if (file_exists($diConfigDir . 'Services.php')) {
114  $phpFileLoader = new PhpFileLoader($containerBuilder, new FileLocator($diConfigDir));
115  $phpFileLoader->load('Services.php');
116  }
117  if (file_exists($diConfigDir . 'Services.yaml')) {
118  $yamlFileLoader = new YamlFileLoader($containerBuilder, new FileLocator($diConfigDir));
119  $yamlFileLoader->load('Services.yaml');
120  }
121  }
122  // Store defaults entries in the DIC container
123  // We need to use a workaround using aliases for synthetic services
124  // But that's common in symfony (same technique is used to provide the
125  // Symfony container interface as well).
126  foreach (array_keys($this->defaultServices) as $id) {
127  $syntheticId = '_early.' . $id;
128  $containerBuilder->register($syntheticId)->setSynthetic(true)->setPublic(true);
129  $containerBuilder->setAlias($id, $syntheticId)->setPublic(true);
130  }
131 
132  $containerBuilder->compile();
133 
134  return $containerBuilder;
135  }
136 
143  protected function dumpContainer(SymfonyContainerBuilder $containerBuilder, FrontendInterface $cache, string $cacheIdentifier): string
144  {
145  $containerClassName = $cacheIdentifier;
146 
147  $phpDumper = new PhpDumper($containerBuilder);
148  $code = $phpDumper->dump(['class' => $containerClassName]);
149  $code = str_replace('<?php', '', $code);
150  // We need to patch the generated source code to use GeneralUtility::makeInstanceForDi() instead of `new`.
151  // This is ugly, but has to stay, as long as we support SingletonInstances to be created/retrieved
152  // through GeneralUtility::makeInstance.
153  $code = preg_replace('/new ([^\‍(]+)\‍(/', '\\TYPO3\\CMS\\Core\\Utility\\GeneralUtility::makeInstanceForDi(\\1::class, ', $code);
154  if ($code === null) {
155  throw new \RuntimeException('Could not generate container code', 1599767133);
156  }
157  $code = str_replace(', )', ')', $code);
158 
159  $cache->set($cacheIdentifier, $code);
160 
161  return $code;
162  }
163 
168  protected function getCacheIdentifier(PackageManager $packageManager): string
169  {
170  $packageManagerCacheIdentifier = $packageManager->getCacheIdentifier() ?? '';
171  return $this->cacheIdentifiers[$packageManagerCacheIdentifier] ?? $this->createCacheIdentifier($packageManagerCacheIdentifier);
172  }
173 
178  protected function createCacheIdentifier(string $additionalIdentifier): string
179  {
180  return $this->cacheIdentifiers[$additionalIdentifier] = 'DependencyInjectionContainer_' . sha1((string)(new Typo3Version()) . ‪Environment::getProjectPath() . $additionalIdentifier . 'DependencyInjectionContainer');
181  }
182 }
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Core\DependencyInjection
Definition: AutowireInjectMethodsPass.php:18
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40