From c16153bb99c191e3c75ec78f2519d5cd915e5bb4 Mon Sep 17 00:00:00 2001 From: Paramtamtam <7326800+tarampampam@users.noreply.github.com> Date: Sat, 12 Feb 2022 15:17:09 +0500 Subject: [PATCH 1/6] laravel-ignition integration and CLI mode detection --- CHANGELOG.md | 13 ++++ bin/rr-worker | 13 ++++ composer.json | 1 + src/Defaults.php | 1 + src/Dumper/Dumper.php | 4 ++ .../ResetLaravelIgnitionListener.php | 53 ++++++++++++++++ .../ResetLaravelIgnitionListenerTest.php | 63 +++++++++++++++++++ 7 files changed, 148 insertions(+) create mode 100644 src/Listeners/ResetLaravelIgnitionListener.php create mode 100644 tests/Unit/Listeners/ResetLaravelIgnitionListenerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0641b91..57e9d31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver]. +## UNRELEASED + +### Added + +- Integration with [spatie/laravel-ignition](https://github.com/spatie/laravel-ignition) is supported now [#88] +- Defining of environment variable `APP_RUNNING_IN_CONSOLE` in the worker "binary" file (it is required for the correct [`Application::runningInConsole`](https://bit.ly/3GPJTNL) method working) [#88] + +### Fixed + +- CLI running mode detection [#88] + +[#88]:https://github.com/spiral/roadrunner-laravel/pull/88 + ## v5.8.0 ### Added diff --git a/bin/rr-worker b/bin/rr-worker index 230a2e6..583b38d 100755 --- a/bin/rr-worker +++ b/bin/rr-worker @@ -7,6 +7,19 @@ declare(strict_types=1); \ini_set('display_errors', 'stderr'); +/* +|-------------------------------------------------------------------------- +| Setup Application Running Mode +|-------------------------------------------------------------------------- +| +| Saying to the Laravel "We are NOT running in the console" +| (https://bit.ly/3GPJTNL). +| +*/ + +$_ENV['APP_RUNNING_IN_CONSOLE'] = false; +\putenv('APP_RUNNING_IN_CONSOLE=false'); + /* |-------------------------------------------------------------------------- | Register The Auto Loader diff --git a/composer.json b/composer.json index 710e689..42b9a72 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "laravel/socialite": "^5.0", "laravel/telescope": "^4.5", "livewire/livewire": "^2.7", + "spatie/laravel-ignition": "^1.0", "mockery/mockery": "~1.3.3 || ^1.4.2", "phpstan/phpstan": "~1.4", "phpunit/phpunit": "^9.3.3" diff --git a/src/Defaults.php b/src/Defaults.php index 4e8f4e1..bfad7c0 100644 --- a/src/Defaults.php +++ b/src/Defaults.php @@ -57,6 +57,7 @@ public static function beforeLoopIteration(): array Listeners\ResetInertiaListener::class, // for Listeners\ResetZiggyListener::class, // for Listeners\ResetLivewireListener::class, // for + Listeners\ResetLaravelIgnitionListener::class, // for ]; } diff --git a/src/Dumper/Dumper.php b/src/Dumper/Dumper.php index 60f5f09..2d4d949 100644 --- a/src/Dumper/Dumper.php +++ b/src/Dumper/Dumper.php @@ -101,6 +101,10 @@ public function dd(...$vars) */ protected function ranUsingCLI(): bool { + if (Env::get('APP_RUNNING_IN_CONSOLE') === true) { + return true; + } + /** @link https://roadrunner.dev/docs/php-environment */ if (Env::get('RR_MODE') !== null && Env::get('RR_RELAY') !== null) { return false; diff --git a/src/Listeners/ResetLaravelIgnitionListener.php b/src/Listeners/ResetLaravelIgnitionListener.php new file mode 100644 index 0000000..8a72cae --- /dev/null +++ b/src/Listeners/ResetLaravelIgnitionListener.php @@ -0,0 +1,53 @@ +. + */ +class ResetLaravelIgnitionListener implements ListenerInterface +{ + use Traits\InvokerTrait; + + /** + * {@inheritdoc} + */ + public function handle($event): void + { + if (!\class_exists(\Spatie\LaravelIgnition\IgnitionServiceProvider::class)) { + return; + } + + if ($event instanceof WithApplication) { + $app = $event->application(); + + /** @see \Spatie\LaravelIgnition\IgnitionServiceProvider::resetFlareAndLaravelIgnition */ + foreach ([ + \Spatie\Ignition\Ignition::class, + \Spatie\LaravelIgnition\Support\SentReports::class, + \Spatie\LaravelIgnition\Recorders\DumpRecorder\DumpRecorder::class, + \Spatie\LaravelIgnition\Recorders\LogRecorder\LogRecorder::class, + \Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder::class, + \Spatie\LaravelIgnition\Recorders\JobRecorder\JobRecorder::class, + ] as $abstract) { + if ($app->resolved($abstract)) { + $instance = $app->make($abstract); + + if (!\is_object($instance)) { + continue; + } + + foreach (['reset', 'clear'] as $method_name) { + if ($this->invokeMethod($instance, $method_name)) { + break; + } + } + } + } + } + } +} diff --git a/tests/Unit/Listeners/ResetLaravelIgnitionListenerTest.php b/tests/Unit/Listeners/ResetLaravelIgnitionListenerTest.php new file mode 100644 index 0000000..2ccd84d --- /dev/null +++ b/tests/Unit/Listeners/ResetLaravelIgnitionListenerTest.php @@ -0,0 +1,63 @@ +spy(\Spatie\Ignition\Ignition::class, function (m\MockInterface $m) { + $m->shouldReceive('reset')->once(); + }); + + $this->spy(\Spatie\LaravelIgnition\Support\SentReports::class, function (m\MockInterface $m) { + $m->shouldReceive('clear')->once(); + }); + + $this->spy(Recorders\DumpRecorder\DumpRecorder::class, function (m\MockInterface $m) { + $m->shouldReceive('reset')->once(); + }); + + $this->spy(Recorders\LogRecorder\LogRecorder::class, function (m\MockInterface $m) { + $m->shouldReceive('reset')->once(); + }); + + $this->spy(Recorders\QueryRecorder\QueryRecorder::class, function (m\MockInterface $m) { + $m->shouldReceive('reset')->once(); + }); + + $this->spy(Recorders\JobRecorder\JobRecorder::class, function (m\MockInterface $m) { + $m->shouldReceive('reset')->once(); + }); + + /** @var m\MockInterface|WithApplication $event_mock */ + $event_mock = m::mock(WithApplication::class) + ->makePartial() + ->expects('application') + ->andReturn($this->app) + ->getMock(); + + $this->listenerFactory()->handle($event_mock); + } + + /** + * @return ResetLaravelIgnitionListener + */ + protected function listenerFactory(): ResetLaravelIgnitionListener + { + return new ResetLaravelIgnitionListener(); + } +} From c7531e8fa0f118d7dc249b655c58f37a4efd7cbf Mon Sep 17 00:00:00 2001 From: Paramtamtam <7326800+tarampampam@users.noreply.github.com> Date: Sat, 12 Feb 2022 15:49:37 +0500 Subject: [PATCH 2/6] fix CI --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 175647b..d07e539 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,7 +59,7 @@ jobs: # Docs: Date: Sat, 12 Feb 2022 15:50:53 +0500 Subject: [PATCH 3/6] fix CI --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d07e539..62ce510 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -63,7 +63,7 @@ jobs: # Docs: Date: Sat, 12 Feb 2022 16:04:13 +0500 Subject: [PATCH 4/6] WIP --- .github/workflows/tests.yml | 9 ++++++--- composer.json | 1 - phpstan.neon.dist | 2 ++ .../Unit/Listeners/ResetLaravelIgnitionListenerTest.php | 4 ++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 62ce510..d0b5442 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,11 +59,14 @@ jobs: # Docs: = 8.0 + run: composer require --dev spatie/laravel-ignition - name: Show most important package versions run: composer info | grep -e laravel -e spiral -e phpunit/phpunit -e phpstan/phpstan @@ -107,7 +110,7 @@ jobs: # Docs: markTestSkipped("Run 'composer require --dev spatie/laravel-ignition' for enabling this test"); + } + $this->spy(\Spatie\Ignition\Ignition::class, function (m\MockInterface $m) { $m->shouldReceive('reset')->once(); }); From e89504427f1e0c033c4507f1179bdfc3c1714385 Mon Sep 17 00:00:00 2001 From: Paramtamtam <7326800+tarampampam@users.noreply.github.com> Date: Sat, 12 Feb 2022 16:07:05 +0500 Subject: [PATCH 5/6] update phpstan config --- .github/workflows/tests.yml | 3 ++- phpstan.neon.dist | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d0b5442..7485798 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -65,7 +65,8 @@ jobs: # Docs: = 8.0 + - name: Install "spatie/laravel-ignition" package + if: ${{ startsWith(matrix.php, '8') && matrix.setup == 'basic' }} # only for php >= 8.0 run: composer require --dev spatie/laravel-ignition - name: Show most important package versions diff --git a/phpstan.neon.dist b/phpstan.neon.dist index d269771..238ecb4 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,7 +6,7 @@ parameters: - fixes - helpers - src - reportUnmatchedIgnoredErrors: true + reportUnmatchedIgnoredErrors: false ignoreErrors: - message: '#Call to protected method bootstrappers\(\) of class.+Kernel#' paths: [src/Application/Factory.php] From eed6c3992f325d4bd33f3ad716631069d3b85369 Mon Sep 17 00:00:00 2001 From: Paramtamtam <7326800+tarampampam@users.noreply.github.com> Date: Sat, 12 Feb 2022 16:10:07 +0500 Subject: [PATCH 6/6] WIP --- .../ResetLaravelIgnitionListener.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Listeners/ResetLaravelIgnitionListener.php b/src/Listeners/ResetLaravelIgnitionListener.php index 8a72cae..4534262 100644 --- a/src/Listeners/ResetLaravelIgnitionListener.php +++ b/src/Listeners/ResetLaravelIgnitionListener.php @@ -13,6 +13,18 @@ class ResetLaravelIgnitionListener implements ListenerInterface { use Traits\InvokerTrait; + /** + * @var array + */ + private static array $abstracts = [ + \Spatie\Ignition\Ignition::class, + \Spatie\LaravelIgnition\Support\SentReports::class, + \Spatie\LaravelIgnition\Recorders\DumpRecorder\DumpRecorder::class, + \Spatie\LaravelIgnition\Recorders\LogRecorder\LogRecorder::class, + \Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder::class, + \Spatie\LaravelIgnition\Recorders\JobRecorder\JobRecorder::class, + ]; + /** * {@inheritdoc} */ @@ -26,14 +38,7 @@ public function handle($event): void $app = $event->application(); /** @see \Spatie\LaravelIgnition\IgnitionServiceProvider::resetFlareAndLaravelIgnition */ - foreach ([ - \Spatie\Ignition\Ignition::class, - \Spatie\LaravelIgnition\Support\SentReports::class, - \Spatie\LaravelIgnition\Recorders\DumpRecorder\DumpRecorder::class, - \Spatie\LaravelIgnition\Recorders\LogRecorder\LogRecorder::class, - \Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder::class, - \Spatie\LaravelIgnition\Recorders\JobRecorder\JobRecorder::class, - ] as $abstract) { + foreach (self::$abstracts as $abstract) { if ($app->resolved($abstract)) { $instance = $app->make($abstract);