From 5b7c7b0f6ad62309ab81423d6cd7246c5b380439 Mon Sep 17 00:00:00 2001 From: yuuki takezawa Date: Sun, 15 Jan 2023 23:16:30 +0900 Subject: [PATCH] removed PostConstruct Annotation / Interceptor / PointCut (#90) * removed PostConstruct * Update php.yml --- .coveralls.yml | 3 - .github/workflows/php.yml | 2 +- .gitignore | 1 - .scrutinizer.yml | 15 - .sensiolabs.yml | 3 - .travis.yml | 13 - README.md | 59 +- composer.json | 13 +- phpunit.xml | 1 + src/AnnotateClass.php | 54 -- src/Annotation/MessageDriven.php | 2 +- src/Annotation/PostConstruct.php | 31 - src/AnnotationConfiguration.php | 1 - src/Console/ModulePublishCommand.php | 1 - src/ContainerInterceptor.php | 13 +- src/Modules/PostConstructModule.php | 40 - src/PointCut/PostConstructPointCut.php | 49 -- src/RayAspectKernel.php | 2 +- tests/AnnotationScanMatcherTest.php | 42 -- tests/AspectTestCase.php | 5 + tests/MockApplication.php | 10 +- tests/PostConstructTest.php | 33 - tests/logs/.gitignore | 3 + tests/logs/clover.xml | 966 ------------------------ tests/logs/laravel.log | 0 tests/logs/quality-assurance/.gitignore | 2 + tests/src/AspectPostConstruct.php | 43 -- tests/src/FailedPostConstruct.php | 51 -- tests/src/PostConstructModule.php | 13 - 29 files changed, 33 insertions(+), 1438 deletions(-) delete mode 100644 .coveralls.yml delete mode 100644 .scrutinizer.yml delete mode 100644 .sensiolabs.yml delete mode 100644 .travis.yml delete mode 100644 src/AnnotateClass.php delete mode 100644 src/Annotation/PostConstruct.php delete mode 100644 src/Modules/PostConstructModule.php delete mode 100644 src/PointCut/PostConstructPointCut.php delete mode 100644 tests/AnnotationScanMatcherTest.php delete mode 100644 tests/PostConstructTest.php create mode 100644 tests/logs/.gitignore delete mode 100644 tests/logs/clover.xml delete mode 100644 tests/logs/laravel.log create mode 100644 tests/logs/quality-assurance/.gitignore delete mode 100644 tests/src/AspectPostConstruct.php delete mode 100644 tests/src/FailedPostConstruct.php delete mode 100644 tests/src/PostConstructModule.php diff --git a/.coveralls.yml b/.coveralls.yml deleted file mode 100644 index 3f3f0a3..0000000 --- a/.coveralls.yml +++ /dev/null @@ -1,3 +0,0 @@ -service_name: travis-ci -coverage_clover: tests/logs/clover.xml -json_path: tests/logs/coveralls-upload.json diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 71bd19e..1d8ea01 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.3', '7.4', '8.0'] + php-versions: ['8.0', '8.1'] name: PHP ${{ matrix.php-versions }} Test steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 3358e2b..b2bc05a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ vendor composer.lock composer.phar *.result.cache - diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index c6eb21b..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,15 +0,0 @@ -filter: - paths: - - src/* - excluded_paths: - - tests/* -tools: - php_code_sniffer: - config: - standard: "PSR2" - php_cs_fixer: - config: { level: psr2 } -checks: - php: - code_rating: true - duplication: true diff --git a/.sensiolabs.yml b/.sensiolabs.yml deleted file mode 100644 index 6b5d571..0000000 --- a/.sensiolabs.yml +++ /dev/null @@ -1,3 +0,0 @@ -global_exclude_dirs: - - vendor - - tests diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0f9c6d6..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -sudo: false -language: php -php: - - 7.3 - - 8.0 -services: sqlite3 -before_script: - - composer self-update - - composer install --prefer-source -script: - - chmod -R 777 tests/logs - - ./vendor/bin/phpunit --coverage-clover tests/logs/clover.xml - - chmod 777 tests/logs/clover.xml diff --git a/README.md b/README.md index e94902c..c9037b0 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,7 @@ aspect-oriented programming Package for laravel framework ![Build Status](https://github.com/ytake/Laravel-Aspect/workflows/Tests/badge.svg?branch=master) -[![Coverage Status](http://img.shields.io/coveralls/ytake/Laravel-Aspect/master.svg?style=flat-square)](https://coveralls.io/r/ytake/Laravel-Aspect?branch=master) -[![Scrutinizer Code Quality](http://img.shields.io/scrutinizer/g/ytake/Laravel-Aspect.svg?style=flat-square)](https://scrutinizer-ci.com/g/ytake/Laravel-Aspect/?branch=master) - [![StyleCI](https://styleci.io/repos/40900709/shield)](https://styleci.io/repos/40900709) -[![SensioLabsInsight](https://insight.sensiolabs.com/projects/70dace68-fe04-4039-aeb4-47a64c6acca3/mini.png)](https://insight.sensiolabs.com/projects/70dace68-fe04-4039-aeb4-47a64c6acca3) [![License](http://img.shields.io/packagist/l/ytake/laravel-aspect.svg?style=flat-square)](https://packagist.org/packages/ytake/laravel-aspect) [![Latest Version](http://img.shields.io/packagist/v/ytake/laravel-aspect.svg?style=flat-square)](https://packagist.org/packages/ytake/laravel-aspect) @@ -31,6 +27,7 @@ This library is heavily inspired by the [jcabi/jcabi-aspects](https://github.com 6.0.x | 4.0 7.x | 6.0 8.x | 7.0 + 9.x | 8.0 ### install @@ -40,25 +37,15 @@ $ composer require ytake/laravel-aspect *Supported Auto-Discovery(^Laravel5.5)* -#### for Laravel5.1, 5.2 -[branch](https://github.com/ytake/Laravel-Aspect/tree/master-laravel5-legacy) - ```json - "require": { - "php": ">=5.5.9", - "laravel/framework": "5.*", - "ytake/laravel-aspect": "^1.0" - }, - ``` - -#### for Laravel5.6 +#### for Laravel9 [Laravel-Aspect Supported Laravel5.6](https://github.com/ytake/Laravel-Aspect/blob/master-laravel5.6) ```json "require": { "php": ">=7.1.3", "laravel/framework": "^5.7", - "ytake/laravel-aspect": "^3.0.0" + "ytake/laravel-aspect": "^8.0.0" }, ``` @@ -448,46 +435,6 @@ class AspectQueryLog testing.INFO: QueryLog:AspectQueryLog.multipleDatabaseAppendRecord {"queries":[{"query":"CREATE TABLE tests (test varchar(255) NOT NULL)","bindings":[],"time":0.58,"connectionName":"testing"},{"query":"CREATE TABLE tests (test varchar(255) NOT NULL)","bindings":[],"time":0.31,"connectionName":"testing_second"} ... ``` -### @PostConstruct -The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. - -you must use the PostConstructModule - -```php -use Ytake\LaravelAspect\Annotation\PostConstruct; - -class Something -{ - protected $abstract; - - protected $counter = 0; - - public function __construct(ExampleInterface $abstract) - { - $this->abstract = $abstract; - } - - /** - * @PostConstruct - */ - public function init() - { - $this->counter += 1; - } - - /** - * @return int - */ - public function returning() - { - return $this->counter; - } -} - -``` - -**The method MUST NOT have any parameters** - ### @RetryOnFailure Retry the method in case of exception. diff --git a/composer.json b/composer.json index f547970..7678a4c 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": "^7.3|^8.0", + "php": "^8.0", "illuminate/console": "^9.0", "illuminate/filesystem": "^9.0", "illuminate/support": "^9.0", @@ -30,16 +30,15 @@ "illuminate/bus": "^9.0", "illuminate/queue": "^9.0", "ray/aop": "^2.9", - "doctrine/annotations": "^1.11", - "nikic/php-parser": "^4.0", - "psr/log": "~1.0" + "doctrine/annotations": "^1.10", + "nikic/php-parser": "^4.2", + "psr/log": "^1.0.1 || ^2.0 || ^3.0" }, "require-dev": { - "symfony/framework-bundle": "^5.2", - "symfony/console": "^5.2|^6.0", + "symfony/framework-bundle": "^6.0", + "symfony/console": "^6.0.9", "phpunit/phpunit": "^9.3.3", "mockery/mockery": "*", - "satooshi/php-coveralls": "*", "sebastian/phpcpd": "*", "phploc/phploc": "*", "pdepend/pdepend" : "^2.2.4", diff --git a/phpunit.xml b/phpunit.xml index 0ff583c..042a274 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -23,5 +23,6 @@ ./tests + diff --git a/src/AnnotateClass.php b/src/AnnotateClass.php deleted file mode 100644 index 6b9d465..0000000 --- a/src/AnnotateClass.php +++ /dev/null @@ -1,54 +0,0 @@ -methodAnnotations); - if (!is_array($methods)) { - return ''; - } - foreach ($methods as $method => $annotations) { - foreach ($annotations as $annotation) { - if ($annotation instanceof PostConstruct) { - return $method; - } - } - } - - return ''; - } -} diff --git a/src/Annotation/MessageDriven.php b/src/Annotation/MessageDriven.php index 29d2dcc..2097f02 100644 --- a/src/Annotation/MessageDriven.php +++ b/src/Annotation/MessageDriven.php @@ -22,7 +22,7 @@ use Doctrine\Common\Annotations\Annotation; /** - * Class PostConstruct + * Class MessageDriven * * @Annotation * @Target("METHOD") diff --git a/src/Annotation/PostConstruct.php b/src/Annotation/PostConstruct.php deleted file mode 100644 index d6b112d..0000000 --- a/src/Annotation/PostConstruct.php +++ /dev/null @@ -1,31 +0,0 @@ - 'Ytake\LaravelAspect\Modules\TransactionalModule', 'LoggableModule' => 'Ytake\LaravelAspect\Modules\LoggableModule', 'LogExceptionsModule' => 'Ytake\LaravelAspect\Modules\LogExceptionsModule', - 'PostConstructModule' => 'Ytake\LaravelAspect\Modules\PostConstructModule', 'RetryOnFailureModule' => 'Ytake\LaravelAspect\Modules\RetryOnFailureModule', 'MessageDrivenModule' => 'Ytake\LaravelAspect\Modules\MessageDrivenModule', 'QueryLogModule' => 'Ytake\LaravelAspect\Modules\QueryLogModule', diff --git a/src/ContainerInterceptor.php b/src/ContainerInterceptor.php index bc444e2..4779f81 100644 --- a/src/ContainerInterceptor.php +++ b/src/ContainerInterceptor.php @@ -31,17 +31,12 @@ final class ContainerInterceptor /** @var Container|\Illuminate\Container\Container */ private $container; - /** @var AnnotateClass */ - private $annotateClass; - /** * @param Container $container - * @param AnnotateClass $annotateClass */ - public function __construct(Container $container, AnnotateClass $annotateClass) + public function __construct(Container $container) { $this->container = $container; - $this->annotateClass = $annotateClass; } /** @@ -64,12 +59,6 @@ public function intercept(string $abstract, BindInterface $bind, string $classNa /** @var WeavedInterface $instance */ $instance = $app->make($className, $params); $instance->bindings = $bind->getBindings(); - $method = $this->annotateClass->getPostConstructMethod($instance); - if (!empty($method)) { - $instance->bindings = $bind->getBindings(); - $instance->$method(); - } - return $instance; }); diff --git a/src/Modules/PostConstructModule.php b/src/Modules/PostConstructModule.php deleted file mode 100644 index d994eb4..0000000 --- a/src/Modules/PostConstructModule.php +++ /dev/null @@ -1,40 +0,0 @@ -annotation), - (new Matcher)->any(), - [] - ); - } -} diff --git a/src/RayAspectKernel.php b/src/RayAspectKernel.php index 0e0dc9e..6f32c68 100644 --- a/src/RayAspectKernel.php +++ b/src/RayAspectKernel.php @@ -124,7 +124,7 @@ public function weave(): void */ protected function containerAdaptor(Container $container): ContainerInterceptor { - return new ContainerInterceptor($container, new AnnotateClass()); + return new ContainerInterceptor($container); } /** diff --git a/tests/AnnotationScanMatcherTest.php b/tests/AnnotationScanMatcherTest.php deleted file mode 100644 index 20b85ef..0000000 --- a/tests/AnnotationScanMatcherTest.php +++ /dev/null @@ -1,42 +0,0 @@ -matcher = new AnnotationScanMatcher; - } - - public function testShouldBeBoolean() - { - $result = $this->matcher->matchesClass(new \ReflectionClass(\__Test\AspectPostConstruct::class), [ - PostConstruct::class - ]); - $this->assertTrue($result); - $result = $this->matcher->matchesClass(new \ReflectionClass(\__Test\FailedPostConstruct::class), [ - PostConstruct::class - ]); - $this->assertFalse($result); - - $reflectionClass = new \ReflectionClass(\__Test\AspectPostConstruct::class); - $result = $this->matcher->matchesMethod($reflectionClass->getMethod('getA'), [ - PostConstruct::class - ]); - $this->assertTrue($result); - $reflectionClass = new \ReflectionClass(\__Test\FailedPostConstruct::class); - $result = $this->matcher->matchesMethod($reflectionClass->getMethod('getA'), [ - PostConstruct::class - ]); - $this->assertFalse($result); - } -} diff --git a/tests/AspectTestCase.php b/tests/AspectTestCase.php index afb19ce..64fc901 100644 --- a/tests/AspectTestCase.php +++ b/tests/AspectTestCase.php @@ -86,6 +86,11 @@ public function storagePath() { return __DIR__; } + + public function runningUnitTests() + { + return true; + } }; $this->app->singleton('config', function () { return new \Illuminate\Config\Repository; diff --git a/tests/MockApplication.php b/tests/MockApplication.php index dade42c..4e2ddcf 100644 --- a/tests/MockApplication.php +++ b/tests/MockApplication.php @@ -174,7 +174,7 @@ public function resourcePath($path = '') // TODO: Implement resourcePath() method. } - public function storagePath() + public function storagePath($path = '') { // TODO: Implement storagePath() method. } @@ -269,7 +269,15 @@ public function terminate() // TODO: Implement terminate() method. } + public function maintenanceMode() + { + // TODO: Implement maintenanceMode() method. + } + public function terminating($callback) + { + // TODO: Implement terminating() method. + } } function base_path() diff --git a/tests/PostConstructTest.php b/tests/PostConstructTest.php deleted file mode 100644 index fa6bc1d..0000000 --- a/tests/PostConstructTest.php +++ /dev/null @@ -1,33 +0,0 @@ -manager = new \Ytake\LaravelAspect\AspectManager($this->app); - $this->resolveManager(); - } - - public function testShouldProceedPostConstructSumVariable() - { - /** @var \__Test\AspectPostConstruct $class */ - $class = $this->app->make(\__Test\AspectPostConstruct::class, ['a' => 1]); - $this->assertInstanceOf(\__Test\AspectPostConstruct::class, $class); - $this->assertSame(2, $class->getA()); - } - - protected function resolveManager() - { - /** @var \Ytake\LaravelAspect\RayAspectKernel $aspect */ - $aspect = $this->manager->driver('ray'); - $aspect->register(\__Test\PostConstructModule::class); - $aspect->weave(); - } -} diff --git a/tests/logs/.gitignore b/tests/logs/.gitignore new file mode 100644 index 0000000..ca7255b --- /dev/null +++ b/tests/logs/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore +!quality-assurance diff --git a/tests/logs/clover.xml b/tests/logs/clover.xml deleted file mode 100644 index 5e62529..0000000 --- a/tests/logs/clover.xml +++ /dev/null @@ -1,966 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/logs/laravel.log b/tests/logs/laravel.log deleted file mode 100644 index e69de29..0000000 diff --git a/tests/logs/quality-assurance/.gitignore b/tests/logs/quality-assurance/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/tests/logs/quality-assurance/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/tests/src/AspectPostConstruct.php b/tests/src/AspectPostConstruct.php deleted file mode 100644 index 9a36cab..0000000 --- a/tests/src/AspectPostConstruct.php +++ /dev/null @@ -1,43 +0,0 @@ -index = $index; - } - - /** - * @PostConstruct - */ - public function initialize() - { - $this->index += 1; - } - - /** - * @return int - */ - public function getA() - { - return $this->index; - } -} diff --git a/tests/src/FailedPostConstruct.php b/tests/src/FailedPostConstruct.php deleted file mode 100644 index d2764ac..0000000 --- a/tests/src/FailedPostConstruct.php +++ /dev/null @@ -1,51 +0,0 @@ -index = $index; - } - - /** - * @PostConstruct - */ - public function initialize() - { - $this->index += 1; - } - - /** - * @PostConstruct - */ - public function initializeTwo() - { - - } - - /** - * @return int - */ - public function getA() - { - return $this->index; - } -} diff --git a/tests/src/PostConstructModule.php b/tests/src/PostConstructModule.php deleted file mode 100644 index 4db561e..0000000 --- a/tests/src/PostConstructModule.php +++ /dev/null @@ -1,13 +0,0 @@ -