1 | <?php declare(strict_types=1); |
||
9 | class ServiceProviderAggregate implements ServiceProviderAggregateInterface |
||
10 | { |
||
11 | use ContainerAwareTrait; |
||
12 | |||
13 | /** |
||
14 | * @var \League\Container\ServiceProvider\ServiceProviderInterface[] |
||
15 | */ |
||
16 | protected $providers = []; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $registered = []; |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | 9 | public function add($provider): ServiceProviderAggregateInterface |
|
27 | { |
||
28 | 9 | if (is_string($provider) && $this->getContainer()->has($provider)) { |
|
29 | $provider = $this->getContainer()->get($provider); |
||
30 | 9 | } elseif (is_string($provider) && class_exists($provider)) { |
|
31 | $provider = new $provider; |
||
32 | } |
||
33 | |||
34 | 9 | if ($provider instanceof ContainerAwareInterface) { |
|
35 | 6 | $provider->setContainer($this->getContainer()); |
|
36 | } |
||
37 | |||
38 | 9 | if ($provider instanceof BootableServiceProviderInterface) { |
|
39 | 6 | $provider->boot(); |
|
40 | } |
||
41 | |||
42 | 9 | if ($provider instanceof ServiceProviderInterface) { |
|
43 | 6 | $this->providers[] = $provider; |
|
44 | |||
45 | 6 | return $this; |
|
46 | } |
||
47 | |||
48 | 3 | throw new ContainerException( |
|
49 | 'A service provider must be a fully qualified class name or instance ' . |
||
50 | 3 | 'of (\League\Container\ServiceProvider\ServiceProviderInterface)' |
|
51 | ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 9 | public function provides(string $service): bool |
|
58 | { |
||
59 | 9 | foreach ($this->getIterator() as $provider) { |
|
60 | 6 | if ($provider->provides($service)) { |
|
61 | 6 | return true; |
|
62 | } |
||
63 | } |
||
64 | |||
65 | 3 | return false; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 9 | public function getIterator(): Generator |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 6 | public function register(string $service) |
|
101 | } |
||
102 |