|
7 | 7 | */ |
8 | 8 | namespace PMA\libraries\di; |
9 | 9 |
|
| 10 | +use Psr\Container\ContainerInterface; |
| 11 | + |
10 | 12 | /** |
11 | 13 | * Class Container |
12 | 14 | * |
13 | 15 | * @package PMA\libraries\di |
14 | 16 | */ |
15 | | -class Container |
| 17 | +class Container implements ContainerInterface |
16 | 18 | { |
17 | | - |
18 | 19 | /** |
19 | 20 | * @var Item[] $content |
20 | 21 | */ |
@@ -46,19 +47,40 @@ public function __construct(Container $base = null) |
46 | 47 | * @param string $name Name |
47 | 48 | * @param array $params Parameters |
48 | 49 | * |
| 50 | + * @throws NotFoundException No entry was found for **this** identifier. |
| 51 | + * @throws ContainerException Error while retrieving the entry. |
| 52 | + * |
49 | 53 | * @return mixed |
50 | 54 | */ |
51 | 55 | public function get($name, $params = array()) |
52 | 56 | { |
53 | | - if (isset($this->content[$name])) { |
54 | | - return $this->content[$name]->get($params); |
| 57 | + if (!$this->has($name)) { |
| 58 | + throw new NotFoundException("No entry was found for $name identifier."); |
55 | 59 | } |
56 | 60 |
|
57 | | - if (isset($GLOBALS[$name])) { |
| 61 | + if (isset($this->content[$name])) { |
| 62 | + return $this->content[$name]->get($params); |
| 63 | + } else if (isset($GLOBALS[$name])) { |
58 | 64 | return $GLOBALS[$name]; |
| 65 | + } else { |
| 66 | + throw new ContainerException("Error while retrieving the entry."); |
59 | 67 | } |
| 68 | + } |
60 | 69 |
|
61 | | - return null; |
| 70 | + /** |
| 71 | + * Returns true if the container can return an entry for the given identifier. |
| 72 | + * Returns false otherwise. |
| 73 | + * |
| 74 | + * `has($name)` returning true does not mean that `get($name)` will not throw an exception. |
| 75 | + * It does however mean that `get($name)` will not throw a `NotFoundException`. |
| 76 | + * |
| 77 | + * @param string $name Identifier of the entry to look for. |
| 78 | + * |
| 79 | + * @return bool |
| 80 | + */ |
| 81 | + public function has($name) |
| 82 | + { |
| 83 | + return isset($this->content[$name]) || isset($GLOBALS[$name]); |
62 | 84 | } |
63 | 85 |
|
64 | 86 | /** |
|
0 commit comments