|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Container\ServiceProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Generator; |
|
6
|
|
|
use League\Container\{ContainerAwareInterface, ContainerAwareTrait}; |
|
7
|
|
|
use League\Container\Exception\ContainerException; |
|
8
|
|
|
|
|
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
|
21 |
|
*/ |
|
26
|
|
|
public function add($provider): ServiceProviderAggregateInterface |
|
27
|
21 |
|
{ |
|
28
|
3 |
|
if (is_string($provider) && $this->getContainer()->has($provider)) { |
|
29
|
2 |
|
$provider = $this->getContainer()->get($provider); |
|
30
|
|
|
} elseif (is_string($provider) && class_exists($provider)) { |
|
31
|
21 |
|
$provider = new $provider; |
|
32
|
18 |
|
} |
|
33
|
12 |
|
|
|
34
|
|
|
if ($provider instanceof ContainerAwareInterface) { |
|
35
|
21 |
|
$provider->setContainer($this->getContainer()); |
|
36
|
9 |
|
} |
|
37
|
6 |
|
|
|
38
|
|
|
if ($provider instanceof BootableServiceProviderInterface) { |
|
39
|
21 |
|
$provider->boot(); |
|
40
|
18 |
|
} |
|
41
|
18 |
|
|
|
42
|
12 |
|
if ($provider instanceof ServiceProviderInterface) { |
|
43
|
|
|
$this->providers[] = $provider; |
|
44
|
18 |
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
3 |
|
|
|
48
|
|
|
throw new ContainerException( |
|
49
|
1 |
|
'A service provider must be a fully qualified class name or instance ' . |
|
50
|
2 |
|
'of (\League\Container\ServiceProvider\ServiceProviderInterface)' |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
33 |
|
*/ |
|
57
|
|
|
public function provides(string $service): bool |
|
58
|
33 |
|
{ |
|
59
|
|
|
foreach ($this->getIterator() as $provider) { |
|
60
|
|
|
if ($provider->provides($service)) { |
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
18 |
|
|
|
65
|
|
|
return false; |
|
66
|
18 |
|
} |
|
67
|
3 |
|
|
|
68
|
3 |
|
/** |
|
69
|
2 |
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getIterator(): Generator |
|
72
|
15 |
|
{ |
|
73
|
15 |
|
$count = count($this->providers); |
|
74
|
|
|
|
|
75
|
15 |
|
for ($i = 0; $i < $count; $i++) { |
|
76
|
3 |
|
yield $this->providers[$i]; |
|
77
|
2 |
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
15 |
|
/** |
|
81
|
6 |
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function register(string $service) |
|
84
|
15 |
|
{ |
|
85
|
|
|
if (! $this->provides($service)) { |
|
86
|
15 |
|
throw new ContainerException( |
|
87
|
15 |
|
sprintf('(%s) is not provided by a service provider', $service) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
foreach ($this->getIterator() as $provider) { |
|
92
|
|
|
if (in_array($provider->getSignature(), $this->registered)) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$provider->register(); |
|
97
|
|
|
|
|
98
|
|
|
$this->registered[] = $provider->getSignature(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|