Skip to content

Commit

Permalink
Add console events
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot committed Jul 31, 2017
1 parent 7c55be1 commit b26a9fa
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Illuminate/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Illuminate\Contracts\Console\Application as ApplicationContract;
Expand Down Expand Up @@ -37,6 +39,13 @@ class Application extends SymfonyApplication implements ApplicationContract
*/
protected static $bootstrappers = [];

/**
* The Event Dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

/**
* Create a new Artisan console application.
*
Expand All @@ -50,14 +59,35 @@ public function __construct(Container $laravel, Dispatcher $events, $version)
parent::__construct('Laravel Framework', $version);

$this->laravel = $laravel;
$this->events = $events;
$this->setAutoExit(false);
$this->setCatchExceptions(false);

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

$this->bootstrap();
}

/**
* {@inheritdoc}
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{
$commandName = $this->getCommandName($input);

$this->events->fire(
new Events\CommandStarting($commandName, $input)
);

$exitCode = parent::run($input, $output);

$this->events->fire(
new Events\CommandTerminating($commandName, $input, $exitCode)
);

return $exitCode;
}

/**
* Determine the proper PHP executable.
*
Expand Down
35 changes: 35 additions & 0 deletions src/Illuminate/Console/Events/CommandStarting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Console\Events;

use Symfony\Component\Console\Input\InputInterface;

class CommandStarting
{
/**
* The command name.
*
* @var string
*/
public $command;

/**
* The console input.
*
* @var string
*/
public $input;

/**
* Create a new event instance.
*
* @param string $command
* @param \Symfony\Component\Console\Input\InputInterface $input
* @return void
*/
public function __construct($command, InputInterface $input)
{
$this->command = $command;
$this->input = $input;
}
}
44 changes: 44 additions & 0 deletions src/Illuminate/Console/Events/CommandTerminating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Illuminate\Console\Events;

use Symfony\Component\Console\Input\InputInterface;

class CommandTerminating
{
/**
* The command name.
*
* @var string
*/
public $command;

/**
* The console input.
*
* @var string
*/
public $input;

/**
* The command exit code.
*
* @var int
*/
public $exitCode;

/**
* Create a new event instance.
*
* @param string $command
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param int $exitCode
* @return void
*/
public function __construct($command, InputInterface $input, $exitCode)
{
$this->command = $command;
$this->input = $input;
$this->exitCode = $exitCode;
}
}

0 comments on commit b26a9fa

Please sign in to comment.