1 | <?php |
||
29 | class GoAspectContainer extends Container |
||
30 | { |
||
31 | /** |
||
32 | * List of resources for application |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $resources = []; |
||
37 | |||
38 | /** |
||
39 | * Cached timestamp for resources |
||
40 | * |
||
41 | * @var integer |
||
42 | */ |
||
43 | protected $maxTimestamp = 0; |
||
44 | |||
45 | /** |
||
46 | * Constructor for container |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | // Register all services in the container |
||
51 | 10 | $this->share('aspect.loader', function (Container $container) { |
|
52 | 2 | $aspectLoader = new AspectLoader( |
|
53 | 2 | $container, |
|
54 | 2 | $container->get('aspect.annotation.reader') |
|
55 | ); |
||
56 | 2 | $lexer = $container->get('aspect.pointcut.lexer'); |
|
57 | 2 | $parser = $container->get('aspect.pointcut.parser'); |
|
58 | |||
59 | // Register general aspect loader extension |
||
60 | 2 | $aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser)); |
|
61 | 2 | $aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser)); |
|
62 | |||
63 | 2 | return $aspectLoader; |
|
64 | 10 | }); |
|
65 | |||
66 | 10 | $this->share('aspect.cached.loader', function (Container $container) { |
|
67 | $options = $container->get('kernel.options'); |
||
68 | if (!empty($options['cacheDir'])) { |
||
69 | $loader = new CachedAspectLoader( |
||
70 | $container, |
||
71 | 'aspect.loader', |
||
72 | $container->get('kernel.options') |
||
73 | ); |
||
74 | } else { |
||
75 | $loader = $container->get('aspect.loader'); |
||
76 | } |
||
77 | |||
78 | return $loader; |
||
79 | 10 | }); |
|
80 | |||
81 | 10 | $this->share('aspect.advisor.accessor', function (Container $container) { |
|
82 | return new LazyAdvisorAccessor( |
||
83 | $container, |
||
84 | $container->get('aspect.cached.loader') |
||
85 | ); |
||
86 | 10 | }); |
|
87 | |||
88 | 10 | $this->share('aspect.advice_matcher', function (Container $container) { |
|
89 | 1 | return new AdviceMatcher( |
|
90 | 1 | $container->get('aspect.loader'), |
|
91 | 1 | $container->get('kernel.interceptFunctions') |
|
92 | ); |
||
93 | 10 | }); |
|
94 | |||
95 | 10 | $this->share('aspect.annotation.cache', function (Container $container) { |
|
96 | 5 | $options = $container->get('kernel.options'); |
|
97 | |||
98 | 5 | if (!empty($options['annotationCache'])) { |
|
99 | return $options['annotationCache']; |
||
100 | } |
||
101 | |||
102 | 5 | if (!empty($options['cacheDir'])) { |
|
103 | return new DoctrineCache\FilesystemCache( |
||
104 | $options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR, |
||
105 | '.annotations.cache', |
||
106 | 0777 & (~$options['cacheFileMode']) |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | 5 | return new DoctrineCache\ArrayCache(); |
|
111 | 10 | }); |
|
112 | |||
113 | 10 | $this->share('aspect.annotation.reader', function (Container $container) { |
|
114 | 4 | $options = $container->get('kernel.options'); |
|
115 | |||
116 | 4 | return new CachedReader( |
|
117 | 4 | new AnnotationReader(), |
|
118 | 4 | $container->get('aspect.annotation.cache'), |
|
119 | 4 | $options['debug'] ?? false |
|
120 | ); |
||
121 | 10 | }); |
|
122 | |||
123 | 10 | $this->share('aspect.cache.path.manager', function (Container $container) { |
|
124 | return new CachePathManager($container->get('kernel')); |
||
125 | 10 | }); |
|
126 | |||
127 | // Pointcut services |
||
128 | 10 | $this->share('aspect.pointcut.lexer', function () { |
|
129 | 3 | return new PointcutLexer(); |
|
130 | 10 | }); |
|
131 | 10 | $this->share('aspect.pointcut.parser', function (Container $container) { |
|
132 | 3 | return new PointcutParser( |
|
133 | 3 | new PointcutGrammar( |
|
134 | 3 | $container, |
|
135 | 3 | $container->get('aspect.annotation.reader') |
|
136 | ) |
||
137 | ); |
||
138 | 10 | }); |
|
139 | 10 | } |
|
140 | |||
141 | /** |
||
142 | * Returns a pointcut by identifier |
||
143 | * |
||
144 | * @param string $id Pointcut identifier |
||
145 | * |
||
146 | * @return Pointcut |
||
147 | */ |
||
148 | 1 | public function getPointcut(string $id): Pointcut |
|
152 | |||
153 | /** |
||
154 | * Store the pointcut in the container |
||
155 | * |
||
156 | * @param Pointcut $pointcut Instance |
||
157 | * @param string $id Key for pointcut |
||
158 | */ |
||
159 | 1 | public function registerPointcut(Pointcut $pointcut, string $id) |
|
163 | |||
164 | /** |
||
165 | * Returns an advisor by identifier |
||
166 | * |
||
167 | * @param string $id Advisor identifier |
||
168 | * |
||
169 | * @return Advisor |
||
170 | */ |
||
171 | public function getAdvisor(string $id): Advisor |
||
175 | |||
176 | /** |
||
177 | * Store the advisor in the container |
||
178 | * |
||
179 | * @param Advisor $advisor Instance |
||
180 | * @param string $id Key for advisor |
||
181 | */ |
||
182 | 1 | public function registerAdvisor(Advisor $advisor, string $id) |
|
186 | |||
187 | /** |
||
188 | * Returns an aspect by id or class name |
||
189 | * |
||
190 | * @param string $aspectName Aspect name |
||
191 | * |
||
192 | * @return Aspect |
||
193 | */ |
||
194 | 1 | public function getAspect(string $aspectName): Aspect |
|
198 | |||
199 | /** |
||
200 | * Register an aspect in the container |
||
201 | * |
||
202 | * @param Aspect $aspect Instance of concrete aspect |
||
203 | */ |
||
204 | 1 | public function registerAspect(Aspect $aspect) |
|
210 | |||
211 | /** |
||
212 | * Add an AOP resource to the container |
||
213 | * Resources is used to check the freshness of AOP cache |
||
214 | * |
||
215 | * @param string $resource Path to the resource |
||
216 | */ |
||
217 | 2 | public function addResource(string $resource) |
|
222 | |||
223 | /** |
||
224 | * Returns list of AOP resources |
||
225 | */ |
||
226 | public function getResources(): array |
||
230 | |||
231 | /** |
||
232 | * Checks the freshness of AOP cache |
||
233 | * |
||
234 | * @param integer $timestamp |
||
235 | * |
||
236 | * @return bool Whether or not concrete file is fresh |
||
237 | */ |
||
238 | 1 | public function isFresh(int $timestamp): bool |
|
246 | } |
||
247 |