1 | <?php |
||
28 | class AspectLoader |
||
29 | { |
||
30 | /** |
||
31 | * Aspect container instance |
||
32 | */ |
||
33 | protected $container; |
||
34 | |||
35 | /** |
||
36 | * List of aspect loaders |
||
37 | * |
||
38 | * @var AspectLoaderExtension[][] |
||
39 | */ |
||
40 | protected $loaders = []; |
||
41 | |||
42 | /** |
||
43 | * Annotation reader for aspects |
||
44 | */ |
||
45 | protected $annotationReader; |
||
46 | |||
47 | /** |
||
48 | * List of aspect class names that were loaded |
||
49 | * |
||
50 | * @var string[] |
||
51 | */ |
||
52 | protected $loadedAspects = []; |
||
53 | |||
54 | /** |
||
55 | * Loader constructor |
||
56 | */ |
||
57 | 9 | public function __construct(AspectContainer $container, Reader $reader) |
|
62 | |||
63 | /** |
||
64 | * Register an aspect loader extension |
||
65 | * |
||
66 | * This method allows to extend the logic of aspect loading by registering an extension for loader. |
||
67 | */ |
||
68 | 2 | public function registerLoaderExtension(AspectLoaderExtension $loader): void |
|
75 | |||
76 | /** |
||
77 | * Loads an aspect with the help of aspect loaders, but don't register it in the container |
||
78 | * |
||
79 | * @see loadAndRegister() method for registration |
||
80 | * |
||
81 | * @return Pointcut[]|Advisor[] |
||
82 | */ |
||
83 | 1 | public function load(Aspect $aspect): array |
|
108 | |||
109 | /** |
||
110 | * Loads and register all items of aspect in the container |
||
111 | */ |
||
112 | 1 | public function loadAndRegister(Aspect $aspect): void |
|
127 | |||
128 | /** |
||
129 | * Returns list of unloaded aspects in the container |
||
130 | * |
||
131 | * @return Aspect[] |
||
132 | */ |
||
133 | 1 | public function getUnloadedAspects(): array |
|
145 | |||
146 | /** |
||
147 | * Load definitions from specific aspect part into the aspect container |
||
148 | * |
||
149 | * @param Aspect $aspect Aspect instance |
||
150 | * @param Reflector $reflector Reflection instance |
||
151 | * @param array|AspectLoaderExtension[] $loaders List of loaders that can produce advisors from aspect class |
||
152 | * |
||
153 | * @throws \InvalidArgumentException If kind of loader isn't supported |
||
154 | * |
||
155 | * @return array|Pointcut[]|Advisor[] |
||
156 | */ |
||
157 | 1 | protected function loadFrom(Aspect $aspect, Reflector $reflector, array $loaders): array |
|
158 | { |
||
159 | 1 | $loadedItems = []; |
|
160 | |||
161 | 1 | foreach ($loaders as $loader) { |
|
162 | 1 | $loaderKind = $loader->getKind(); |
|
163 | switch ($loaderKind) { |
||
164 | 1 | case AspectLoaderExtension::KIND_REFLECTION: |
|
165 | if ($loader->supports($aspect, $reflector)) { |
||
166 | $loadedItems += $loader->load($aspect, $reflector); |
||
167 | } |
||
168 | break; |
||
169 | |||
170 | 1 | case AspectLoaderExtension::KIND_ANNOTATION: |
|
171 | 1 | $annotations = $this->getAnnotations($reflector); |
|
172 | 1 | foreach ($annotations as $annotation) { |
|
173 | 1 | if ($loader->supports($aspect, $reflector, $annotation)) { |
|
174 | 1 | $loadedItems += $loader->load($aspect, $reflector, $annotation); |
|
175 | } |
||
176 | } |
||
177 | 1 | break; |
|
178 | |||
179 | default: |
||
180 | throw new InvalidArgumentException("Unsupported loader kind {$loaderKind}"); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | 1 | return $loadedItems; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * Return list of annotations for reflection point |
||
189 | * |
||
190 | * @return array list of annotations |
||
191 | * @throws \InvalidArgumentException if $reflector is unsupported |
||
192 | */ |
||
193 | 1 | protected function getAnnotations(Reflector $reflector): array |
|
209 | } |
||
210 |