Skip to content

Commit 4848926

Browse files
committed
Added cached transformer target query results
1 parent 68ac00d commit 4848926

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

src/Service/TransformerContainer.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class TransformerContainer implements ServiceInterface
3737
*/
3838
private array $transformerTargets = [];
3939

40+
/**
41+
* Cached transformer target query results.
42+
*
43+
* @var array
44+
*/
45+
private array $transformerQueryResults = [];
46+
4047
// region Pre-Initialization
4148

4249
/**
@@ -121,26 +128,7 @@ public static function getTargetClasses(): array
121128
return array_keys($instance->transformerTargets);
122129
}
123130

124-
/**
125-
* Return the list of transformers that match the class name.
126-
*
127-
* @param string $className
128-
*
129-
* @return Transformer[]
130-
*/
131-
private function matchTransformers(string $className): array
132-
{
133-
$matchedInstances = [];
134-
135-
foreach ($this->transformerTargets as $target => $instances) {
136-
$regex = Regex::fromWildcard($target);
137-
if ($regex->matches($className)) {
138-
$matchedInstances = array_merge($matchedInstances, $instances);
139-
}
140-
}
141-
142-
return $matchedInstances;
143-
}
131+
// region Transform Code
144132

145133
/**
146134
* Transform the code.
@@ -186,6 +174,37 @@ public static function transform(Metadata $metadata)
186174
CacheStateManager::setCacheState($originalFilePath, $cacheState);
187175
}
188176

177+
/**
178+
* Return the list of transformers that match the class name.
179+
*
180+
* @param string $className
181+
*
182+
* @return Transformer[]
183+
*/
184+
public static function matchTransformers(string $className): array
185+
{
186+
$instance = self::getInstance();
187+
188+
// Check if the query has been cached
189+
if (isset($instance->transformerQueryResults[$className])) {
190+
return $instance->transformerQueryResults[$className];
191+
}
192+
193+
// Match the transformers
194+
$matchedInstances = [];
195+
foreach ($instance->transformerTargets as $target => $instances) {
196+
$regex = Regex::fromWildcard($target);
197+
if ($regex->matches($className)) {
198+
$matchedInstances = array_merge($matchedInstances, $instances);
199+
}
200+
}
201+
202+
// Cache the query result
203+
$instance->transformerQueryResults[$className] = $matchedInstances;
204+
205+
return $matchedInstances;
206+
}
207+
189208
/**
190209
* Get the list of transformer file paths.
191210
*
@@ -216,7 +235,7 @@ function (Transformer $transformer) {
216235
* @return void
217236
* @noinspection PhpMissingReturnTypeInspection For okapi/aop
218237
*/
219-
protected function processTransformers(Metadata $metadata, array $transformers)
238+
private function processTransformers(Metadata $metadata, array $transformers)
220239
{
221240
// Sort the transformers by priority
222241
usort(
@@ -230,4 +249,6 @@ function (Transformer $a, Transformer $b) {
230249
$transformer->transform($metadata->code);
231250
}
232251
}
252+
253+
// endregion
233254
}

0 commit comments

Comments
 (0)