1
0
mirror of synced 2025-02-20 22:23:14 +03:00

Merge pull request #1069 from anho/reuse-console-app

added method to be able to reuse the console application
This commit is contained in:
Marco Pivetta 2014-08-04 16:17:11 +02:00
commit 723529ffff
3 changed files with 62 additions and 1 deletions

View File

@ -507,3 +507,22 @@ defined ones) is possible through the command:
new \MyProject\Tools\Console\Commands\AnotherCommand(),
new \MyProject\Tools\Console\Commands\OneMoreCommand(),
));
Re-use console application
--------------------------
You are also able to retrieve and re-use the default console application.
Just call ``ConsoleRunner::createApplication(...)`` with an appropriate
HelperSet, like it is described in the configuration section.
.. code-block:: php
<?php
// Retrieve default console application
$cli = ConsoleRunner::createApplication($helperSet);
// Runs console application
$cli->run();

View File

@ -55,13 +55,29 @@ class ConsoleRunner
* @return void
*/
static public function run(HelperSet $helperSet, $commands = array())
{
$cli = self::createApplication($helperSet, $commands);
$cli->run();
}
/**
* Creates a console application with the given helperset and
* optional commands.
*
* @param \Symfony\Component\Console\Helper\HelperSet $helperSet
* @param array $commands
*
* @return \Symfony\Component\Console\Application
*/
static public function createApplication(HelperSet $helperSet, $commands = array())
{
$cli = new Application('Doctrine Command Line Interface', Version::VERSION);
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);
self::addCommands($cli);
$cli->addCommands($commands);
$cli->run();
return $cli;
}
/**

View File

@ -0,0 +1,26 @@
<?php
namespace Doctrine\Tests\ORM\Tools\Console;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Version;
use Doctrine\Tests\DoctrineTestCase;
use Symfony\Component\Console\Helper\HelperSet;
/**
* @group DDC-3186
*
* @covers \Doctrine\ORM\Tools\Console\ConsoleRunner
*/
class ConsoleRunnerTest extends DoctrineTestCase
{
public function testCreateApplication()
{
$helperSet = new HelperSet();
$app = ConsoleRunner::createApplication($helperSet);
$this->assertInstanceOf('Symfony\Component\Console\Application', $app);
$this->assertSame($helperSet, $app->getHelperSet());
$this->assertEquals(Version::VERSION, $app->getVersion());
}
}