|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DrMVC\Framework; |
|
4
|
|
|
|
|
5
|
|
|
use DrMVC\Config\ConfigInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class for work with PSR containers |
|
9
|
|
|
* @package DrMVC\Framework |
|
10
|
|
|
* @since 3.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class Containers implements ContainersInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $_containers = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $name |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
private function getContainer(string $name) |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->_containers[$name]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @param object $object |
|
31
|
|
|
* @return ContainersInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private function setContainer(string $name, $object): ContainersInterface |
|
34
|
|
|
{ |
|
35
|
|
|
$this->_containers[$name] = $object; |
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* PSR-11 set container |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $container |
|
43
|
|
|
* @param string|object $object |
|
44
|
|
|
* @param ConfigInterface $config |
|
45
|
|
|
* @return ContainersInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function set(string $container, $object, ConfigInterface $config = null): ContainersInterface |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$this->has($container)) { |
|
50
|
|
|
if (\is_object($object)) { |
|
51
|
|
|
$this->setContainer($container, $object); |
|
52
|
|
|
} else { |
|
53
|
|
|
$class = '\\DrMVC\\' . $object; |
|
54
|
|
|
$this->setContainer($container, new $class($config)); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get container by name |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get($name) |
|
67
|
|
|
{ |
|
68
|
|
|
// TODO: NotFoundExceptionInterface |
|
69
|
|
|
return $this->has($name) ? $this->getContainer($name) : null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Container is exist |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function has($name): bool |
|
79
|
|
|
{ |
|
80
|
|
|
// TODO: NotFoundExceptionInterface |
|
81
|
|
|
return isset($this->_containers[$name]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|