Skip to content

Fixed issues with debugging #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
35 changes: 30 additions & 5 deletions src/Core/AutoloadInterceptor/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,26 +72,50 @@ 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
elseif ($this->options->getEnvironment() === Environment::DEVELOPMENT
&& $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,
);
Expand All @@ -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() */
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Matcher/AspectMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Empty file added test
Empty file.
13 changes: 8 additions & 5 deletions tests/ClassLoaderMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Loading