Skip to content

Commit

Permalink
Merge pull request #1069 from anho/reuse-console-app
Browse files Browse the repository at this point in the history
added method to be able to reuse the console application
  • Loading branch information
Ocramius committed Aug 4, 2014
2 parents 2ae8538 + a76506c commit 723529f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
19 changes: 19 additions & 0 deletions docs/en/reference/tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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();
18 changes: 17 additions & 1 deletion lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,29 @@ public static function createHelperSet(EntityManagerInterface $entityManager)
* @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;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/Console/ConsoleRunnerTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 723529f

Please sign in to comment.