1 | <?php |
||
14 | class ServiceLocator implements ServiceLocatorInterface { |
||
15 | |||
16 | /** |
||
17 | * Holds the service locator instance |
||
18 | * @var ServiceLocator |
||
19 | */ |
||
20 | private static $instance = null; |
||
21 | |||
22 | /** |
||
23 | * Holds the requested services |
||
24 | * @var array |
||
25 | */ |
||
26 | private static $services = []; |
||
27 | |||
28 | /** @var array */ |
||
29 | private static $backup = []; |
||
30 | |||
31 | /** |
||
32 | * ServiceLocator private constructor. |
||
33 | */ |
||
34 | private function __construct() {} |
||
35 | |||
36 | /** |
||
37 | * Return the instance of the service locator |
||
38 | * @return ServiceLocator |
||
39 | */ |
||
40 | public static function instance() |
||
48 | |||
49 | /** |
||
50 | * Try to get the service. |
||
51 | * Returns per default always the same instance of the service/factory. |
||
52 | * |
||
53 | * @param string $service |
||
54 | * @param bool $shared |
||
55 | * @return ServiceInterface|FactoryInterface |
||
56 | */ |
||
57 | public function get(string $service = '', bool $shared = true) |
||
82 | |||
83 | /** |
||
84 | * Get specific service by class name |
||
85 | * |
||
86 | * @param string $service |
||
87 | * @return mixed |
||
88 | * @throws ServiceNotFoundException |
||
89 | */ |
||
90 | private function _getService(string $service) |
||
98 | |||
99 | /** |
||
100 | * Check if we have a factory for this service |
||
101 | * |
||
102 | * @param string $service |
||
103 | * @return FactoryInterface|null |
||
104 | * @throws FactoryMayIncompatibleException |
||
105 | */ |
||
106 | private function _getFactory(string $service) |
||
126 | |||
127 | /** |
||
128 | * @param string $name |
||
129 | * @param ServiceInterface $service |
||
130 | * @internal |
||
131 | */ |
||
132 | public function set($name, $service) |
||
140 | |||
141 | /** |
||
142 | * Reset the service locators instance |
||
143 | * |
||
144 | * @internal |
||
145 | */ |
||
146 | public static function destroy() |
||
150 | |||
151 | } |