forked from Dhii/containers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegatingContainer.php
More file actions
160 lines (135 loc) · 4.15 KB
/
DelegatingContainer.php
File metadata and controls
160 lines (135 loc) · 4.15 KB
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
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Collection\ContainerInterface;
use Dhii\Container\Exception\ContainerException;
use Dhii\Container\Exception\NotFoundException;
use Dhii\Container\Util\StringTranslatingTrait;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use UnexpectedValueException;
class DelegatingContainer implements ContainerInterface
{
use StringTranslatingTrait;
/**
* @var ServiceProviderInterface
*/
protected $provider;
/**
* @var PsrContainerInterface|null
*/
protected $parent;
/**
*/
public function __construct(ServiceProviderInterface $provider, PsrContainerInterface $parent = null)
{
$this->provider = $provider;
$this->parent = $parent;
}
/**
* {@inheritDoc}
*/
public function get($id)
{
$provider = $this->provider;
$services = $provider->getFactories();
if (!array_key_exists($id, $services)) {
throw new NotFoundException(
$this->__('Service not found for key "%1$s"', [$id]),
0,
null
);
}
$service = $services[$id];
try {
$service = $this->invokeFactory($service);
} catch (UnexpectedValueException $e) {
throw new ContainerException(
$this->__('Could not create service "%1$s"', [$id]),
0,
$e
);
}
$extensions = $provider->getExtensions();
if (!array_key_exists($id, $extensions)) {
return $service;
}
$extension = $extensions[$id];
try {
$service = $this->invokeExtension($extension, $service);
} catch (UnexpectedValueException $e) {
throw new ContainerException(
$this->__('Could not extend service "%1$s"', [$id]),
0,
$e
);
}
return $service;
}
/**
* {@inheritDoc}
*/
public function has($id)
{
$services = $this->provider->getFactories();
return array_key_exists($id, $services);
}
/**
* Retrieves a service by invoking its factory.
*
* @param callable $factory The factory to invoke.
*
* @return mixed The service created by the factory.
*
* @throws UnexpectedValueException If factory could not be invoked.
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*/
protected function invokeFactory(callable $factory)
{
if (!is_callable($factory)) {
throw new UnexpectedValueException(
$this->__('Factory could not be invoked'),
0,
null
);
}
$baseContainer = $this->getBaseContainer();
$service = $factory($baseContainer);
return $service;
}
/**
* Extends the service by invoking the extension with it.
*
* @param callable $extension The extension to invoke.
* @param mixed $service The service to extend.
*
* @return mixed The extended service.
*
* @throws UnexpectedValueException If extension cannot be invoked.
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*/
protected function invokeExtension(callable $extension, $service)
{
if (!is_callable($extension)) {
throw new UnexpectedValueException(
$this->__('Factory could not be invoked'),
0,
null
);
}
$baseContainer = $this->getBaseContainer();
$service = $extension($baseContainer, $service);
return $service;
}
/**
* Retrieves the container to be used for definitions and extensions.
*
* @return PsrContainerInterface The parent container, if set. Otherwise, this instance.
*/
protected function getBaseContainer(): PsrContainerInterface
{
return $this->parent instanceof PsrContainerInterface
? $this->parent
: $this;
}
}