11<?php
22/** @noinspection PhpPropertyOnlyWrittenInspection */
3- namespace Okapi \CodeTransformer \Service \ ClassLoader ;
3+ namespace Okapi \CodeTransformer \Core \ AutoloadInterceptor ;
44
55use Composer \Autoload \ClassLoader as ComposerClassLoader ;
66use DI \Attribute \Inject ;
7- use Okapi \CodeTransformer \Service \AutoloadInterceptor ;
8- use Okapi \CodeTransformer \Service \CacheStateManager ;
9- use Okapi \CodeTransformer \Service \Matcher \TransformerMatcher ;
10- use Okapi \CodeTransformer \Service \Options ;
11- use Okapi \CodeTransformer \Service \StreamFilter ;
12- use Okapi \CodeTransformer \Service \StreamFilter \FilterInjector ;
7+ use Okapi \CodeTransformer \Core \AutoloadInterceptor ;
8+ use Okapi \CodeTransformer \Core \ Cache \CacheStateManager ;
9+ use Okapi \CodeTransformer \Core \Matcher \TransformerMatcher ;
10+ use Okapi \CodeTransformer \Core \Options ;
11+ use Okapi \CodeTransformer \Core \StreamFilter ;
12+ use Okapi \CodeTransformer \Core \StreamFilter \FilterInjector ;
1313use Okapi \Path \Path ;
1414
1515/**
@@ -46,12 +46,12 @@ class ClassLoader extends ComposerClassLoader
4646 /**
4747 * Code Transformer class loader constructor.
4848 *
49- * @param ComposerClassLoader $original
49+ * @param ComposerClassLoader $originalClassLoader
5050 *
5151 * @noinspection PhpMissingParentConstructorInspection (Parent already constructed)
5252 */
5353 public function __construct (
54- public readonly ComposerClassLoader $ original ,
54+ public ComposerClassLoader $ originalClassLoader ,
5555 ) {}
5656
5757 /**
@@ -60,6 +60,8 @@ public function __construct(
6060 * @param $namespacedClass
6161 *
6262 * @return bool
63+ *
64+ * @noinspection PhpParameterNameChangedDuringInheritanceInspection
6365 */
6466 public function loadClass ($ namespacedClass ): bool
6567 {
@@ -76,15 +78,17 @@ public function loadClass($namespacedClass): bool
7678 }
7779
7880 /**
79- * Find the path to the file and apply the transformers.
81+ * Find the path to the file and match and apply the transformers.
8082 *
8183 * @param $namespacedClass
8284 *
8385 * @return false|string
86+ *
87+ * @noinspection PhpParameterNameChangedDuringInheritanceInspection
8488 */
8589 public function findFile ($ namespacedClass ): false |string
8690 {
87- $ filePath = $ this ->original ->findFile ($ namespacedClass );
91+ $ filePath = $ this ->originalClassLoader ->findFile ($ namespacedClass );
8892
8993 // @codeCoverageIgnoreStart
9094 // Not sure how to test this
@@ -100,53 +104,52 @@ public function findFile($namespacedClass): false|string
100104
101105 $ filePath = Path::resolve ($ filePath );
102106
107+
108+
109+ // Query cache state
110+ $ cacheState = $ this ->cacheStateManager ->queryCacheState ($ filePath );
111+
112+ // Check if the file is cached and up to date
113+ if ($ cacheState ?->isFresh() && !$ this ->options ->isDebug ()) {
114+ // Use the cached file if transformations have been applied
115+ // Or return the original file if no transformations have been applied
116+ return $ cacheState ->getFilePath () ?? $ filePath ;
117+ }
118+
119+
103120 // Check if the class should be transformed
104- if ($ this ->transformerMatcher ->shouldTransform ($ namespacedClass )) {
105- $ cacheState = $ this ->cacheStateManager ->queryCacheState ($ filePath );
106-
107- // Check if the file is cached and up to date
108- if (!$ this ->options ->isDebug () && $ cacheState ?->isFresh()) {
109- // Use the cached file if transformations have been applied
110- // Or return the original file if no transformations have been applied
111- return $ cacheState ->cachedFilePath ?? $ filePath ;
112- }
113-
114- // Add the class to store the file path
115- $ this ->classContainer ->addNamespacedClassPath ($ filePath , $ namespacedClass );
116-
117- // Replace the file path with a PHP stream filter
118- /** @see StreamFilter::filter() */
119- return $ this ->filterInjector ->rewrite ($ filePath );
121+ if (!$ this ->transformerMatcher ->match ($ namespacedClass , $ filePath )) {
122+ return $ filePath ;
120123 }
121124
122- return $ filePath ;
125+ // Add the class to store the file path
126+ $ this ->classContainer ->addNamespacedClassPath ($ filePath , $ namespacedClass );
127+
128+ // Replace the file path with a PHP stream filter
129+ /** @see StreamFilter::filter() */
130+ return $ this ->filterInjector ->rewrite ($ filePath );
123131 }
124132
125133 /**
126134 * Check if the class is internal to the Code Transformer.
127135 *
128- * @param string $class
136+ * @param string $namespacedClass
129137 *
130138 * @return bool
131139 */
132- protected function isInternal (string $ class ): bool
140+ protected function isInternal (string $ namespacedClass ): bool
133141 {
134- // Code Transformer
135- if (str_starts_with ($ class , "Okapi \\CodeTransformer \\" )
136- && !str_starts_with ($ class , "Okapi \\CodeTransformer \\Tests \\" )) {
137- return true ;
138- }
139-
140- // Wildcards
141- if (str_starts_with ($ class , "Okapi \\Wildcards \\" )) {
142- return true ;
143- }
144-
145- // DI
146- if (str_starts_with ($ class , "DI \\" )) {
147- return true ;
148- }
149-
150- return false ;
142+ return str_starts_with_any_but_not (
143+ $ namespacedClass ,
144+ [
145+ 'Okapi \\CodeTransformer \\' ,
146+ 'Okapi \\Path \\' ,
147+ 'Okapi \\Wildcards \\' ,
148+ 'DI \\'
149+ ],
150+ [
151+ 'Okapi \\CodeTransformer \\Tests \\'
152+ ]
153+ );
151154 }
152155}
0 commit comments