Container.php
4.4 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\Container class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
use Psr\Container\ContainerInterface;
/**
* Class Container
*
* @package PhpMyAdmin\Di
*/
class Container implements ContainerInterface
{
/**
* @var Item[] $content
*/
protected $content = array();
/**
* @var Container
*/
protected static $defaultContainer;
/**
* Create a dependency injection container
*
* @param Container $base Container
*/
public function __construct(Container $base = null)
{
if (isset($base)) {
$this->content = $base->content;
} else {
$this->alias('container', 'Container');
}
$this->set('Container', $this);
}
/**
* Get an object with given name and parameters
*
* @param string $name Name
* @param array $params Parameters
*
* @throws NotFoundException No entry was found for **this** identifier.
* @throws ContainerException Error while retrieving the entry.
*
* @return mixed
*/
public function get($name, array $params = array())
{
if (!$this->has($name)) {
throw new NotFoundException("No entry was found for $name identifier.");
}
if (isset($this->content[$name])) {
return $this->content[$name]->get($params);
} elseif (isset($GLOBALS[$name])) {
return $GLOBALS[$name];
} else {
throw new ContainerException("Error while retrieving the entry.");
}
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($name)` returning true does not mean that `get($name)` will not throw an exception.
* It does however mean that `get($name)` will not throw a `NotFoundException`.
*
* @param string $name Identifier of the entry to look for.
*
* @return bool
*/
public function has($name)
{
return isset($this->content[$name]) || isset($GLOBALS[$name]);
}
/**
* Remove an object from container
*
* @param string $name Name
*
* @return void
*/
public function remove($name)
{
unset($this->content[$name]);
}
/**
* Rename an object in container
*
* @param string $name Name
* @param string $newName New name
*
* @return void
*/
public function rename($name, $newName)
{
$this->content[$newName] = $this->content[$name];
$this->remove($name);
}
/**
* Set values in the container
*
* @param string|array $name Name
* @param mixed $value Value
*
* @return void
*/
public function set($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->set($key, $val);
}
return;
}
$this->content[$name] = new ValueItem($value);
}
/**
* Register a service in the container
*
* @param string $name Name
* @param mixed $service Service
*
* @return void
*/
public function service($name, $service = null)
{
if (!isset($service)) {
$service = $name;
}
$this->content[$name] = new ServiceItem($this, $service);
}
/**
* Register a factory in the container
*
* @param string $name Name
* @param mixed $factory Factory
*
* @return void
*/
public function factory($name, $factory = null)
{
if (!isset($factory)) {
$factory = $name;
}
$this->content[$name] = new FactoryItem($this, $factory);
}
/**
* Register an alias in the container
*
* @param string $name Name
* @param string $target Target
*
* @return void
*/
public function alias($name, $target)
{
// The target may be not defined yet
$this->content[$name] = new AliasItem($this, $target);
}
/**
* Get the global default container
*
* @return Container
*/
public static function getDefaultContainer()
{
if (!isset(static::$defaultContainer)) {
static::$defaultContainer = new Container();
}
return static::$defaultContainer;
}
}