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