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 | 1 | $aspectLoader = new AspectLoader( |
|
53 | 1 | $container, |
|
54 | 1 | $container->get('aspect.annotation.reader') |
|
55 | ); |
||
56 | 1 | $lexer = $container->get('aspect.pointcut.lexer'); |
|
57 | 1 | $parser = $container->get('aspect.pointcut.parser'); |
|
58 | |||
59 | // Register general aspect loader extension |
||
60 | 1 | $aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser)); |
|
61 | 1 | $aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser)); |
|
62 | |||
63 | 1 | 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('kernel.interceptFunctions') |
|
91 | ); |
||
92 | 10 | }); |
|
93 | |||
94 | 10 | $this->share('aspect.annotation.cache', function (Container $container) { |
|
95 | 4 | $options = $container->get('kernel.options'); |
|
96 | |||
97 | 4 | if (!empty($options['annotationCache'])) { |
|
98 | return $options['annotationCache']; |
||
99 | } |
||
100 | |||
101 | 4 | if (!empty($options['cacheDir'])) { |
|
102 | return new DoctrineCache\FilesystemCache( |
||
103 | $options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR, |
||
104 | '.annotations.cache', |
||
105 | 0777 & (~$options['cacheFileMode']) |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | 4 | return new DoctrineCache\ArrayCache(); |
|
110 | 10 | }); |
|
111 | |||
112 | 10 | $this->share('aspect.annotation.reader', function (Container $container) { |
|
113 | 3 | $options = $container->get('kernel.options'); |
|
114 | |||
115 | 3 | return new CachedReader( |
|
116 | 3 | new AnnotationReader(), |
|
117 | 3 | $container->get('aspect.annotation.cache'), |
|
118 | 3 | $options['debug'] ?? false |
|
119 | ); |
||
120 | 10 | }); |
|
121 | |||
122 | 10 | $this->share('aspect.cache.path.manager', function (Container $container) { |
|
123 | return new CachePathManager($container->get('kernel')); |
||
124 | 10 | }); |
|
125 | |||
126 | // Pointcut services |
||
127 | 10 | $this->share('aspect.pointcut.lexer', function () { |
|
128 | 2 | return new PointcutLexer(); |
|
129 | 10 | }); |
|
130 | 10 | $this->share('aspect.pointcut.parser', function (Container $container) { |
|
131 | 2 | return new PointcutParser( |
|
132 | 2 | new PointcutGrammar( |
|
133 | 2 | $container, |
|
134 | 2 | $container->get('aspect.annotation.reader') |
|
135 | ) |
||
136 | ); |
||
137 | 10 | }); |
|
138 | 10 | } |
|
139 | |||
140 | /** |
||
141 | * Returns a pointcut by identifier |
||
142 | */ |
||
143 | 1 | public function getPointcut(string $id): Pointcut |
|
144 | { |
||
145 | 1 | return $this->get("pointcut.{$id}"); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Store the pointcut in the container |
||
150 | */ |
||
151 | 1 | public function registerPointcut(Pointcut $pointcut, string $id): void |
|
152 | { |
||
153 | 1 | $this->set("pointcut.{$id}", $pointcut, ['pointcut']); |
|
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * Returns an advisor by identifier |
||
158 | */ |
||
159 | public function getAdvisor(string $id): Advisor |
||
160 | { |
||
161 | return $this->get("advisor.{$id}"); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Store the advisor in the container |
||
166 | */ |
||
167 | 1 | public function registerAdvisor(Advisor $advisor, string $id): void |
|
168 | { |
||
169 | 1 | $this->set("advisor.{$id}", $advisor, ['advisor']); |
|
170 | 1 | } |
|
171 | |||
172 | /** |
||
173 | * Returns an aspect by id or class name |
||
174 | */ |
||
175 | 1 | public function getAspect(string $aspectName): Aspect |
|
179 | |||
180 | /** |
||
181 | * Register an aspect in the container |
||
182 | */ |
||
183 | 1 | public function registerAspect(Aspect $aspect): void |
|
184 | { |
||
189 | |||
190 | /** |
||
191 | * Add an AOP resource to the container |
||
192 | * Resources is used to check the freshness of AOP cache |
||
193 | * |
||
194 | * @param string $resource Path to the resource |
||
195 | */ |
||
196 | 2 | public function addResource(string $resource) |
|
201 | |||
202 | /** |
||
203 | * Returns list of AOP resources |
||
204 | */ |
||
205 | public function getResources(): array |
||
209 | |||
210 | /** |
||
211 | * Checks the freshness of AOP cache |
||
212 | * |
||
213 | * @return bool Whether or not concrete file is fresh |
||
214 | */ |
||
215 | 1 | public function isFresh(int $timestamp): bool |
|
223 | } |
||
224 |