| Conditions | 7 |
| Paths | 6 |
| Total Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 24 | public function get($id) |
||
| 25 | { |
||
| 26 | if (!$this->has($id)) { |
||
| 27 | throw new NotFoundException("Couldn't find {$id}"); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (is_callable($this->container_stack[$id])) { |
||
| 31 | // Performance optimization, isset returns false on null values, but is faster |
||
| 32 | // https://stackoverflow.com/a/9522522/10604655 |
||
| 33 | if (!isset($this->instances[$id]) && !array_key_exists($id, $this->instances)) { |
||
| 34 | |||
| 35 | if (isset($this->call_stack[$id])) { |
||
| 36 | throw new ContainerException("Circular dependency detected when calling {$id}"); |
||
| 37 | } |
||
| 38 | |||
| 39 | $this->call_stack[$id] = true; |
||
| 40 | |||
| 41 | try { |
||
| 42 | $this->instances[$id] = call_user_func($this->container_stack[$id], $this); |
||
| 43 | } catch (Exception $e) { |
||
| 44 | throw new ContainerException($e->getMessage() . " - Tried to call {$id}"); |
||
| 45 | } |
||
| 46 | |||
| 47 | $this->call_stack = []; |
||
| 48 | } |
||
| 49 | |||
| 50 | return $this->instances[$id]; |
||
| 51 | } |
||
| 52 | |||
| 53 | return $this->container_stack[$id]; |
||
| 54 | } |
||
| 55 | |||
| 61 |