1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Di |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Di\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Di\Message\Message; |
18
|
|
|
use Phossa2\Di\Resolver\ObjectResolver; |
19
|
|
|
use Phossa2\Di\Exception\LogicException; |
20
|
|
|
use Phossa2\Di\Interfaces\ScopeInterface; |
21
|
|
|
|
22
|
|
|
trait ContainerTrait |
23
|
|
|
{ |
24
|
|
|
use ScopeTrait, FactoryAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* instances pool |
28
|
|
|
* |
29
|
|
|
* @var object[] |
30
|
|
|
* @access protected |
31
|
|
|
*/ |
32
|
|
|
protected $pool = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* for loop detection |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $loop = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected $counter = 0; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* execute init methods defined in 'di.init' node |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
* @throws RuntimeException if anything goes wrong |
53
|
|
|
* @access protected |
54
|
|
|
*/ |
55
|
|
|
protected function initContainer() |
56
|
|
|
{ |
57
|
|
|
if ($this->getResolver()->hasInSection('', 'init')) { |
58
|
|
|
$this->getFactory()->executeMethodBatch( |
59
|
|
|
$this->getResolver()->getInSection('', 'init') |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the instance either from the pool or create it |
67
|
|
|
* |
68
|
|
|
* @param string $id service id with or without the scope |
69
|
|
|
* @param array $args arguments for the constructor |
70
|
|
|
* @return object |
71
|
|
|
* @throws LogicException if instantiation goes wrong |
72
|
|
|
* @throws RuntimeException if method execution goes wrong |
73
|
|
|
* @access protected |
74
|
|
|
*/ |
75
|
|
|
protected function getInstance(/*# string */ $id, array $args) |
76
|
|
|
{ |
77
|
|
|
// get id & scope info |
78
|
|
|
list($rawId, $scopedId, $scope) = $this->realScopeInfo($id); |
79
|
|
|
|
80
|
|
|
// get a new instance if args not empty or in single scope |
81
|
|
|
if (!empty($args) || ScopeInterface::SCOPE_SINGLE === $scope) { |
82
|
|
|
return $this->createInstance($rawId, $args); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// if not in the pool, create a new instance |
86
|
|
|
if (!isset($this->pool[$scopedId])) { |
87
|
|
|
$this->pool[$scopedId] = $this->createInstance($rawId); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->pool[$scopedId]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Full scope info with consideration of ancestor instances |
95
|
|
|
* |
96
|
|
|
* @param string $id |
97
|
|
|
* @return array |
98
|
|
|
* @access protected |
99
|
|
|
*/ |
100
|
|
|
protected function realScopeInfo(/*# string */ $id)/*# : array */ |
101
|
|
|
{ |
102
|
|
|
list($rawId, $scope) = $this->scopedInfo($id); |
103
|
|
|
|
104
|
|
|
// special treatment if $scope is a '#service_id' |
105
|
|
|
if (isset($this->loop[$scope])) { |
106
|
|
|
$scope .= '_' . $this->loop[$scope]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return [$rawId, $this->scopedId($rawId, $scope), $scope]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Create the instance with loop detection |
114
|
|
|
* |
115
|
|
|
* Loop: an instance depends on itself in the creation chain. |
116
|
|
|
* |
117
|
|
|
* @param string $rawId |
118
|
|
|
* @param array $args arguments for the constructor if any |
119
|
|
|
* @return object |
120
|
|
|
* @throws LogicException if instantiation goes wrong or loop detected |
121
|
|
|
* @access protected |
122
|
|
|
*/ |
123
|
|
|
protected function createInstance(/*# string */ $rawId, array $args = []) |
124
|
|
|
{ |
125
|
|
|
// conver 'service_id' to '#service_id' |
126
|
|
|
$serviceId = ObjectResolver::getServiceId($rawId); |
127
|
|
|
|
128
|
|
|
if (isset($this->loop[$serviceId])) { |
129
|
|
|
// found self in the chain |
130
|
|
|
throw new LogicException( |
131
|
|
|
Message::get(Message::DI_LOOP_DETECTED, $rawId), |
132
|
|
|
Message::DI_LOOP_DETECTED |
133
|
|
|
); |
134
|
|
|
} else { |
135
|
|
|
$this->loop[$serviceId] = ++$this->counter; |
136
|
|
|
$obj = $this->getFactory()->createInstance($rawId, $args); |
137
|
|
|
unset($this->loop[$serviceId]); |
138
|
|
|
return $obj; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|