| Total Complexity | 7 |
| Total Lines | 69 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | class Containers implements ContainersInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $_containers = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param string $name |
||
| 16 | * @return mixed |
||
| 17 | */ |
||
| 18 | private function getContainer(string $name) |
||
| 19 | { |
||
| 20 | return $this->_containers[$name]; |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $name |
||
| 25 | * @param object $object |
||
| 26 | * @return ContainersInterface |
||
| 27 | */ |
||
| 28 | private function setContainer(string $name, $object): ContainersInterface |
||
| 29 | { |
||
| 30 | $this->_containers[$name] = $object; |
||
| 31 | return $this; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * PSR-11 set container |
||
| 36 | * |
||
| 37 | * @param string $container |
||
| 38 | * @param string|object $object |
||
| 39 | * @param ConfigInterface $config |
||
| 40 | * @return ContainersInterface |
||
| 41 | */ |
||
| 42 | public function set(string $container, $object, ConfigInterface $config = null): ContainersInterface |
||
| 43 | { |
||
| 44 | if (\is_object($object)) { |
||
| 45 | $this->setContainer($container, $object); |
||
| 46 | } else { |
||
| 47 | $class = '\\DrMVC\\' . $object; |
||
| 48 | $this->setContainer($container, new $class($config)); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get container by name |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | * @return mixed |
||
| 59 | */ |
||
| 60 | public function get($name) |
||
| 61 | { |
||
| 62 | // TODO: NotFoundExceptionInterface |
||
| 63 | return $this->has($name) ? $this->getContainer($name) : null; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Container is exist |
||
| 68 | * |
||
| 69 | * @param string $name |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function has($name): bool |
||
| 76 | } |
||
| 77 | } |
||
| 78 |