Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.2] [FrameworkBundle] Add events to console commands #3889

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

Closed
wants to merge 15 commits into from
Closed
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
17 changes: 15 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Bundle\FrameworkBundle\Console\ConsoleEvents;
use Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleEvent;
use Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleTerminateEvent;

/**
* Application.
Expand Down Expand Up @@ -75,7 +78,17 @@ public function doRun(InputInterface $input, OutputInterface $output)
return 0;
}

return parent::doRun($input, $output);
$dispatcher = $this->kernel->getContainer()->get('event_dispatcher');

$initEvent = new ConsoleEvent($input, $output);
$dispatcher->dispatch(ConsoleEvents::INIT, $initEvent);

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

$terminateEvent = new ConsoleTerminateEvent($input, $output, $exitCode);
$dispatcher->dispatch(ConsoleEvents::TERMINATE, $terminateEvent);

return $exitCode;
}

protected function registerCommands()
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Console/ConsoleEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Console;

/**
* Contains all events thrown during Console commands execution
*
* @author Francesco Levorato <[email protected]>
*
*/
final class ConsoleEvents
{
/**
* The INIT event allows you to attach listeners before any command is
* executed by the console. It also allows you to modify the input and output
* before they are handled to the command.
*
* The event listener method receives a \Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleEvent
* instance.
*
* @var string
*/
const INIT = 'console.init';

/**
* The TERMINATE event allows you to attach listeners after a command is
* executed by the console.
*
* The event listener method receives a \Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleTerminateEvent
* instance.
*
* @var string
*/
const TERMINATE = 'console.terminate';

Copy link
Member

Choose a reason for hiding this comment

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

extra line here

}
56 changes: 56 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Console\Event;

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

/**
* Allows to inspect input and output of a command.
*
* @author Francesco Levorato <[email protected]>
*/
class ConsoleEvent extends Event
{

private $input;

private $output;

public function __construct(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
}

/**
* Returns the input object
*
* @return InputInterface
*/
public function getInput()
{
return $this->input;
}

/**
* Returns the output object
*
* @return OutputInterface
*/
public function getOutput()
{
return $this->output;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Console\Event;

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

/**
* Allows to receive the exit code of a command after its execution.
*
* @author Francesco Levorato <[email protected]>
*/
class ConsoleTerminateEvent extends ConsoleEvent
{

/**
* The exit code of the command.
*
* @var integer
*/
private $exitCode;

public function __construct(InputInterface $input, OutputInterface $output, $exitCode)
{
parent::__construct($input, $output);
$this->exitCode = $exitCode;
}

/**
* Returns the exit code.
*
* @return integer
*/
public function getExitCode()
{
return $this->exitCode;
}

Copy link
Member

Choose a reason for hiding this comment

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

this one should probably extend from the ConsoleInitEvent (which should then be renamed to ConsoleEvent) to give access to the input and output too

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ private function getKernel(array $bundles)
->method('getBundles')
->will($this->returnValue($bundles))
;
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

$container
->expects($this->any())
->method('get')
->with($this->equalTo('event_dispatcher'))
->will($this->returnValue($eventDispatcher));
$kernel
->expects($this->any())
->method('getContainer')
->will($this->returnValue($container))
;

return $kernel;
}
Expand Down