Total Complexity | 7 |
Total Lines | 71 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
10 | class App implements AppInterface |
||
11 | { |
||
12 | private $_containers = []; |
||
13 | |||
14 | public function __construct(ConfigInterface $config) |
||
15 | { |
||
16 | $this->setContainer('config', $config); |
||
17 | } |
||
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 AppInterface |
||
32 | */ |
||
33 | private function setContainer(string $name, $object): AppInterface |
||
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 |
||
44 | * @param ConfigInterface $config |
||
45 | * @return AppInterface |
||
46 | */ |
||
47 | public function set(string $container, string $object, ConfigInterface $config): AppInterface |
||
48 | { |
||
49 | $objectConfig = $config->get($object); |
||
50 | |||
51 | if (null !== $objectConfig) { |
||
52 | $class = '\\DrMVC\\' . $object; |
||
53 | $this->setContainer($container, new $class($config)); |
||
54 | } |
||
55 | |||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get container by name |
||
61 | * |
||
62 | * @param string $name |
||
63 | * @return mixed |
||
64 | */ |
||
65 | public function get($name) |
||
66 | { |
||
67 | // TODO: NotFoundExceptionInterface |
||
68 | return $this->has($name) ?? $this->getContainer($name); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Container is exist |
||
73 | * |
||
74 | * @param string $name |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function has($name): bool |
||
81 | } |
||
82 | } |
||
83 |