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

[5.3] Use bootstrappers instead of events #16012

Merged
merged 1 commit into from
Oct 22, 2016
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
32 changes: 32 additions & 0 deletions src/Illuminate/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class Application extends SymfonyApplication implements ApplicationContract
*/
protected $lastOutput;

/**
* The console application bootstrappers.
*
* @var array
*/
protected static $bootstrappers = [];

/**
* Create a new Artisan console application.
*
Expand All @@ -44,6 +51,20 @@ public function __construct(Container $laravel, Dispatcher $events, $version)
$this->setCatchExceptions(false);

$events->fire(new Events\ArtisanStarting($this));

$this->bootstrap();
}

/**
* Bootstrap the console application.
*
* @return void
*/
protected function bootstrap()
{
foreach (static::$bootstrappers as $bootstrapper) {
$bootstrapper($this);
}
}

/**
Expand Down Expand Up @@ -169,4 +190,15 @@ public function getLaravel()
{
return $this->laravel;
}

/**
* Register an application starting bootstrapper.
*
* @param \Closure $callback
* @return void
*/
public static function starting(\Closure $callback)
{
static::$bootstrappers[] = $callback;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should automatically run $callback when calling starting() after Illuminate\Console\Events\ArtisanStarting has been called.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Illuminate\Foundation\Console\Kernel::registerCommand() will resolve Illuminate\Console\Application when it call getArtisan(), if any part of the code execute this too early (before all events registered) it's not going to be called again.

}
5 changes: 2 additions & 3 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Events\ArtisanStarting;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Console\Kernel as KernelContract;
use Symfony\Component\Debug\Exception\FatalThrowableError;
Expand Down Expand Up @@ -181,8 +180,8 @@ public function command($signature, Closure $callback)
{
$command = new ClosureCommand($signature, $callback);

$this->app['events']->listen(ArtisanStarting::class, function ($event) use ($command) {
$event->artisan->add($command);
Artisan::starting(function ($artisan) use ($command) {
$artisan->add($command);
});

return $command;
Expand Down
11 changes: 3 additions & 8 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Support;

use Illuminate\Console\Events\ArtisanStarting;
use Illuminate\Console\Application as Artisan;

abstract class ServiceProvider
{
Expand Down Expand Up @@ -176,13 +176,8 @@ public function commands($commands)
{
$commands = is_array($commands) ? $commands : func_get_args();

// To register the commands with Artisan, we will grab each of the arguments
// passed into the method and listen for Artisan "start" event which will
// give us the Artisan console instance which we will give commands to.
$events = $this->app['events'];

$events->listen(ArtisanStarting::class, function ($event) use ($commands) {
$event->artisan->resolveCommands($commands);
Artisan::starting(function ($artisan) use ($commands) {
$artisan->resolveCommands($commands);
});
}

Expand Down