1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2011, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Aop\Framework; |
12
|
|
|
|
13
|
|
|
use Go\Aop\Intercept\ConstructorInvocation; |
14
|
|
|
use Go\Aop\Intercept\Interceptor; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
use ReflectionMethod; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Reflection constructor invocation implementation |
20
|
|
|
*/ |
21
|
|
|
class ReflectionConstructorInvocation extends AbstractInvocation implements ConstructorInvocation |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Reflection class |
25
|
|
|
* |
26
|
|
|
* @var ReflectionClass |
27
|
|
|
*/ |
28
|
|
|
protected $class = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instance of created class, can be used for Around or After types of advices |
32
|
|
|
* |
33
|
|
|
* @var object|null |
34
|
|
|
*/ |
35
|
|
|
protected $instance = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Instance of reflection constructor for class |
39
|
|
|
* |
40
|
|
|
* @var null|ReflectionMethod |
41
|
|
|
*/ |
42
|
|
|
private $constructor = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Number of constructor arguments |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $constructorArguments = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor for constructor invocation :) |
53
|
|
|
* |
54
|
|
|
* @param string $className Class name |
55
|
|
|
* @param $advices array List of advices for this invocation |
56
|
|
|
* @param string $type |
57
|
|
|
*/ |
58
|
|
|
public function __construct($className, $type, array $advices) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->class = new ReflectionClass($className); |
61
|
|
|
$this->constructor = $constructor = $this->class->getConstructor(); |
62
|
|
|
|
63
|
|
|
// Give an access to call protected constructor |
64
|
|
|
if ($constructor && $constructor->isProtected()) { |
65
|
|
|
$constructor->setAccessible(true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($constructor) { |
69
|
|
|
$this->constructorArguments = $constructor->getNumberOfParameters(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
parent::__construct($advices); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Proceed to the next interceptor in the Chain |
77
|
|
|
* |
78
|
|
|
* Typically this method is called inside previous closure, as instance of Joinpoint is passed to callback |
79
|
|
|
* Do not call this method directly, only inside callback closures. |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
final public function proceed() |
84
|
|
|
{ |
85
|
|
|
if (isset($this->advices[$this->current])) { |
86
|
|
|
/** @var $currentInterceptor Interceptor */ |
87
|
|
|
$currentInterceptor = $this->advices[$this->current]; |
88
|
|
|
$this->current++; |
89
|
|
|
|
90
|
|
|
return $currentInterceptor->invoke($this); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!$this->constructorArguments) { |
94
|
|
|
$this->instance = $this->class->newInstance(); |
95
|
|
|
} else { |
96
|
|
|
$this->instance = $this->class->newInstanceArgs($this->arguments); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->instance; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the constructor being called. |
104
|
|
|
* |
105
|
|
|
* @return ReflectionMethod|null the constructor being called or null if it is absent. |
106
|
|
|
*/ |
107
|
|
|
public function getConstructor() |
108
|
|
|
{ |
109
|
|
|
return $this->constructor; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the object that holds the current joinpoint's static |
114
|
|
|
* part. |
115
|
|
|
* |
116
|
|
|
* @return object|null the object (can be null if the accessible object is |
117
|
|
|
* static). |
118
|
|
|
*/ |
119
|
|
|
public function getThis() |
120
|
|
|
{ |
121
|
|
|
return $this->instance; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the static part of this joinpoint. |
126
|
|
|
* |
127
|
|
|
* @return null|ReflectionMethod |
128
|
|
|
*/ |
129
|
|
|
public function getStaticPart() |
130
|
|
|
{ |
131
|
|
|
return $this->getConstructor(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Invokes current constructor invocation with all interceptors |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
|
|
final public function __invoke(array $arguments = []) |
140
|
|
|
{ |
141
|
|
|
$this->current = 0; |
142
|
|
|
$this->arguments = $arguments; |
143
|
|
|
|
144
|
|
|
return $this->proceed(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns a friendly description of current joinpoint |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
final public function __toString() |
153
|
|
|
{ |
154
|
|
|
return sprintf( |
155
|
|
|
"initialization(%s)", |
156
|
|
|
$this->class->name |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.