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.5] Add console events #20298

Merged
merged 5 commits into from
Jul 31, 2017
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: 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, $output)
);

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

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

return $exitCode;
}

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

namespace Illuminate\Console\Events;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

/**
* The console input.
*
* @var \Symfony\Component\Console\Input\InputInterface|null
*/
public $input;

/**
* The command output.
*
* @var \Symfony\Component\Console\Output\OutputInterface|null
*/
protected $output;

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

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

namespace Illuminate\Console\Events;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

/**
* The console input.
*
* @var \Symfony\Component\Console\Input\InputInterface|null
*/
public $input;

/**
* The command output.
*
* @var \Symfony\Component\Console\Output\OutputInterface|null
*/
protected $output;

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