Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File watching in development environments #458

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 config/nativephp.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
],
],

'on_php_file_change' => [
\Native\Laravel\Pipelines\ProjectFileChanged\RestartQueueWorkers::class,
],

/**
* Define your own scripts to run before and after the build process.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Events/App/ProjectFileChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Native\Laravel\Events\App;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;

class ProjectFileChanged implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets;

public function __construct(public readonly string $relativePath) {}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
19 changes: 19 additions & 0 deletions src/Listeners/ProjectFileChangedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Native\Laravel\Listeners;

use Illuminate\Support\Facades\Pipeline;
use Native\Laravel\Events\App\ProjectFileChanged;
use Webmozart\Assert\Assert;

class ProjectFileChangedListener
{
public function handle(ProjectFileChanged $event): void
{
foreach ($pipelines = config('nativephp.on_php_file_change') as $class) {
Assert::classExists($class, "Class {$class} does not exist");
}

Pipeline::send($event)->through($pipelines)->thenReturn();
}
}
5 changes: 5 additions & 0 deletions src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Native\Laravel;

use Illuminate\Console\Application;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Application as Foundation;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
Expand All @@ -19,9 +20,11 @@
use Native\Laravel\Contracts\QueueWorker as QueueWorkerContract;
use Native\Laravel\Contracts\WindowManager as WindowManagerContract;
use Native\Laravel\DTOs\QueueConfig;
use Native\Laravel\Events\App\ProjectFileChanged;
use Native\Laravel\Events\EventWatcher;
use Native\Laravel\Exceptions\Handler;
use Native\Laravel\GlobalShortcut as GlobalShortcutImplementation;
use Native\Laravel\Listeners\ProjectFileChangedListener;
use Native\Laravel\Logging\LogWatcher;
use Native\Laravel\PowerMonitor as PowerMonitorImplementation;
use Native\Laravel\Windows\WindowManager as WindowManagerImplementation;
Expand Down Expand Up @@ -97,6 +100,8 @@ public function packageRegistered()

public function bootingPackage()
{
$this->app->make(Dispatcher::class)->listen(ProjectFileChanged::class, ProjectFileChangedListener::class);

if (config('nativephp-internal.running')) {
$this->rewriteDatabase();
}
Expand Down
26 changes: 26 additions & 0 deletions src/Pipelines/ProjectFileChanged/RestartQueueWorkers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Native\Laravel\Pipelines\ProjectFileChanged;

use Closure;
use Native\Laravel\Contracts\QueueWorker;
use Native\Laravel\DTOs\QueueConfig;
use Native\Laravel\Events\App\ProjectFileChanged;

class RestartQueueWorkers
{
public function __construct(
private readonly QueueWorker $queueWorker,
) {}

public function __invoke(ProjectFileChanged $event, Closure $next): ProjectFileChanged
{
$queueConfigs = QueueConfig::fromConfigArray(config('nativephp.queue_workers'));

foreach ($queueConfigs as $queueConfig) {
$this->queueWorker->down($queueConfig->alias);
}

return $next($event);
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Fakes/FakePipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Native\Laravel\Tests\Fixtures\Fakes;

use Closure;

class FakePipeline
{
public bool $handled = false;

public mixed $carry;

public function handle(mixed $carry, Closure $next)
{
$this->handled = true;
$this->carry = $carry;

return $next($carry);
}
}
35 changes: 35 additions & 0 deletions tests/Listeners/ProjectFileChangedListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Contracts\Events\Dispatcher;
use Native\Laravel\Events\App\ProjectFileChanged;
use Native\Laravel\Tests\Fixtures\Fakes\FakePipeline;
use Webmozart\Assert\InvalidArgumentException as WebmozartInvalidArgumentException;

it('listens for the project file changed event and runs configured pipelines', function () {
app()->singleton(FakePipeline::class, fn () => $fake = new FakePipeline);

config(['nativephp.on_php_file_change' => [
FakePipeline::class,
]]);

app(Dispatcher::class)->dispatch(new ProjectFileChanged('some/file.php'));

expect(app(FakePipeline::class)->handled)->toBeTrue();
expect(app(FakePipeline::class)->carry)->toBeInstanceOf(ProjectFileChanged::class);
});

it('rejects nonexistent classes', function () {
config(['nativephp.on_php_file_change' => [
'definitely-not-a-class-fqcn',
]]);

try {
app(Dispatcher::class)->dispatch(new ProjectFileChanged('some/file.php'));
} catch (WebmozartInvalidArgumentException $e) {
expect($e->getMessage())->toBe('Class definitely-not-a-class-fqcn does not exist');

return;
}

$this->fail('Expected an exception to be thrown');
});
22 changes: 22 additions & 0 deletions tests/Pipelines/ProjectFileChanged/RestartQueueWorkersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Pipeline;
use Native\Laravel\Events\App\ProjectFileChanged;
use Native\Laravel\Facades\QueueWorker;
use Native\Laravel\Pipelines\ProjectFileChanged\RestartQueueWorkers;

it('restarts configured queue workers', function () {
QueueWorker::fake();

config(['nativephp.queue_workers' => [
'something' => [],
'another' => [],
]]);

Pipeline::send(new ProjectFileChanged('some/file.php'))
->through([RestartQueueWorkers::class])
->thenReturn();

QueueWorker::assertDown('something');
QueueWorker::assertDown('another');
});
Loading