-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.php
166 lines (147 loc) · 4.09 KB
/
Container.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Rad\DependencyInjection;
use ArrayAccess;
use Rad\Core\SingletonTrait;
use Rad\DependencyInjection\Exception\ServiceLockedException;
use Rad\DependencyInjection\Exception\ServiceNotFoundException;
/**
* Container
*
* @package Rad\DependencyInjection
*/
class Container implements ArrayAccess
{
use SingletonTrait;
/**
* @var Service[]
*/
protected static $services = [];
/**
* Set service
*
* @param string $name
* @param callable|object|string $definition
* @param bool $shared
* @param bool $locked
*
* @throws Exception
*/
public static function set($name, $definition, $shared = false, $locked = false)
{
if (isset(self::$services[$name]) && self::$services[$name]->isLocked()) {
throw new ServiceLockedException(sprintf('Service "%s" is locked.', $name));
}
self::$services[$name] = new Service($name, $definition, $shared, $locked);
}
/**
* Set service as shared
*
* @param string $name
* @param callable|object|string $definition
* @param bool $locked
*
* @throws Exception
*/
public static function setShared($name, $definition, $locked = false)
{
if (isset(self::$services[$name]) && self::$services[$name]->isLocked()) {
throw new ServiceLockedException(sprintf('Service "%s" is locked.', $name));
}
self::$services[$name] = new Service($name, $definition, true, $locked);
}
/**
* Get and resolve service
*
* @param string $name
* @param array $args
*
* @return mixed|object
* @throws Exception
*/
public static function get($name, array $args = [])
{
if (!isset(self::$services[$name])) {
throw new ServiceNotFoundException(sprintf('Service "%s" does not exist.', $name));
}
$instance = self::$services[$name]->resolve(self::getInstance(), $args);
if ($instance instanceof ContainerAwareInterface) {
$instance->setContainer(self::getInstance());
}
return $instance;
}
/**
* Check service is exist
*
* @param string $name
*
* @return bool
*/
public static function has($name)
{
return isset(self::$services[$name]);
}
/**
* Remove service
*
* @param string $name
*
* @throws Exception
*/
public static function remove($name)
{
if (isset(self::$services[$name]) && self::$services[$name]->isLocked()) {
throw new Exception(sprintf('You can not remove locked service.', $name));
}
unset(self::$services[$name]);
}
/**
* Whether a offset exists
*
* @param string $offset An offset to check for.
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @return boolean true on success or false on failure.
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* Offset to retrieve
*
* @param mixed $offset The offset to retrieve.
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Offset to set
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @return void
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Offset to unset
*
* @param mixed $offset The offset to unset.
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @return void
*/
public function offsetUnset($offset)
{
$this->remove($offset);
}
}