Skip to content

Commit 5d53c36

Browse files
authored
laravel-ignition integration and CLI mode detection (#88)
1 parent dfd5bf6 commit 5d53c36

File tree

8 files changed

+163
-1
lines changed

8 files changed

+163
-1
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ jobs: # Docs: <https://help.github.com/en/articles/workflow-syntax-for-github-ac
6565
if: matrix.setup == 'basic'
6666
run: composer update --prefer-dist --no-interaction --no-progress --ansi
6767

68+
- name: Install "spatie/laravel-ignition" package
69+
if: ${{ startsWith(matrix.php, '8') && matrix.setup == 'basic' }} # only for php >= 8.0
70+
run: composer require --dev spatie/laravel-ignition
71+
6872
- name: Show most important package versions
6973
run: composer info | grep -e laravel -e spiral -e phpunit/phpunit -e phpstan/phpstan
7074

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## UNRELEASED
8+
9+
### Added
10+
11+
- Integration with [spatie/laravel-ignition](https://github.com/spatie/laravel-ignition) is supported now [#88]
12+
- 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]
13+
14+
### Fixed
15+
16+
- CLI running mode detection [#88]
17+
18+
[#88]:https://github.com/spiral/roadrunner-laravel/pull/88
19+
720
## v5.8.0
821

922
### Added

bin/rr-worker

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ declare(strict_types=1);
77

88
\ini_set('display_errors', 'stderr');
99

10+
/*
11+
|--------------------------------------------------------------------------
12+
| Setup Application Running Mode
13+
|--------------------------------------------------------------------------
14+
|
15+
| Saying to the Laravel "We are NOT running in the console"
16+
| (https://bit.ly/3GPJTNL).
17+
|
18+
*/
19+
20+
$_ENV['APP_RUNNING_IN_CONSOLE'] = false;
21+
\putenv('APP_RUNNING_IN_CONSOLE=false');
22+
1023
/*
1124
|--------------------------------------------------------------------------
1225
| Register The Auto Loader

phpstan.neon.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ parameters:
66
- fixes
77
- helpers
88
- src
9-
reportUnmatchedIgnoredErrors: true
9+
reportUnmatchedIgnoredErrors: false
1010
ignoreErrors:
1111
- message: '#Call to protected method bootstrappers\(\) of class.+Kernel#'
1212
paths: [src/Application/Factory.php]
13+
- message: '#Class Spatie\\.+Ignition.* not found#'
14+
paths: [src/Listeners/ResetLaravelIgnitionListener.php]

src/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static function beforeLoopIteration(): array
5757
Listeners\ResetInertiaListener::class, // for <https://github.com/inertiajs/inertia-laravel>
5858
Listeners\ResetZiggyListener::class, // for <https://github.com/tighten/ziggy>
5959
Listeners\ResetLivewireListener::class, // for <https://github.com/livewire/livewire>
60+
Listeners\ResetLaravelIgnitionListener::class, // for <https://github.com/spatie/laravel-ignition>
6061
];
6162
}
6263

src/Dumper/Dumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public function dd(...$vars)
101101
*/
102102
protected function ranUsingCLI(): bool
103103
{
104+
if (Env::get('APP_RUNNING_IN_CONSOLE') === true) {
105+
return true;
106+
}
107+
104108
/** @link https://roadrunner.dev/docs/php-environment */
105109
if (Env::get('RR_MODE') !== null && Env::get('RR_RELAY') !== null) {
106110
return false;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
8+
9+
/**
10+
* Target package: <https://github.com/spatie/laravel-ignition/>.
11+
*/
12+
class ResetLaravelIgnitionListener implements ListenerInterface
13+
{
14+
use Traits\InvokerTrait;
15+
16+
/**
17+
* @var array<class-string>
18+
*/
19+
private static array $abstracts = [
20+
\Spatie\Ignition\Ignition::class,
21+
\Spatie\LaravelIgnition\Support\SentReports::class,
22+
\Spatie\LaravelIgnition\Recorders\DumpRecorder\DumpRecorder::class,
23+
\Spatie\LaravelIgnition\Recorders\LogRecorder\LogRecorder::class,
24+
\Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder::class,
25+
\Spatie\LaravelIgnition\Recorders\JobRecorder\JobRecorder::class,
26+
];
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function handle($event): void
32+
{
33+
if (!\class_exists(\Spatie\LaravelIgnition\IgnitionServiceProvider::class)) {
34+
return;
35+
}
36+
37+
if ($event instanceof WithApplication) {
38+
$app = $event->application();
39+
40+
/** @see \Spatie\LaravelIgnition\IgnitionServiceProvider::resetFlareAndLaravelIgnition */
41+
foreach (self::$abstracts as $abstract) {
42+
if ($app->resolved($abstract)) {
43+
$instance = $app->make($abstract);
44+
45+
if (!\is_object($instance)) {
46+
continue;
47+
}
48+
49+
foreach (['reset', 'clear'] as $method_name) {
50+
if ($this->invokeMethod($instance, $method_name)) {
51+
break;
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Mockery as m;
8+
use Spatie\LaravelIgnition\Recorders;
9+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
10+
use Spiral\RoadRunnerLaravel\Listeners\ResetLaravelIgnitionListener;
11+
12+
/**
13+
* @covers \Spiral\RoadRunnerLaravel\Listeners\ResetLaravelIgnitionListener
14+
*/
15+
class ResetLaravelIgnitionListenerTest extends AbstractListenerTestCase
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function testHandle(): void
21+
{
22+
if (!\class_exists(\Spatie\LaravelIgnition\IgnitionServiceProvider::class)) {
23+
$this->markTestSkipped("Run 'composer require --dev spatie/laravel-ignition' for enabling this test");
24+
}
25+
26+
$this->spy(\Spatie\Ignition\Ignition::class, function (m\MockInterface $m) {
27+
$m->shouldReceive('reset')->once();
28+
});
29+
30+
$this->spy(\Spatie\LaravelIgnition\Support\SentReports::class, function (m\MockInterface $m) {
31+
$m->shouldReceive('clear')->once();
32+
});
33+
34+
$this->spy(Recorders\DumpRecorder\DumpRecorder::class, function (m\MockInterface $m) {
35+
$m->shouldReceive('reset')->once();
36+
});
37+
38+
$this->spy(Recorders\LogRecorder\LogRecorder::class, function (m\MockInterface $m) {
39+
$m->shouldReceive('reset')->once();
40+
});
41+
42+
$this->spy(Recorders\QueryRecorder\QueryRecorder::class, function (m\MockInterface $m) {
43+
$m->shouldReceive('reset')->once();
44+
});
45+
46+
$this->spy(Recorders\JobRecorder\JobRecorder::class, function (m\MockInterface $m) {
47+
$m->shouldReceive('reset')->once();
48+
});
49+
50+
/** @var m\MockInterface|WithApplication $event_mock */
51+
$event_mock = m::mock(WithApplication::class)
52+
->makePartial()
53+
->expects('application')
54+
->andReturn($this->app)
55+
->getMock();
56+
57+
$this->listenerFactory()->handle($event_mock);
58+
}
59+
60+
/**
61+
* @return ResetLaravelIgnitionListener
62+
*/
63+
protected function listenerFactory(): ResetLaravelIgnitionListener
64+
{
65+
return new ResetLaravelIgnitionListener();
66+
}
67+
}

0 commit comments

Comments
 (0)