Skip to content

Commit

Permalink
Use bootstrappers instead of events
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Oct 19, 2016
1 parent bec2d79 commit 1a09306
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
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;
}
}
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

0 comments on commit 1a09306

Please sign in to comment.