diff --git a/src/Symfony/Component/Console/SingleCommandApplication.php b/src/Symfony/Component/Console/SingleCommandApplication.php new file mode 100644 index 0000000000000..a4326700fe132 --- /dev/null +++ b/src/Symfony/Component/Console/SingleCommandApplication.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; + +/** + * Application providing access to just one command. + * + * When a console application only consists of one + * command, having to specify this command's name as first + * argument is superfluous. + * This class simplifies creating and using this + * kind of applications. + * + * Usage: + * + * $cmd = new SimpleCommand(); + * $app = new SingleCommandApplication($cmd, '1.2'); + * $app->run(); + * + * @author Stefaan Lippens + */ +class SingleCommandApplication extends Application +{ + /** + * Name of the single accessible command of this application + * @var string + */ + private $commandName; + + /** + * Constructor to build a "single command" application, given a command. + * + * The application will adopt the same name as the command. + * + * @param Command $command The single (accessible) command for this application + * @param string $version The version of the application + */ + public function __construct(Command $command, $version = 'UNKNOWN') + { + parent::__construct($command->getName(), $version); + + // Add the given command as single (accessible) command. + $this->add($command); + $this->commandName = $command->getName(); + + // Override the Application's definition so that it does not + // require a command name as first argument. + $this->getDefinition()->setArguments(); + } + + /** + * {@inheritdoc} + */ + protected function getCommandName(InputInterface $input) + { + return $this->commandName; + } + + /** + * Adds a command object. + * + * This function overrides (public) Application::add() + * but should should only be used internally. + * Will raise \LogicException when called + * after the single accessible command is set up + * (from the constructor). + * + * @param Command $command A Command object + * + * @return Command The registered command + * + * @throws \LogicException + */ + public function add(Command $command) + { + // Fail if we already set up the single accessible command. + if ($this->commandName) { + throw new \LogicException("You should not add additional commands to a SingleCommandApplication instance."); + } + + return parent::add($command); + } +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/FooScaCommand.php b/src/Symfony/Component/Console/Tests/Fixtures/FooScaCommand.php new file mode 100644 index 0000000000000..865e175c0de1c --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/FooScaCommand.php @@ -0,0 +1,38 @@ +setName('foosca') + ->setDescription('The foosca command'); + $this->addArgument( + 'items', + InputArgument::IS_ARRAY, + 'Items to process' + ); + $this->addOption( + 'bar', + 'b', + InputOption::VALUE_NONE, + 'Enable barring' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $bar = $input->getOption('bar'); + $output->writeln('FooSca' . ($bar ? ' (barred)': ' (basic)')); + + foreach ($input->getArgument('items') as $item) { + $output->writeln('Item: ' . $item); + } + } +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_run_foosca_help.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_run_foosca_help.txt new file mode 100644 index 0000000000000..8de558f063cae --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_run_foosca_help.txt @@ -0,0 +1,16 @@ +Usage: + foosca [-b|--bar] [items1] ... [itemsN] + +Arguments: + items Items to process + +Options: + --bar (-b) Enable barring + --help (-h) Display this help message. + --quiet (-q) Do not output any message. + --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug + --version (-V) Display this application version. + --ansi Force ANSI output. + --no-ansi Disable ANSI output. + --no-interaction (-n) Do not ask any interactive question. + diff --git a/src/Symfony/Component/Console/Tests/SingleCommandApplicationTest.php b/src/Symfony/Component/Console/Tests/SingleCommandApplicationTest.php new file mode 100644 index 0000000000000..446486ab24705 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/SingleCommandApplicationTest.php @@ -0,0 +1,138 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests; + +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Console\SingleCommandApplication; + +class SingleCommandApplicationTest extends \PHPUnit_Framework_TestCase +{ + protected static $fixturesPath; + + public static function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__ . '/Fixtures/'); + require_once self::$fixturesPath.'/FooCommand.php'; + require_once self::$fixturesPath . '/FooScaCommand.php'; + } + + public function testConstructor() + { + $application = new SingleCommandApplication(new \FooScaCommand(), 'v2.3'); + $this->assertEquals( + 'foosca', + $application->getName(), + '__construct() takes the application name as its first argument' + ); + $this->assertEquals( + 'v2.3', + $application->getVersion(), + '__construct() takes the application version as its second argument' + ); + $this->assertEquals( + array('help', 'list', 'foosca'), + array_keys($application->all()), + '__construct() registered the help and list commands by default' + ); + } + + /** + * @dataProvider provideRunData + */ + public function testRun(InputInterface $input, $expectedOutput, $expectedStatusCode=0) + { + // Set up application. + $application = new SingleCommandApplication(new \FooScaCommand(), '1.234'); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + + // Set up output for application to render to. + $stream = fopen('php://memory', 'w', false); + $output = new StreamOutput($stream); + + // Run application with given input. + $statusCode = $application->run($input, $output); + + // Get generated output (and normalize newlines) + rewind($stream); + $display = stream_get_contents($stream); + $display = str_replace(PHP_EOL, "\n", $display); + + $this->assertEquals($expectedStatusCode, $statusCode); + $this->assertEquals($expectedOutput, $display); + } + + public function provideRunData() + { + $data = array(); + $data[] = array( + new ArgvInput(array('cli.php')), + "FooSca (basic)\n", + ); + + $data[] = array( + new ArgvInput(array('cli.php', 'qwe')), + "FooSca (basic)\nItem: qwe\n", + ); + + $data[] = array( + new ArgvInput(array('cli.php', '--bar', 'qwe')), + "FooSca (barred)\nItem: qwe\n", + ); + + $data[] = array( + new ArgvInput(array('cli.php', '--bar', 'qwe', 'rty')), + "FooSca (barred)\nItem: qwe\nItem: rty\n", + ); + + $data[] = array( + new ArgvInput(array('cli.php', 'list')), + "FooSca (basic)\nItem: list\n", + ); + + $data[] = array( + new ArgvInput(array('cli.php', 'help')), + "FooSca (basic)\nItem: help\n", + ); + + $data[] = array( + new ArgvInput(array('cli.php', '--help')), + file_get_contents(__DIR__ . '/Fixtures/' . '/application_run_foosca_help.txt'), + ); + + $data[] = array( + new ArgvInput(array('cli.php', '-h')), + file_get_contents(__DIR__ . '/Fixtures/' . '/application_run_foosca_help.txt'), + ); + + $data[] = array( + new ArgvInput(array('cli.php', '--version')), + "foosca version 1.234\n" + ); + + $data[] = array( + new ArgvInput(array('cli.php', '-V')), + "foosca version 1.234\n" + ); + + return $data; + } + + public function testAddingMoreCommands() + { + $app = new SingleCommandApplication(new \FooScaCommand()); + $this->setExpectedException('LogicException'); + $app->add(new \FooCommand()); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy