PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
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

107 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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