Skip to content

Commit

Permalink
Merge pull request #63: Added Production/Development environment option
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterWoshid authored Nov 5, 2023
2 parents 1198af2 + 0fa90f7 commit de09610
Show file tree
Hide file tree
Showing 144 changed files with 1,180 additions and 715 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/performance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
operating-system: [ ubuntu-latest ]
php-version: [ '8.1', '8.2' ]

name: PHP ${{ matrix.php-version }} test on ${{ matrix.operating-system }}
name: PHP ${{ matrix.php-version }} performance test on ${{ matrix.operating-system }}

steps:
- name: Checkout Code
Expand All @@ -27,6 +27,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
coverage: none
ini-values: opcache.enable_cli=1

- name: Check PHP version
run: php -v
Expand All @@ -45,4 +46,4 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: PHPUnit Performance Tests
run: vendor/bin/phpunit --testsuite=Performance
run: vendor/bin/phpunit --testsuite=Performance --display-notices
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
operating-system: [ ubuntu-latest ]
php-version: [ '8.1', '8.2' ]

name: PHP ${{ matrix.php-version }} performance tests on ${{ matrix.operating-system }}
name: PHP ${{ matrix.php-version }} tests on ${{ matrix.operating-system }}

steps:
- name: Checkout Code
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: PHPUnit Tests
run: vendor/bin/phpunit --testsuite=Tests --coverage-clover ./tests/coverage.xml
run: vendor/bin/phpunit --testsuite=Tests --coverage-clover ./tests/coverage.xml --display-notices

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ composer require okapi/aop
- [How it works](#how-it-works)
- [Testing](#testing)
- [Contributing](#contributing)
- [Roadmap](#roadmap)



Expand Down Expand Up @@ -793,12 +792,6 @@ class EverythingAspect



## Roadmap

See [Roadmap](https://github.com/okapi-web/php-aop/issues/9) for more details.



## Show your support

Give a ⭐ if this project helped you!
Expand Down
10 changes: 5 additions & 5 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.6",
"version": "1.2.7",
"type": "library",
"homepage": "https://github.com/okapi-web/php-aop",
"license": "MIT",
Expand All @@ -19,14 +19,14 @@
"php-aop"
],
"scripts": {
"test": "phpunit --testsuite=Tests",
"test-performance": "phpunit --testsuite=Performance",
"test-coverage": "phpunit --coverage-html tests/coverage"
"test": "phpunit --testsuite=Tests --display-notices",
"test-performance": "phpunit --testsuite=Performance --display-notices",
"test-coverage": "phpunit --testsuite=Tests --coverage-html tests/coverage --display-notices"
},
"require": {
"php": ">=8.1",
"nette/php-generator": "^4.0",
"okapi/code-transformer": "^1.3",
"okapi/code-transformer": "^1.3.4",
"okapi/wildcards": "^1.0",
"okapi/singleton": "^1.0",
"php-di/php-di": "^7.0"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<testsuites>
<testsuite name="Tests">
<directory>tests/Functional</directory>
<directory>tests/Integration</directory>
</testsuite>

<testsuite name="Performance">
Expand Down
7 changes: 7 additions & 0 deletions src/AopKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
*
* The AOP Kernel is the heart of the AOP library.
* It manages an environment for Aspect Oriented Programming.
*
* 1. Extend this class and define a list of aspects in the {@link $aspects}
* property.
* 2. Call the {@link init()} method early in the application lifecycle.
*
* If you want to modify the kernel options dynamically, override the
* {@link configureOptions()} method.
*/
abstract class AopKernel extends CodeTransformerKernel
{
Expand Down
22 changes: 20 additions & 2 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\Options\Environment;
use Okapi\CodeTransformer\Core\StreamFilter;
use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector;
use Okapi\Path\Path;
Expand Down Expand Up @@ -35,6 +36,8 @@ class ClassLoader extends CodeTransformerClassLoader
* @param class-string $namespacedClass
*
* @return false|string
*
* @noinspection PhpStatementHasEmptyBodyInspection
*/
public function findFile($namespacedClass): false|string
{
Expand All @@ -58,13 +61,28 @@ public function findFile($namespacedClass): false|string
// Query cache state
$cacheState = $this->cacheStateManager->queryCacheState($filePath);

// If the cache is cached and up to date
if ($cacheState?->isFresh() && !$this->options->isDebug()) {
// When debugging, bypass the caching mechanism
if ($this->options->isDebug()) {
// ...
}

// In production mode, use the cache without checking if it is fresh
elseif ($this->options->getEnvironment() === Environment::PRODUCTION
&& $cacheState
) {
// Use the cached file if aspects have been applied
// Or return the original file if no aspects have been applied
return $cacheState->getFilePath() ?? $filePath;
}

// In development mode, check if the cache is fresh
elseif ($this->options->getEnvironment() === Environment::DEVELOPMENT
&& $cacheState
&& $cacheState->isFresh()
) {
return $cacheState->getFilePath() ?? $filePath;
}


// Match the aspects
$matchedAspects = $this->aspectMatcher->matchByClassLoader(
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Container/AspectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function loadAspect(mixed $aspectClassName): void
{
// Check if the aspect is already loaded
if (array_key_exists($aspectClassName, $this->aspectAdviceContainers)) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}

// Validate the aspect
Expand Down
14 changes: 6 additions & 8 deletions src/Core/Invocation/AdviceChainAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ public function setAdviceChain(AdviceChain $adviceChain): void
/**
* Call next advice or target method.
*
* @param bool $allowRepeatedCalls <br>If {@see true}, the original method
* will be called again.<br>
* If {@see false}, the original method will
* be called only once and every subsequent
* call will return the same result.<br>
* Default: {@see false}<br>
* <b>WARNING: May cause unexpected behavior
* and side effects.</b>
* @param bool $allowRepeatedCalls
* <br>If {@see true}, the original method will be called again.<br>
* If {@see false}, the original method will be called only once and every
* subsequent call will return the same result.<br>
* Default: {@see false}<br>
* <b>WARNING: May cause unexpected behavior and side effects.</b>
*
* @return mixed
*/
Expand Down
91 changes: 91 additions & 0 deletions tests/ClassLoaderMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

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\StreamFilter;
use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector;
use Okapi\Path\Path;
use PHPUnit\Framework\Assert;
use ReflectionProperty;

trait ClassLoaderMockTrait
{
private ?ClassLoader $classLoader = null;

private function findClassMock(string $class): string
{
if (!isset($this->classLoader)) {
$this->findClassLoader();
}

return $this->classLoader->findFile($class);
}

private function findOriginalClassMock(string $class): string
{
if (!isset($this->classLoader)) {
$this->findClassLoader();
}

$original = new ReflectionProperty(ClassLoader::class, 'originalClassLoader');
$original = $original->getValue($this->classLoader);
return $original->findFile($class);
}

private function findClassLoader(): void
{
foreach (spl_autoload_functions() as $function) {
if (is_array($function) && $function[0] instanceof ClassLoader) {
$this->classLoader = $function[0];
break;
}
}
}

public function assertWillBeWoven(string $className): void
{
$originalFilePath = Path::resolve($this->findOriginalClassMock($className));

$wovenPath =
FilterInjector::PHP_FILTER_READ .
StreamFilter::FILTER_ID . '/resource=' .
$originalFilePath;

$filePathMock = $this->findClassMock($className);

Assert::assertEquals(
$wovenPath,
$filePathMock,
"$className will not be woven",
);
}

public function assertAspectLoadedFromCache(string $className): void
{
$filePath = $this->findOriginalClassMock($className);
$cachePaths = DI::get(CachePaths::class);
$cachePath = $cachePaths->getProxyCachePath($filePath);
$filePathMock = $this->findClassMock($className);

Assert::assertEquals(
$cachePath,
$filePathMock,
"$className will not be loaded from cache",
);
}

public function assertAspectNotApplied(string $className): void
{
$originalFilePath = Path::resolve($this->findOriginalClassMock($className));
$filePathMock = $this->findClassMock($className);

Assert::assertEquals(
$originalFilePath,
$filePathMock,
"$className will be woven",
);
}
}
32 changes: 0 additions & 32 deletions tests/Functional/AbstractMethod/AbstractMethodTest.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod;

use Okapi\Aop\Tests\ClassLoaderMockTrait;
use Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod\Aspect\FileUploaderAspect;
use Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod\Target\LocalFileUploader;
use Okapi\Aop\Tests\Util;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

#[RunTestsInSeparateProcesses]
class AdviceMatchingAbstractMethodTest extends TestCase
{
use ClassLoaderMockTrait;

/**
* @see FileUploaderAspect::modifyResult()
*/
public function testAbstractMethod(): void
{
Util::clearCache();
Kernel::init();

$this->assertWillBeWoven(LocalFileUploader::class);

$uploader = new LocalFileUploader();

$result = $uploader->upload('C:\Windows\Temp\file.txt');

/** @noinspection PhpConditionAlreadyCheckedInspection */
$this->assertEquals(
'C:/Windows/Temp/file.txt',
$result
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/** @noinspection PhpUnused */
namespace Okapi\Aop\Tests\Functional\AbstractMethod\Aspect;
namespace Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod\Aspect;

use Okapi\Aop\Attributes\After;
use Okapi\Aop\Attributes\Aspect;
use Okapi\Aop\Invocation\AfterMethodInvocation;
use Okapi\Aop\Tests\Functional\AbstractMethod\ClassesToIntercept\FileUploader;
use Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod\Target\FileUploader;

#[Aspect]
class FileUploaderAspect
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod;

use Okapi\Aop\AopKernel;
use Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod\Aspect\FileUploaderAspect;
use Okapi\Aop\Tests\Util;

class Kernel extends AopKernel
{
protected ?string $cacheDir = Util::CACHE_DIR;

protected array $aspects = [
FileUploaderAspect::class,
];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Okapi\Aop\Tests\Functional\AbstractMethod\ClassesToIntercept;
namespace Okapi\Aop\Tests\Functional\AdviceApplication\AdviceMatchingAbstractMethod\Target;

abstract class FileUploader
{
Expand Down
Loading

0 comments on commit de09610

Please sign in to comment.