diff --git a/composer.json b/composer.json index 8659100..2712396 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "okapi/aop", "description": "PHP AOP is a PHP library that provides a powerful Aspect Oriented Programming (AOP) implementation for PHP.", - "version": "1.2.9", + "version": "1.2.10", "type": "library", "homepage": "https://github.com/okapi-web/php-aop", "license": "MIT", @@ -26,7 +26,7 @@ "require": { "php": ">=8.1", "nette/php-generator": "^4.0", - "okapi/code-transformer": "1.3.5", + "okapi/code-transformer": "1.3.6", "okapi/wildcards": "^1.0", "okapi/singleton": "^1.0", "php-di/php-di": "^7.0" diff --git a/src/Core/AutoloadInterceptor/ClassLoader.php b/src/Core/AutoloadInterceptor/ClassLoader.php index 8c45b12..df66749 100644 --- a/src/Core/AutoloadInterceptor/ClassLoader.php +++ b/src/Core/AutoloadInterceptor/ClassLoader.php @@ -6,6 +6,7 @@ use Okapi\Aop\Core\Matcher\AspectMatcher; use Okapi\CodeTransformer\Core\AutoloadInterceptor; use Okapi\CodeTransformer\Core\AutoloadInterceptor\ClassLoader as CodeTransformerClassLoader; +use Okapi\CodeTransformer\Core\CachedStreamFilter; use Okapi\CodeTransformer\Core\Options\Environment; use Okapi\CodeTransformer\Core\StreamFilter; use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector; @@ -71,8 +72,22 @@ public function findFile($namespacedClass): false|string && $cacheState ) { // Use the cached file if aspects have been applied + if ($cacheFilePath = $cacheState->getFilePath()) { + $this->classContainer->addClassContext( + $filePath, + $namespacedClass, + $cacheFilePath, + ); + + // For cached files, the debugger will have trouble finding the + // original file, that's why we rewrite the file path with a PHP + // stream filter + /** @see CachedStreamFilter::filter() */ + return $this->filterInjector->rewriteCached($filePath); + } + // Or return the original file if no aspects have been applied - return $cacheState->getFilePath() ?? $filePath; + return $filePath; } // In development mode, check if the cache is fresh @@ -80,17 +95,27 @@ public function findFile($namespacedClass): false|string && $cacheState && $cacheState->isFresh() ) { - return $cacheState->getFilePath() ?? $filePath; + if ($cacheFilePath = $cacheState->getFilePath()) { + $this->classContainer->addClassContext( + $filePath, + $namespacedClass, + $cacheFilePath, + ); + + return $this->filterInjector->rewriteCached($filePath); + } + + return $filePath; } // Match the aspects - $matchedAspects = $this->aspectMatcher->matchByClassLoader( + $matchedAspects = $this->aspectMatcher->matchByClassLoaderAndStore( $namespacedClass, ); // Match the transformer - $matchedTransformers = $this->transformerMatcher->match( + $matchedTransformers = $this->transformerMatcher->matchAndStore( $namespacedClass, $filePath, ); @@ -101,7 +126,7 @@ public function findFile($namespacedClass): false|string } // Add the class to store the file path - $this->classContainer->addNamespacedClassPath($filePath, $namespacedClass); + $this->classContainer->addClassContext($filePath, $namespacedClass); // Replace the file path with a PHP stream filter /** @see StreamFilter::filter() */ diff --git a/src/Core/Matcher/AspectMatcher.php b/src/Core/Matcher/AspectMatcher.php index 88d49f7..77736fa 100644 --- a/src/Core/Matcher/AspectMatcher.php +++ b/src/Core/Matcher/AspectMatcher.php @@ -78,7 +78,7 @@ class AspectMatcher * * @return bool */ - public function matchByClassLoader(string $namespacedClass): bool + public function matchByClassLoaderAndStore(string $namespacedClass): bool { // Get the reflection class $refClass = $this->reflectionHelper->getReflectionClass($namespacedClass); diff --git a/test b/test new file mode 100644 index 0000000..e69de29 diff --git a/tests/ClassLoaderMockTrait.php b/tests/ClassLoaderMockTrait.php index a75271d..d39bdc5 100644 --- a/tests/ClassLoaderMockTrait.php +++ b/tests/ClassLoaderMockTrait.php @@ -3,8 +3,7 @@ namespace Okapi\Aop\Tests; use Okapi\Aop\Core\AutoloadInterceptor\ClassLoader; -use Okapi\Aop\Core\Cache\CachePaths; -use Okapi\CodeTransformer\Core\DI; +use Okapi\CodeTransformer\Core\CachedStreamFilter; use Okapi\CodeTransformer\Core\StreamFilter; use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector; use Okapi\Path\Path; @@ -65,9 +64,13 @@ public function assertWillBeWoven(string $className): void public function assertAspectLoadedFromCache(string $className): void { - $filePath = $this->findOriginalClassMock($className); - $cachePaths = DI::get(CachePaths::class); - $cachePath = $cachePaths->getProxyCachePath($filePath); + $filePath = Path::resolve($this->findOriginalClassMock($className)); + + $cachePath = + FilterInjector::PHP_FILTER_READ . + CachedStreamFilter::CACHED_FILTER_ID . '/resource=' . + $filePath; + $filePathMock = $this->findClassMock($className); Assert::assertEquals(