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->services[self::SERVICE_CONTAINER] = $c = $this; |
33
|
|
|
$this->type(self::SERVICE_CONTAINER, \array_keys(\class_implements($c) + \class_parents($c) + [static::class => static::class])); |
34
|
|
|
} |
35
|
|
|
$this->resolver = new Resolver($s ?? $this); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets a new service to a unique identifier. |
40
|
|
|
* |
41
|
|
|
* @param string $offset The unique identifier for the parameter or object |
42
|
|
|
* @param mixed $value The value of the service assign to the $offset |
43
|
|
|
* |
44
|
|
|
* @throws FrozenServiceException Prevent override of a frozen service |
45
|
|
|
*/ |
46
|
|
|
public function offsetSet($offset, $value): void |
47
|
|
|
{ |
48
|
|
|
$this->autowire($offset, $value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets a registered service definition. |
53
|
|
|
* |
54
|
|
|
* @param string $offset The unique identifier for the service |
55
|
|
|
* |
56
|
|
|
* @throws NotFoundServiceException If the identifier is not defined |
57
|
106 |
|
* |
58
|
|
|
* @return mixed The value of the service |
59
|
106 |
|
*/ |
60
|
106 |
|
#[\ReturnTypeWillChange] |
61
|
106 |
|
public function offsetGet($offset) |
62
|
|
|
{ |
63
|
|
|
return $this->get($offset); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks if a service is set. |
68
|
|
|
* |
69
|
|
|
* @param string $offset The unique identifier for the service |
70
|
|
|
*/ |
71
|
63 |
|
public function offsetExists($offset): bool |
72
|
|
|
{ |
73
|
63 |
|
return $this->has($offset); |
74
|
63 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Unset a service by given offset. |
78
|
|
|
* |
79
|
|
|
* @param string $offset The unique identifier for service definition |
80
|
|
|
*/ |
81
|
|
|
public function offsetUnset($offset): void |
82
|
|
|
{ |
83
|
|
|
$this->removeDefinition($offset); |
84
|
|
|
} |
85
|
55 |
|
|
86
|
|
|
/** |
87
|
55 |
|
* {@inheritdoc} |
88
|
|
|
* |
89
|
|
|
* @throws FrozenServiceException if definition has been initialized |
90
|
|
|
*/ |
91
|
|
|
public function definition(string $id) |
92
|
|
|
{ |
93
|
|
|
if (\array_key_exists($id, $this->privates)) { |
94
|
|
|
throw new FrozenServiceException(\sprintf('The "%s" internal service is meant to be private and out of reach.', $id)); |
95
|
7 |
|
} |
96
|
|
|
|
97
|
7 |
|
return parent::definition($id); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
* |
103
|
|
|
* @throws \ReflectionException |
104
|
|
|
*/ |
105
|
6 |
|
protected function doCreate(string $id, object $definition, int $invalidBehavior) |
106
|
|
|
{ |
107
|
6 |
|
if ($definition instanceof Definitions\DefinitionInterface) { |
108
|
6 |
|
if ($definition instanceof Definitions\ShareableDefinitionInterface) { |
109
|
|
|
if ($definition->isAbstract()) { |
110
|
|
|
throw new ContainerResolutionException(\sprintf('Resolving an abstract definition %s is not allowed.', $id)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!$definition->isPublic()) { |
114
|
|
|
$this->removeDefinition($id); |
115
|
|
|
} |
116
|
17 |
|
|
117
|
|
|
if ($definition->isShared()) { |
118
|
17 |
|
$s = &$this->services[$id] ?? null; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
$s = $definition->build($id, $this->resolver); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->definitions[$id] = ($this->services[$id] ??= $s ?? $definition); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|