‪TYPO3CMS  10.4
ServiceProviderRegistry.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;
22 use TYPO3\CMS\Core\Package\PackageManager;
23 
30 class ‪ServiceProviderRegistry implements \IteratorAggregate
31 {
35  private ‪$packageManager;
36 
40  private ‪$failsafe;
41 
47  private ‪$instances;
48 
55  private ‪$serviceFactories = [];
56 
63  private ‪$serviceExtensions = [];
64 
73  public function ‪__construct(PackageManager ‪$packageManager, bool ‪$failsafe = false)
74  {
75  $this->packageManager = ‪$packageManager;
76  $this->failsafe = ‪$failsafe;
77  }
78 
85  public function ‪has(string $packageKey): bool
86  {
87  if (isset($this->instances[$packageKey])) {
88  return true;
89  }
90 
91  if ($this->packageManager->isPackageActive($packageKey)) {
92  if ($this->failsafe && $this->packageManager->getPackage($packageKey)->isPartOfMinimalUsableSystem() === false) {
93  return false;
94  }
95  return true;
96  }
97 
98  return false;
99  }
100 
107  public function get(string $packageKey): ‪ServiceProviderInterface
108  {
109  return $this->instances[$packageKey] ?? $this->‪create($packageKey);
110  }
111 
119  private function ‪create(string $packageKey, ‪Package $package = null): ‪ServiceProviderInterface
120  {
121  if ($package === null) {
122  if (!$this->packageManager->isPackageActive($packageKey)) {
123  throw new \InvalidArgumentException('Package ' . $packageKey . ' is not active', 1550351445);
124  }
125  $package = $this->packageManager->getPackage($packageKey);
126  }
127  $serviceProviderClassName = $package->getServiceProvider();
128  $instance = new $serviceProviderClassName($package);
129 
130  if (!$instance instanceof ‪ServiceProviderInterface) {
131  throw new \InvalidArgumentException('Service providers need to implement ' . ServiceProviderInterface::class, 1550302554);
132  }
133 
134  return $this->instances[$packageKey] = $instance;
135  }
136 
144  public function ‪getFactories(string $packageKey): array
145  {
146  return $this->serviceFactories[$packageKey] ?? ($this->serviceFactories[$packageKey] = $this->get($packageKey)->‪getFactories());
147  }
148 
156  public function ‪getExtensions(string $packageKey): array
157  {
158  return $this->serviceExtensions[$packageKey] ?? ($this->serviceExtensions[$packageKey] = $this->get($packageKey)->‪getExtensions());
159  }
160 
167  public function ‪createService(string $packageKey, string $serviceName, ContainerInterface $container)
168  {
169  $factory = $this->‪getFactories($packageKey)[$serviceName];
170  return $factory($container);
171  }
172 
181  public function ‪extendService(string $packageKey, string $serviceName, ContainerInterface $container, $previous = null)
182  {
183  $extension = $this->‪getExtensions($packageKey)[$serviceName];
184  return $extension($container, $previous);
185  }
186 
190  public function ‪getIterator(): \Generator
191  {
192  foreach ($this->packageManager->getActivePackages() as $package) {
193  if ($this->failsafe && $package->isPartOfMinimalUsableSystem() === false) {
194  continue;
195  }
196  $packageKey = $package->getPackageKey();
197  yield $packageKey => ($this->instances[$packageKey] ?? $this->‪create($packageKey, $package));
198  }
199  }
200 }
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\getIterator
‪Generator getIterator()
Definition: ServiceProviderRegistry.php:185
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\has
‪bool has(string $packageKey)
Definition: ServiceProviderRegistry.php:80
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\createService
‪mixed createService(string $packageKey, string $serviceName, ContainerInterface $container)
Definition: ServiceProviderRegistry.php:162
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\$instances
‪array $instances
Definition: ServiceProviderRegistry.php:44
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\__construct
‪__construct(PackageManager $packageManager, bool $failsafe=false)
Definition: ServiceProviderRegistry.php:68
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\$failsafe
‪bool $failsafe
Definition: ServiceProviderRegistry.php:38
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\create
‪ServiceProviderInterface create(string $packageKey, Package $package=null)
Definition: ServiceProviderRegistry.php:114
‪TYPO3\CMS\Core\DependencyInjection
Definition: AutowireInjectMethodsPass.php:18
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:26
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry
Definition: ServiceProviderRegistry.php:31
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderInterface
Definition: ServiceProviderInterface.php:24
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\extendService
‪mixed extendService(string $packageKey, string $serviceName, ContainerInterface $container, $previous=null)
Definition: ServiceProviderRegistry.php:176
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\$serviceExtensions
‪array $serviceExtensions
Definition: ServiceProviderRegistry.php:58
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\$serviceFactories
‪array $serviceFactories
Definition: ServiceProviderRegistry.php:51
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\getExtensions
‪array getExtensions(string $packageKey)
Definition: ServiceProviderRegistry.php:151
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\getFactories
‪array getFactories(string $packageKey)
Definition: ServiceProviderRegistry.php:139
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry\$packageManager
‪PackageManager $packageManager
Definition: ServiceProviderRegistry.php:34