|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\Symfony\Facades; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ServiceLocator; |
|
7
|
|
|
|
|
8
|
|
|
final class Container |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Container |
|
12
|
|
|
*/ |
|
13
|
|
|
private static $instance = null; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ContainerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $container = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param ContainerInterface $container |
|
22
|
|
|
*/ |
|
23
|
|
|
private function __construct(ContainerInterface $container) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->container = $container; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get a service using the container or the locator. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $serviceId |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private function getService(string $serviceId) |
|
36
|
|
|
{ |
|
37
|
|
|
if($this->container->has($serviceId)) |
|
38
|
|
|
{ |
|
39
|
|
|
// A public service will be found in the container. |
|
40
|
|
|
return $this->container->get($serviceId); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ServiceLocator |
|
45
|
|
|
*/ |
|
46
|
|
|
$locator = $this->container->get('lagdo.facades.service_locator', |
|
47
|
|
|
ContainerInterface::NULL_ON_INVALID_REFERENCE); |
|
48
|
|
|
// If not found in the container, then look in the service locator. |
|
49
|
|
|
if($locator !== null && $locator->has($serviceId)) |
|
50
|
|
|
{ |
|
51
|
|
|
return $locator->get($serviceId); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param ContainerInterface $container |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function createInstance(ContainerInterface $container) |
|
61
|
|
|
{ |
|
62
|
|
|
self::$instance = new Container($container); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get a service using the container or the locator. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $serviceId |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed|null |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getFacadeService(string $serviceId) |
|
73
|
|
|
{ |
|
74
|
|
|
return self::$instance->getService($serviceId); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|