imagify
/
classes
/
Dependencies
/
League
/
Container
/
ServiceProvider
/
ServiceProviderAggregate.php
ServiceProviderAggregate.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2, at classes/Dependencies/League/Container/ServiceProvider/ServiceProviderAggregate.php
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Imagify\Dependencies\League\Container\ServiceProvider; |
| 4 | |
| 5 | use Generator; |
| 6 | use Imagify\Dependencies\League\Container\{ContainerAwareInterface, ContainerAwareTrait}; |
| 7 | use Imagify\Dependencies\League\Container\Exception\ContainerException; |
| 8 | |
| 9 | class ServiceProviderAggregate implements ServiceProviderAggregateInterface |
| 10 | { |
| 11 | use ContainerAwareTrait; |
| 12 | |
| 13 | /** |
| 14 | * @var ServiceProviderInterface[] |
| 15 | */ |
| 16 | protected $providers = []; |
| 17 | |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $registered = []; |
| 22 | |
| 23 | /** |
| 24 | * {@inheritdoc} |
| 25 | */ |
| 26 | public function add($provider) : ServiceProviderAggregateInterface |
| 27 | { |
| 28 | if (is_string($provider) && $this->getContainer()->has($provider)) { |
| 29 | $provider = $this->getContainer()->get($provider); |
| 30 | } elseif (is_string($provider) && class_exists($provider)) { |
| 31 | $provider = new $provider; |
| 32 | } |
| 33 | |
| 34 | if (in_array($provider, $this->providers, true)) { |
| 35 | return $this; |
| 36 | } |
| 37 | |
| 38 | if ($provider instanceof ContainerAwareInterface) { |
| 39 | $provider->setLeagueContainer($this->getLeagueContainer()); |
| 40 | } |
| 41 | |
| 42 | if ($provider instanceof BootableServiceProviderInterface) { |
| 43 | $provider->boot(); |
| 44 | } |
| 45 | |
| 46 | if ($provider instanceof ServiceProviderInterface) { |
| 47 | $this->providers[] = $provider; |
| 48 | |
| 49 | return $this; |
| 50 | } |
| 51 | |
| 52 | throw new ContainerException( |
| 53 | 'A service provider must be a fully qualified class name or instance ' . |
| 54 | 'of (\Imagify\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface)' |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * {@inheritdoc} |
| 60 | */ |
| 61 | public function provides(string $service) : bool |
| 62 | { |
| 63 | foreach ($this->getIterator() as $provider) { |
| 64 | if ($provider->provides($service)) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritdoc} |
| 74 | */ |
| 75 | public function getIterator() : Generator |
| 76 | { |
| 77 | $count = count($this->providers); |
| 78 | |
| 79 | for ($i = 0; $i < $count; $i++) { |
| 80 | yield $this->providers[$i]; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritdoc} |
| 86 | */ |
| 87 | public function register(string $service) |
| 88 | { |
| 89 | if (false === $this->provides($service)) { |
| 90 | throw new ContainerException( |
| 91 | sprintf('(%s) is not provided by a service provider', $service) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | foreach ($this->getIterator() as $provider) { |
| 96 | if (in_array($provider->getIdentifier(), $this->registered, true)) { |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | if ($provider->provides($service)) { |
| 101 | $this->registered[] = $provider->getIdentifier(); |
| 102 | $provider->register(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 |