Repository navigation
Expand file tree
/
Copy pathAspectProcessor.php
More file actions
236 lines (208 loc) · 8.12 KB
/
Copy pathAspectProcessor.php
File metadata and controls
236 lines (208 loc) · 8.12 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* @noinspection PhpPropertyOnlyWrittenInspection
* @noinspection PhpPossiblePolymorphicInvocationInspection
*/
namespace Okapi\Aop\Core\Processor;
use DI\Attribute\Inject;
use Nette\PhpGenerator\Factory;
use Okapi\Aop\Core\Cache\CacheState\WovenCacheState;
use Okapi\Aop\Core\Container\AdviceContainer;
use Okapi\Aop\Core\Matcher\AspectMatcher;
use Okapi\Aop\Core\Transform\ProxiedClassModifier;
use Okapi\Aop\Core\Transform\WovenClassBuilder;
use Okapi\CodeTransformer\Core\Cache\CacheState;
use Okapi\CodeTransformer\Core\Cache\CacheState\NoTransformationsCacheState;
use Okapi\CodeTransformer\Core\Cache\CacheState\TransformedCacheState;
use Okapi\CodeTransformer\Core\DI;
use Okapi\CodeTransformer\Core\Processor\TransformerProcessor;
use Okapi\CodeTransformer\Core\StreamFilter\Metadata;
use Okapi\CodeTransformer\Transformer\Code;
use Okapi\Filesystem\Filesystem;
/**
* # Aspect Processor
*
* This class is used to process the aspects and weave the advices into the
* target classes.
*/
class AspectProcessor extends TransformerProcessor
{
// region DI
#[Inject]
private AspectMatcher $aspectMatcher;
// endregion
/**
* @inheritDoc
*
* @noinspection PhpPossiblePolymorphicInvocationInspection
*/
public function transform(Metadata $metadata): void
{
$namespacedClass = $metadata->code->getNamespacedClass();
// Get the matched transformers
$transformerContainers = $this->transformerMatcher->getMatchedTransformerContainers($namespacedClass);
// Match the advices
$adviceContainers = $this->aspectMatcher->getMatchedAdviceContainers($namespacedClass);
// Process the transformers
if ($transformerContainers) {
$this->processTransformers($metadata, $transformerContainers);
}
// Process the advices
$wovenFile = null;
if (
$adviceContainers !== []
// Don't weave the factory
&& $namespacedClass !== Factory::class
) {
$wovenFile = $this->processAdvices($metadata, $adviceContainers);
}
$originalFilePath = $metadata->uri;
$cachePaths = $this->cachePaths;
if (!$cachePaths instanceof \Okapi\Aop\Core\Cache\CachePaths) {
throw new \LogicException('Aspect processing requires AOP cache paths.');
}
$proxyFilePath = $cachePaths->getProxyCachePath($originalFilePath);
$wovenFilePath = $cachePaths->getWovenCachePath($originalFilePath);
$transformed = $metadata->code->hasChanges();
// Save the transformed code
if ($transformed) {
// Proxy
Filesystem::writeFile($proxyFilePath, $metadata->code->getNewSource());
// Weaving
if ($wovenFile) {
Filesystem::writeFile($wovenFilePath, $wovenFile);
}
}
// Update the cache state
$modificationTime = $_SERVER['REQUEST_TIME'] ?? time();
if ($wovenFile) {
$transformerFilePaths = $transformerContainers
? $this->getTransformerFilePaths($transformerContainers)
: [];
$adviceNames = $this->getAdviceNames($adviceContainers);
$aspectFilePaths = $this->getAspectFilePaths($adviceContainers);
$aspectClassNames = $this->getAspectClassNames($adviceContainers);
$cacheState = DI::make(WovenCacheState::class, [
CacheState::DATA => [
CacheState::ORIGINAL_FILE_PATH_KEY => $originalFilePath,
CacheState::NAMESPACED_CLASS_KEY => $namespacedClass,
CacheState::MODIFICATION_TIME_KEY => $modificationTime,
WovenCacheState::PROXY_FILE_PATH_KEY => $proxyFilePath,
WovenCacheState::WOVEN_FILE_PATH_KEY => $wovenFilePath,
WovenCacheState::TRANSFORMER_FILE_PATHS_KEY => $transformerFilePaths,
WovenCacheState::ADVICE_NAMES_KEY => $adviceNames,
WovenCacheState::ASPECT_FILE_PATHS_KEY => $aspectFilePaths,
WovenCacheState::ASPECT_CLASS_NAMES_KEY => $aspectClassNames,
],
]);
$this->cacheStateManager->setCacheState($originalFilePath, $cacheState);
return;
}
if ($transformed) {
$transformerFilePaths = $this->getTransformerFilePaths($transformerContainers);
$cacheState = DI::make(TransformedCacheState::class, [
CacheState::DATA => [
CacheState::ORIGINAL_FILE_PATH_KEY => $originalFilePath,
CacheState::NAMESPACED_CLASS_KEY => $namespacedClass,
CacheState::MODIFICATION_TIME_KEY => $modificationTime,
TransformedCacheState::TRANSFORMED_FILE_PATH_KEY => $proxyFilePath,
TransformedCacheState::TRANSFORMER_FILE_PATHS_KEY => $transformerFilePaths,
],
]);
$this->cacheStateManager->setCacheState($originalFilePath, $cacheState);
return;
}
$cacheState = DI::make(NoTransformationsCacheState::class, [
CacheState::DATA => [
CacheState::ORIGINAL_FILE_PATH_KEY => $originalFilePath,
CacheState::NAMESPACED_CLASS_KEY => $namespacedClass,
CacheState::MODIFICATION_TIME_KEY => $modificationTime,
],
]);
$this->cacheStateManager->setCacheState($originalFilePath, $cacheState);
}
/**
* Process the advices.
*
* @param Metadata $metadata
* @param AdviceContainer[] $adviceContainers
*
* @return string The woven code
*/
private function processAdvices(Metadata $metadata, array $adviceContainers): string
{
// Sort the advices by priority
usort($adviceContainers, static function (AdviceContainer $a, AdviceContainer $b) {
$orderA = $a->adviceAttributeInstance->order;
$orderB = $b->adviceAttributeInstance->order;
return $orderA <=> $orderB;
});
$proxiedClassModifier = DI::make(ProxiedClassModifier::class, [
'metadata' => $metadata,
]);
$proxiedClassModifier->modify();
// Create the weaving file
return $this->processAdviceContainers($adviceContainers, $metadata->code);
}
/**
* Process the advices.
*
* @param AdviceContainer[] $adviceContainers
* @param Code $code
*
* @return string
*/
private function processAdviceContainers(array $adviceContainers, Code $code): string
{
$weavingClassBuilder = DI::make(WovenClassBuilder::class, [
'code' => $code,
'adviceContainers' => $adviceContainers,
]);
return $weavingClassBuilder->build();
}
/**
* Get the advice names.
*
* @param AdviceContainer[] $adviceContainers
*
* @return string[]
*/
protected function getAdviceNames(array $adviceContainers): array
{
return array_unique(array_map(
static fn(AdviceContainer $adviceContainer) => $adviceContainer->getName(),
$adviceContainers,
));
}
/**
* Get the aspect file paths.
*
* @param AdviceContainer[] $adviceContainers
*
* @return string[]
*/
protected function getAspectFilePaths(array $adviceContainers): array
{
return array_unique(array_map(static function (AdviceContainer $adviceContainer) {
$filePath = $adviceContainer->aspectRefClass->getFileName();
if ($filePath === false) {
throw new \LogicException('An aspect must have a source file.');
}
return $filePath;
}, $adviceContainers));
}
/**
* Get the aspect class names.
*
* @param AdviceContainer[] $adviceContainers
*
* @return class-string[]
*/
protected function getAspectClassNames(array $adviceContainers): array
{
return array_unique(array_map(
static fn(AdviceContainer $adviceContainer) => $adviceContainer->aspectClassName,
$adviceContainers,
));
}
}