Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

laravel-ignition integration and CLI mode detection #88

Merged
merged 6 commits into from
Feb 12, 2022
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: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ jobs: # Docs: <https://help.github.com/en/articles/workflow-syntax-for-github-ac
if: matrix.setup == 'basic'
run: composer update --prefer-dist --no-interaction --no-progress --ansi

- 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
run: composer info | grep -e laravel -e spiral -e phpunit/phpunit -e phpstan/phpstan

Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions bin/rr-worker
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ parameters:
- fixes
- helpers
- src
reportUnmatchedIgnoredErrors: true
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- message: '#Call to protected method bootstrappers\(\) of class.+Kernel#'
paths: [src/Application/Factory.php]
- message: '#Class Spatie\\.+Ignition.* not found#'
paths: [src/Listeners/ResetLaravelIgnitionListener.php]
1 change: 1 addition & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static function beforeLoopIteration(): array
Listeners\ResetInertiaListener::class, // for <https://github.com/inertiajs/inertia-laravel>
Listeners\ResetZiggyListener::class, // for <https://github.com/tighten/ziggy>
Listeners\ResetLivewireListener::class, // for <https://github.com/livewire/livewire>
Listeners\ResetLaravelIgnitionListener::class, // for <https://github.com/spatie/laravel-ignition>
];
}

Expand Down
4 changes: 4 additions & 0 deletions src/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions src/Listeners/ResetLaravelIgnitionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Listeners;

use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;

/**
* Target package: <https://github.com/spatie/laravel-ignition/>.
*/
class ResetLaravelIgnitionListener implements ListenerInterface
{
use Traits\InvokerTrait;

/**
* @var array<class-string>
*/
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}
*/
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 (self::$abstracts 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;
}
}
}
}
}
}
}
67 changes: 67 additions & 0 deletions tests/Unit/Listeners/ResetLaravelIgnitionListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;

use Mockery as m;
use Spatie\LaravelIgnition\Recorders;
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
use Spiral\RoadRunnerLaravel\Listeners\ResetLaravelIgnitionListener;

/**
* @covers \Spiral\RoadRunnerLaravel\Listeners\ResetLaravelIgnitionListener
*/
class ResetLaravelIgnitionListenerTest extends AbstractListenerTestCase
{
/**
* {@inheritdoc}
*/
public function testHandle(): void
{
if (!\class_exists(\Spatie\LaravelIgnition\IgnitionServiceProvider::class)) {
$this->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();
});

$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();
}
}