1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI; |
19
|
|
|
|
20
|
|
|
use Rade\DI\Exceptions\{ContainerResolutionException, FrozenServiceException, NotFoundServiceException}; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dependency injection container. |
24
|
|
|
* |
25
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Container extends AbstractContainer implements \ArrayAccess |
28
|
|
|
{ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
if (empty($this->types)) { |
32
|
|
|
$this->type( |
33
|
|
|
self::SERVICE_CONTAINER, |
34
|
|
|
\array_keys(\class_implements($c = static::class) + \class_parents($c) + [$c => $c]) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
$this->resolver = new Resolver($this->services[self::SERVICE_CONTAINER] = $this); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets a new service to a unique identifier. |
42
|
|
|
* |
43
|
|
|
* @param string $offset The unique identifier for the parameter or object |
44
|
|
|
* @param mixed $value The value of the service assign to the $offset |
45
|
|
|
* |
46
|
|
|
* @throws FrozenServiceException Prevent override of a frozen service |
47
|
|
|
*/ |
48
|
|
|
public function offsetSet($offset, $value): void |
49
|
|
|
{ |
50
|
|
|
$this->autowire($offset, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets a registered service definition. |
55
|
|
|
* |
56
|
|
|
* @param string $offset The unique identifier for the service |
57
|
106 |
|
* |
58
|
|
|
* @throws NotFoundServiceException If the identifier is not defined |
59
|
106 |
|
* |
60
|
106 |
|
* @return mixed The value of the service |
61
|
106 |
|
*/ |
62
|
|
|
#[\ReturnTypeWillChange] |
63
|
|
|
public function offsetGet($offset) |
64
|
|
|
{ |
65
|
|
|
return $this->get($offset); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if a service is set. |
70
|
|
|
* |
71
|
63 |
|
* @param string $offset The unique identifier for the service |
72
|
|
|
*/ |
73
|
63 |
|
public function offsetExists($offset): bool |
74
|
63 |
|
{ |
75
|
|
|
return $this->has($offset); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Unset a service by given offset. |
80
|
|
|
* |
81
|
|
|
* @param string $offset The unique identifier for service definition |
82
|
|
|
*/ |
83
|
|
|
public function offsetUnset($offset): void |
84
|
|
|
{ |
85
|
55 |
|
$this->removeDefinition($offset); |
86
|
|
|
} |
87
|
55 |
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
* |
91
|
|
|
* @throws FrozenServiceException if definition has been initialized |
92
|
|
|
*/ |
93
|
|
|
public function definition(string $id) |
94
|
|
|
{ |
95
|
7 |
|
if (\array_key_exists($id, $this->privates) || isset($this->methodsMap[$id])) { |
96
|
|
|
throw new FrozenServiceException(\sprintf('The "%s" internal service is meant to be private and out of reach.', $id)); |
97
|
7 |
|
} |
98
|
|
|
|
99
|
|
|
return parent::definition($id); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
* |
105
|
6 |
|
* @throws \ReflectionException |
106
|
|
|
*/ |
107
|
6 |
|
protected function doCreate(string $id, object $definition, int $invalidBehavior) |
108
|
6 |
|
{ |
109
|
|
|
if ($definition instanceof Definitions\DefinitionInterface) { |
110
|
|
|
if ($definition instanceof Definitions\ShareableDefinitionInterface) { |
111
|
|
|
if ($definition->isAbstract()) { |
112
|
|
|
throw new ContainerResolutionException(\sprintf('Resolving an abstract definition %s is not allowed.', $id)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!$definition->isPublic()) { |
116
|
17 |
|
$this->removeDefinition($id); |
117
|
|
|
} |
118
|
17 |
|
|
119
|
|
|
if ($definition->isShared()) { |
120
|
|
|
$s = &$this->services[$id] ?? null; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
$s = $definition->build($id, $this->resolver); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->definitions[$id] = ($this->services[$id] ??= $s ?? $definition); |
127
|
11 |
|
} |
128
|
|
|
} |
129
|
|
|
|