1 | <?php |
||
22 | trait SaturateTrait |
||
23 | { |
||
24 | /** |
||
25 | * Must be used only to resolve optional constructor arguments. Use in classes which are |
||
26 | * generally resolved using Container. Default value MUST always be supplied from outside. |
||
27 | * |
||
28 | * @param mixed $default Default value. |
||
29 | * @param string $class Requested class. |
||
30 | * |
||
31 | * @return mixed|null|object |
||
32 | * |
||
33 | * @throws ScopeException |
||
34 | */ |
||
35 | 5 | private function saturate($default, $class) |
|
|
|||
36 | { |
||
37 | 5 | if (!empty($default)) { |
|
38 | 2 | return $default; |
|
39 | } |
||
40 | |||
41 | 3 | $container = $this->iocContainer(); |
|
42 | |||
43 | 3 | if (empty($container)) { |
|
44 | 1 | throw new ScopeException("Unable to saturate '{$class}': no container available"); |
|
45 | } |
||
46 | |||
47 | //Only when global container is set |
||
48 | try { |
||
49 | 2 | return $container->get($class); |
|
50 | 1 | } catch (ContainerException $e) { |
|
51 | 1 | throw new ScopeException("Unable to saturate '{$class}': {$e->getMessage()}", 0, $e); |
|
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Class specific container. |
||
57 | * |
||
58 | * @return ContainerInterface |
||
59 | */ |
||
60 | abstract protected function iocContainer(); |
||
61 | } |
||
62 |