Skip to content

[10.x] Fixes CommandStarting and CommandFinished not working #1272

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

Merged
merged 3 commits into from
Oct 13, 2023
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
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ public function flush()
$this->router = null;
$this->dispatcher = null;
static::$instance = null;
static::$aliasesRegistered = false;
}

/**
Expand Down
51 changes: 50 additions & 1 deletion src/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
namespace Laravel\Lumen\Console;

use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Scheduling\ScheduleRunCommand;
use Illuminate\Contracts\Console\Kernel as KernelContract;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\Request;
use Laravel\Lumen\Application;
use Laravel\Lumen\Exceptions\Handler;
use RuntimeException;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Throwable;

class Kernel implements KernelContract
Expand All @@ -22,6 +29,13 @@ class Kernel implements KernelContract
*/
protected $app;

/**
* The Symfony event dispatcher implementation.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface|null
*/
protected $symfonyDispatcher;

/**
* The Artisan application instance.
*
Expand Down Expand Up @@ -55,6 +69,8 @@ public function __construct(Application $app)

if ($this->app->runningInConsole()) {
$this->setRequestForConsole($this->app);
} else {
$this->rerouteSymfonyCommandEvents();
}

$this->app->prepareForConsoleCommand($this->aliases);
Expand Down Expand Up @@ -87,6 +103,34 @@ protected function setRequestForConsole(Application $app)
));
}

/**
* Re-route the Symfony command events to their Laravel counterparts.
*
* @internal
*
* @return $this
*/
public function rerouteSymfonyCommandEvents()
{
if (is_null($this->symfonyDispatcher)) {
$this->symfonyDispatcher = new EventDispatcher;

$this->symfonyDispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
$this->app[Dispatcher::class]->dispatch(
new CommandStarting($event->getCommand()->getName(), $event->getInput(), $event->getOutput())
);
});

$this->symfonyDispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) {
$this->app[Dispatcher::class]->dispatch(
new CommandFinished($event->getCommand()->getName(), $event->getInput(), $event->getOutput(), $event->getExitCode())
);
});
}

return $this;
}

/**
* Define the application's command schedule.
*
Expand Down Expand Up @@ -212,9 +256,14 @@ public function output()
protected function getArtisan()
{
if (is_null($this->artisan)) {
return $this->artisan = (new Artisan($this->app, $this->app->make('events'), $this->app->version()))
$this->artisan = (new Artisan($this->app, $this->app->make('events'), $this->app->version()))
->resolveCommands($this->getCommands())
->setContainerCommandLoader();

if ($this->symfonyDispatcher instanceof EventDispatcher) {
$this->artisan->setDispatcher($this->symfonyDispatcher);
$this->artisan->setSignalsToDispatchEvent();
}
}

return $this->artisan;
Expand Down
42 changes: 42 additions & 0 deletions tests/Console/KernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
use Laravel\Lumen\Application;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;

class KernelTest extends \Laravel\Lumen\Testing\TestCase
{
/**
* Creates the application.
*
* Needs to be implemented by subclasses.
*
* @return \Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
$app = new Application();

$app->configure('app');

$app->singleton(ExceptionHandlerContract::class, fn () => new ExceptionHandler());
$app->singleton(ConsoleKernelContract::class, function () use ($app) {
return tap(new ConsoleKernel($app), function ($kernel) {
$kernel->rerouteSymfonyCommandEvents();
});
});

return $app;
}

public function testItCanRerouteToSymfonyEvent()
{
$this->expectsEvents([CommandStarting::class, CommandFinished::class]);

$this->artisan('cache:forget', ['key' => 'lumen']);
}
}
6 changes: 6 additions & 0 deletions tests/FullApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Facade;
use Illuminate\View\ViewServiceProvider;
use Laravel\Lumen\Application;
use Laravel\Lumen\Console\ConsoleServiceProvider;
Expand All @@ -16,6 +17,11 @@

class FullApplicationTest extends TestCase
{
protected function setUp(): void
{
Facade::clearResolvedInstances();
}

protected function tearDown(): void
{
m::close();
Expand Down