-
Notifications
You must be signed in to change notification settings - Fork 3
/
CompositeCachingServiceProvider.php
149 lines (128 loc) · 4.07 KB
/
CompositeCachingServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
/**
* A service provider that aggregates service definitions from other providers.
*/
class CompositeCachingServiceProvider implements ServiceProviderInterface
{
/**
* @var iterable<ServiceProviderInterface>
*/
protected $providers;
/**
* @var ?iterable<callable>
*/
protected $factories;
/**
* @var ?iterable<callable>
*/
protected $extensions;
/**
* @param iterable<ServiceProviderInterface> $providers
*/
public function __construct(iterable $providers)
{
$this->providers = $providers;
$this->factories = null;
$this->extensions = null;
}
/**
* @inheritDoc
*
* @psalm-suppress InvalidNullableReturnType
* It isn't actually going to return null ever, because $factories will be filled during indexing.
*/
public function getFactories()
{
if (!is_array($this->factories)) {
$this->indexProviderDefinitions($this->providers);
}
/**
* @psalm-suppress NullableReturnStatement
* Not going to be null because will be populated by indexing
*/
return $this->factories;
}
/**
* @inheritDoc
*
* @psalm-suppress InvalidNullableReturnType
* It isn't actually going to return null ever, because $factories will be filled during indexing.
*/
public function getExtensions()
{
if (!is_array($this->extensions)) {
$this->indexProviderDefinitions($this->providers);
}
/**
* @psalm-suppress NullableReturnStatement
* Not going to be null because will be populated by indexing
*/
return $this->extensions;
}
/**
* Indexes definitions in the specified service providers.
*
* Caches them internally.
*
* @param iterable<ServiceProviderInterface> $providers The providers to index.
*/
protected function indexProviderDefinitions(iterable $providers): void
{
$factories = [];
$extensions = [];
foreach ($providers as $provider) {
$factories = $this->mergeFactories($factories, $provider->getFactories());
$extensions = $this->mergeExtensions($extensions, $provider->getExtensions());
}
$this->factories = $factories;
$this->extensions = $extensions;
}
/**
* Merges two maps of factories.
*
* @param callable[] $defaults The factory map to merge into.
* @param callable[] $definitions The factory map to merge. Values from here will override defaults.
*
* @return callable[] The merged factories.
*/
protected function mergeFactories(array $defaults, array $definitions): array
{
return array_merge($defaults, $definitions);
}
/**
* Merged service extensions.
*
* @param callable[] $defaults
* @param iterable<callable> $extensions
*
* @return callable[] The merged extensions.
*/
protected function mergeExtensions(array $defaults, iterable $extensions): array
{
$merged = [];
foreach ($extensions as $key => $extension) {
if (isset($defaults[$key])) {
$default = $defaults[$key];
/**
* @psalm-suppress MissingClosureReturnType
* @psalm-suppress MissingClosureParamType
* Unable to specify mixed before PHP 8.
*/
$merged[$key] = function (PsrContainerInterface $c, $previous = null) use ($default, $extension) {
$result = $default($c, $previous);
$result = $extension($c, $result);
return $result;
};
unset($defaults[$key]);
} else {
$merged[$key] = $extension;
}
}
$merged = $this->mergeFactories($defaults, $merged);
return $merged;
}
}