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
|
|
|
/** |
23
|
|
|
* InstanceFactoryTrait |
24
|
|
|
* |
25
|
|
|
* Manufacturing instances for container |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Di |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
trait InstanceFactoryTrait |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* instances pool |
36
|
|
|
* |
37
|
|
|
* @var object[] |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $pool = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* for loop detection |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected $loop = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
* @access protected |
53
|
|
|
*/ |
54
|
|
|
protected $counter = 0; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the instance either from the pool or create it |
58
|
|
|
* |
59
|
|
|
* @param string $id service id with or without the scope |
60
|
|
|
* @param array $args arguments for the constructor |
61
|
|
|
* @return object |
62
|
|
|
* @throws LogicException if instantiation goes wrong |
63
|
|
|
* @throws RuntimeException if method execution goes wrong |
64
|
|
|
* @access protected |
65
|
|
|
*/ |
66
|
|
|
protected function getInstance(/*# string */ $id, array $args) |
67
|
|
|
{ |
68
|
|
|
// get id & scope info |
69
|
|
|
list($rawId, $scopedId, $scope) = $this->realScopeInfo($id); |
70
|
|
|
|
71
|
|
|
// get from the pool |
72
|
|
|
if (isset($this->pool[$scopedId])) { |
73
|
|
|
return $this->pool[$scopedId]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// create instance |
77
|
|
|
$instance = $this->createInstance($rawId, $args); |
78
|
|
|
|
79
|
|
|
// save in the pool |
80
|
|
|
if (empty($args) && ScopeInterface::SCOPE_SINGLE !== $scope) { |
81
|
|
|
$this->pool[$scopedId] = $instance; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Full scope info with consideration of ancestor instances |
89
|
|
|
* |
90
|
|
|
* @param string $id |
91
|
|
|
* @return array |
92
|
|
|
* @access protected |
93
|
|
|
*/ |
94
|
|
|
protected function realScopeInfo(/*# string */ $id)/*# : array */ |
95
|
|
|
{ |
96
|
|
|
list($rawId, $scope) = $this->scopedInfo($id); |
|
|
|
|
97
|
|
|
|
98
|
|
|
// special treatment if $scope is a '#service_id' |
99
|
|
|
if (isset($this->loop[$scope])) { |
100
|
|
|
$scope .= '_' . $this->loop[$scope]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return [$rawId, $this->scopedId($rawId, $scope), $scope]; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create the instance with loop detection |
108
|
|
|
* |
109
|
|
|
* Loop: an instance depends on itself in the creation chain. |
110
|
|
|
* |
111
|
|
|
* @param string $rawId |
112
|
|
|
* @param array $args arguments for the constructor if any |
113
|
|
|
* @return object |
114
|
|
|
* @throws LogicException if instantiation goes wrong or loop detected |
115
|
|
|
* @access protected |
116
|
|
|
*/ |
117
|
|
|
protected function createInstance(/*# string */ $rawId, array $args) |
118
|
|
|
{ |
119
|
|
|
// conver 'service_id' to '#service_id' |
120
|
|
|
$serviceId = ObjectResolver::getServiceId($rawId); |
121
|
|
|
|
122
|
|
|
if (isset($this->loop[$serviceId])) { |
123
|
|
|
throw new LogicException( |
124
|
|
|
Message::get(Message::DI_LOOP_DETECTED, $rawId), |
125
|
|
|
Message::DI_LOOP_DETECTED |
126
|
|
|
); |
127
|
|
|
} else { |
128
|
|
|
$this->loop[$serviceId] = ++$this->counter; |
129
|
|
|
$obj = $this->getFactory()->createInstance($rawId, $args); |
|
|
|
|
130
|
|
|
unset($this->loop[$serviceId]); |
131
|
|
|
return $obj; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.