1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2012, 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\Core; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\FileCacheReader; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
use Go\Aop; |
16
|
|
|
use Go\Aop\Pointcut\PointcutLexer; |
17
|
|
|
use Go\Aop\Pointcut\PointcutGrammar; |
18
|
|
|
use Go\Aop\Pointcut\PointcutParser; |
19
|
|
|
use Go\Instrument\RawAnnotationReader; |
20
|
|
|
use Go\Instrument\ClassLoading\CachePathManager; |
21
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Aspect container contains list of all pointcuts and advisors |
25
|
|
|
*/ |
26
|
|
|
class GoAspectContainer extends Container implements AspectContainer |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* List of resources for application |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $resources = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Cached timestamp for resources |
37
|
|
|
* |
38
|
|
|
* @var integer |
39
|
|
|
*/ |
40
|
|
|
protected $maxTimestamp = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor for container |
44
|
|
|
*/ |
45
|
11 |
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
// Register all services in the container |
48
|
|
|
$this->share('aspect.loader', function(Container $container) { |
|
|
|
|
49
|
2 |
|
$aspectLoader = new AspectLoader( |
50
|
|
|
$container, |
|
|
|
|
51
|
2 |
|
$container->get('aspect.annotation.reader') |
52
|
|
|
); |
53
|
2 |
|
$lexer = $container->get('aspect.pointcut.lexer'); |
54
|
2 |
|
$parser = $container->get('aspect.pointcut.parser'); |
55
|
|
|
|
56
|
|
|
// Register general aspect loader extension |
57
|
2 |
|
$aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser)); |
58
|
2 |
|
$aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser)); |
59
|
|
|
|
60
|
2 |
|
return $aspectLoader; |
61
|
11 |
|
}); |
62
|
|
|
|
63
|
|
|
$this->share('aspect.cached.loader', function(Container $container) { |
|
|
|
|
64
|
|
|
$cachedAspectLoader = new CachedAspectLoader( |
65
|
|
|
$container, |
|
|
|
|
66
|
|
|
'aspect.loader', |
67
|
|
|
$container->get('kernel.options') |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $cachedAspectLoader; |
71
|
11 |
|
}); |
72
|
|
|
|
73
|
|
|
$this->share('aspect.advisor.accessor', function(Container $container) { |
|
|
|
|
74
|
|
|
return new LazyAdvisorAccessor( |
75
|
|
|
$container, |
|
|
|
|
76
|
|
|
$container->get('aspect.cached.loader') |
77
|
|
|
); |
78
|
11 |
|
}); |
79
|
|
|
|
80
|
|
|
$this->share('aspect.advice_matcher', function(Container $container) { |
|
|
|
|
81
|
1 |
|
return new AdviceMatcher( |
82
|
1 |
|
$container->get('aspect.loader'), |
83
|
1 |
|
$container->get('kernel.interceptFunctions') |
84
|
|
|
); |
85
|
11 |
|
}); |
86
|
|
|
|
87
|
|
|
$this->share('aspect.annotation.reader', function(Container $container) { |
|
|
|
|
88
|
3 |
|
$options = $container->get('kernel.options'); |
89
|
3 |
|
$reader = new AnnotationReader(); |
90
|
3 |
|
if (!empty($options['cacheDir'])) { |
91
|
|
|
$reader = new FileCacheReader( |
|
|
|
|
92
|
|
|
$reader, |
93
|
|
|
$options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR, |
94
|
|
|
$options['debug'], |
95
|
|
|
0777 & (~$options['cacheFileMode']) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return $reader; |
100
|
11 |
|
}); |
101
|
|
|
$this->share('aspect.annotation.raw.reader', function() { |
|
|
|
|
102
|
4 |
|
return new RawAnnotationReader(); |
103
|
11 |
|
}); |
104
|
|
|
$this->share('aspect.cache.path.manager', function(Container $container) { |
|
|
|
|
105
|
|
|
return new CachePathManager($container->get('kernel')); |
106
|
11 |
|
}); |
107
|
|
|
|
108
|
|
|
// Pointcut services |
109
|
|
|
$this->share('aspect.pointcut.lexer', function() { |
|
|
|
|
110
|
3 |
|
return new PointcutLexer(); |
111
|
11 |
|
}); |
112
|
11 |
|
$this->share('aspect.pointcut.parser', function(Container $container) { |
|
|
|
|
113
|
3 |
|
return new PointcutParser( |
114
|
3 |
|
new PointcutGrammar( |
115
|
|
|
$container, |
|
|
|
|
116
|
3 |
|
$container->get('aspect.annotation.raw.reader') |
117
|
|
|
) |
118
|
|
|
); |
119
|
11 |
|
}); |
120
|
11 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns a pointcut by identifier |
124
|
|
|
* |
125
|
|
|
* @param string $id Pointcut identifier |
126
|
|
|
* |
127
|
|
|
* @return Aop\Pointcut |
128
|
|
|
*/ |
129
|
1 |
|
public function getPointcut($id) |
130
|
|
|
{ |
131
|
1 |
|
return $this->get("pointcut.{$id}"); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Store the pointcut in the container |
136
|
|
|
* |
137
|
|
|
* @param Aop\Pointcut $pointcut Instance |
138
|
|
|
* @param string $id Key for pointcut |
139
|
|
|
*/ |
140
|
1 |
|
public function registerPointcut(Aop\Pointcut $pointcut, $id) |
141
|
|
|
{ |
142
|
1 |
|
$this->set("pointcut.{$id}", $pointcut, array('pointcut')); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns an advisor by identifier |
147
|
|
|
* |
148
|
|
|
* @param string $id Advisor identifier |
149
|
|
|
* |
150
|
|
|
* @return Aop\Advisor |
151
|
|
|
*/ |
152
|
|
|
public function getAdvisor($id) |
153
|
|
|
{ |
154
|
|
|
return $this->get("advisor.{$id}"); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Store the advisor in the container |
159
|
|
|
* |
160
|
|
|
* @param Aop\Advisor $advisor Instance |
161
|
|
|
* @param string $id Key for advisor |
162
|
|
|
*/ |
163
|
1 |
|
public function registerAdvisor(Aop\Advisor $advisor, $id) |
164
|
|
|
{ |
165
|
1 |
|
$this->set("advisor.{$id}", $advisor, array('advisor')); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns an aspect by id or class name |
170
|
|
|
* |
171
|
|
|
* @param string $aspectName Aspect name |
172
|
|
|
* |
173
|
|
|
* @return Aop\Aspect |
174
|
|
|
*/ |
175
|
1 |
|
public function getAspect($aspectName) |
176
|
|
|
{ |
177
|
1 |
|
return $this->get("aspect.{$aspectName}"); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Register an aspect in the container |
182
|
|
|
* |
183
|
|
|
* @param Aop\Aspect $aspect Instance of concrete aspect |
184
|
|
|
*/ |
185
|
1 |
|
public function registerAspect(Aop\Aspect $aspect) |
186
|
|
|
{ |
187
|
1 |
|
$refAspect = new ReflectionClass($aspect); |
188
|
1 |
|
$this->set("aspect.{$refAspect->name}", $aspect, array('aspect')); |
189
|
1 |
|
$this->addResource($refAspect->getFileName()); |
190
|
1 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Add an AOP resource to the container |
194
|
|
|
* |
195
|
|
|
* Resources is used to check the freshness of AOP cache |
196
|
|
|
*/ |
197
|
2 |
|
public function addResource($resource) |
198
|
|
|
{ |
199
|
2 |
|
$this->resources[] = $resource; |
200
|
2 |
|
$this->maxTimestamp = 0; |
201
|
2 |
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns list of AOP resources |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
public function getResources() |
209
|
|
|
{ |
210
|
|
|
return $this->resources; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Checks the freshness of AOP cache |
215
|
|
|
* |
216
|
|
|
* @param integer $timestamp |
217
|
|
|
* |
218
|
|
|
* @return bool Whether or not concrete file is fresh |
219
|
|
|
*/ |
220
|
1 |
|
public function isFresh($timestamp) |
221
|
|
|
{ |
222
|
1 |
|
if (!$this->maxTimestamp && !empty($this->resources)) { |
223
|
1 |
|
$this->maxTimestamp = max(array_map('filemtime', $this->resources)); |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
return $this->maxTimestamp <= $timestamp; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|