|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Di |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Di; |
|
13
|
|
|
|
|
14
|
|
|
use Phoole\Config\ConfigInterface; |
|
15
|
|
|
use Phoole\Di\Util\ContainerTrait; |
|
16
|
|
|
use Psr\Container\ContainerInterface; |
|
17
|
|
|
use Phoole\Di\Exception\LogicException; |
|
18
|
|
|
use Phoole\Di\Exception\NotFoundException; |
|
19
|
|
|
use Phoole\Base\Reference\ReferenceInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Dependency Injection |
|
23
|
|
|
* |
|
24
|
|
|
* @package Phoole\Di |
|
25
|
|
|
*/ |
|
26
|
|
|
class Container implements ContainerInterface, ReferenceInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use ContainerTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $container; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor |
|
37
|
|
|
* |
|
38
|
|
|
* $config is the Phoole\Config\Config object |
|
39
|
|
|
* $delegator is for lookup delegation. If NULL will use $this |
|
40
|
|
|
* |
|
41
|
|
|
* @param ConfigInterface $config |
|
42
|
|
|
* @param ContainerInterface $delegator |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
ConfigInterface $config, |
|
46
|
|
|
?ContainerInterface $delegator = NULL |
|
47
|
|
|
) { |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
$this->delegator = $delegator ?? $this; |
|
50
|
|
|
|
|
51
|
|
|
$this->setReferencePattern('${#', '}'); |
|
52
|
|
|
$this->reloadAll(); |
|
53
|
|
|
|
|
54
|
|
|
self::$container = $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Access objects from a static way |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* @param array $arguments |
|
62
|
|
|
* @return object |
|
63
|
|
|
* @throws LogicException |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function __callStatic($name, $arguments): object |
|
66
|
|
|
{ |
|
67
|
|
|
$container = self::$container; |
|
68
|
|
|
if (is_null($container)) { |
|
69
|
|
|
throw new LogicException("unInitialized container"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$object = $container->get($name); |
|
73
|
|
|
if (!empty($arguments) && is_callable($object)) { |
|
74
|
|
|
return $object($arguments); |
|
75
|
|
|
} |
|
76
|
|
|
return $object; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* ```php |
|
82
|
|
|
* // get the cache object |
|
83
|
|
|
* $cache = $container->get('cache'); |
|
84
|
|
|
* |
|
85
|
|
|
* // get a NEW cache object |
|
86
|
|
|
* $cacheNew = $container->get('cache@'); |
|
87
|
|
|
* |
|
88
|
|
|
* // get an object shared in SESSION scope |
|
89
|
|
|
* $sessCache = $container->get('cache@SESSION'); |
|
90
|
|
|
* ``` |
|
91
|
|
|
* |
|
92
|
|
|
* {@inheritDoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function get($id): object |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->has($id)) { |
|
97
|
|
|
return $this->getInstance($id); |
|
98
|
|
|
} else { |
|
99
|
|
|
throw new NotFoundException("Service $id not found"); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function has($id): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->hasDefinition($id); |
|
109
|
|
|
} |
|
110
|
|
|
} |