Repository navigation
Expand file tree
/
Copy pathClassLoader.php
More file actions
145 lines (127 loc) · 4.55 KB
/
Copy pathClassLoader.php
File metadata and controls
145 lines (127 loc) · 4.55 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
<?php
/** @noinspection PhpPropertyOnlyWrittenInspection */
namespace Okapi\Aop\Core\AutoloadInterceptor;
use DI\Attribute\Inject;
use Okapi\Aop\Core\Matcher\AspectMatcher;
use Okapi\CodeTransformer\Core\AutoloadInterceptor;
use Okapi\CodeTransformer\Core\AutoloadInterceptor\ClassLoader as CodeTransformerClassLoader;
use Okapi\CodeTransformer\Core\Options\Environment;
use Okapi\CodeTransformer\Core\StreamFilter;
use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector;
use Okapi\Path\Path;
/**
* # AOP Class Loader
*
* This class loader is responsible for loading classes that should be
* intercepted by the AOP framework.
*
* @see AutoloadInterceptor::overloadComposerLoaders() - Initialization of the AOP class loader.
* @see FilterInjector::rewrite() - Switching the original file with a PHP filter.
* @see StreamFilter::filter() - Applying the aspects to the file.
*/
class ClassLoader extends CodeTransformerClassLoader
{
// region DI
#[Inject]
private AspectMatcher $aspectMatcher;
// endregion
/**
* Find the path to the file and match and apply the aspects.
*
* @param string $class
*
* @return false|string
*/
public function findFile($class): false|string
{
$namespacedClass = $class;
$filePath = $this->originalClassLoader->findFile($namespacedClass);
// @codeCoverageIgnoreStart
// Not sure how to test this
if ($filePath === false) {
return false;
}
// @codeCoverageIgnoreEnd
// Prevent infinite recursion
if ($this->isInternal($namespacedClass)) {
return $filePath;
}
$filePath = Path::resolve($filePath);
if (!is_string($filePath)) {
return false;
}
$excludePaths = $this->options->getExcludePaths();
foreach ($excludePaths as $path) {
$resolvedPath = Path::resolve($path);
if (is_string($resolvedPath) && str_starts_with($filePath, $resolvedPath)) {
return $filePath;
}
}
// Query cache state
$cacheState = $this->cacheStateManager->queryCacheState($filePath);
// Production trusts the cache; development checks freshness.
$useCache =
!$this->options->isDebug()
&& $cacheState !== null
&& (
$this->options->getEnvironment() === Environment::PRODUCTION
|| $this->options->getEnvironment() === Environment::DEVELOPMENT
&& $cacheState->isFresh()
);
if ($useCache) {
$cacheFilePath = $cacheState->getFilePath();
if ($cacheFilePath) {
$this->classContainer->addClassContext($filePath, $namespacedClass, $cacheFilePath);
// Preserve the original source path for debuggers.
return $this->filterInjector->rewriteCached($filePath);
}
return $filePath;
}
// Match the aspects
$matchedAspects = $this->aspectMatcher->matchByClassLoaderAndStore($namespacedClass);
// Match the transformer
$matchedTransformers = $this->transformerMatcher->matchAndStore($namespacedClass, $filePath);
// No aspects or transformers matched
if (!($matchedAspects || $matchedTransformers)) {
return $filePath;
}
// Add the class to store the file path
$this->classContainer->addClassContext($filePath, $namespacedClass);
// Replace the file path with a PHP stream filter
/** @see StreamFilter::filter() */
return $this->filterInjector->rewrite($filePath);
}
/**
* Check if the class is internal to the AOP framework.
*
* @param string $namespacedClass
*
* @return bool
*/
protected function isInternal(string $namespacedClass): bool
{
return str_starts_with_any_but_not(
$namespacedClass,
[
'Okapi\Aop\\',
'Okapi\CodeTransformer\\',
'Okapi\Path\\',
'Okapi\Wildcards\\',
'PhpParser\\',
'Microsoft\PhpParser\\',
'DI\\',
'Roave\BetterReflection\\',
'SebastianBergmann\\',
'PHPUnit\\',
'Nette\\',
],
[
'Okapi\CodeTransformer\Tests\\',
'Okapi\Aop\AopKernel',
'Okapi\Aop\Tests\\',
'Nette\PhpGenerator\Factory',
'Nette\Utils\Reflection',
],
);
}
}