Author: fabien
Date: 2010-01-14 12:22:25 +0100 (Thu, 14 Jan 2010)
New Revision: 26613
Added:
branches/2.0/src/Symfony/Components/Console/Shell.php
Modified:
branches/2.0/src/Symfony/Components/Console/Command/Command.php
branches/2.0/tests/fixtures/Symfony/Components/Console/TestCommand.php
branches/2.0/tests/unit/Symfony/Components/Console/Command/CommandTest.php
Log:
Merge branch 'master' of git://github.com/symfony/symfony
Modified: branches/2.0/src/Symfony/Components/Console/Command/Command.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Command/Command.php
2010-01-14 11:00:59 UTC (rev 26612)
+++ branches/2.0/src/Symfony/Components/Console/Command/Command.php
2010-01-14 11:22:25 UTC (rev 26613)
@@ -197,6 +197,16 @@
}
/**
+ * Gets the InputDefinition attached to this Command.
+ *
+ * @return InputDefinition $definition An InputDefinition instance
+ */
+ public function getDefinition()
+ {
+ return $this->definition;
+ }
+
+ /**
* Adds an argument.
*
* @param string $name The argument name
Added: branches/2.0/src/Symfony/Components/Console/Shell.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Shell.php
(rev 0)
+++ branches/2.0/src/Symfony/Components/Console/Shell.php 2010-01-14
11:22:25 UTC (rev 26613)
@@ -0,0 +1,146 @@
+<?php
+
+namespace Symfony\Components\Console;
+
+use Symfony\Components\Console\Application;
+use Symfony\Components\Console\Input\StringInput;
+use Symfony\Components\Console\Output\ConsoleOutput;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * A Shell wraps an Application to add shell capabilities to it.
+ *
+ * This class only works with a PHP compiled with readline support
+ * (either --with-readline or --with-libedit)
+ *
+ * @package symfony
+ * @subpackage cli
+ * @author Fabien Potencier <[email protected]>
+ */
+class Shell
+{
+ protected $application;
+ protected $history;
+ protected $output;
+
+ /**
+ * Constructor.
+ *
+ * If there is no readline support for the current PHP executable
+ * a \RuntimeException exception is thrown.
+ *
+ * @param Application $application An application instance
+ */
+ public function __construct(Application $application)
+ {
+ if (!function_exists('readline'))
+ {
+ throw new \RuntimeException('Unable to start the shell as the Readline
extension is not enabled.');
+ }
+
+ $this->application = $application;
+ $this->history = getenv('HOME').'/.history_'.$application->getName();
+ $this->output = new ConsoleOutput();
+ }
+
+ /**
+ * Runs the shell.
+ */
+ public function run()
+ {
+ $this->application->setAutoExit(false);
+ $this->application->setCatchExceptions(true);
+
+ readline_read_history($this->history);
+ readline_completion_function(array($this, 'autocompleter'));
+
+ $this->output->write($this->getHeader());
+ while (true)
+ {
+ $command = readline($this->application->getName().' > ');
+
+ if (false === $command)
+ {
+ $this->output->write("\n");
+
+ break;
+ }
+
+ readline_add_history($command);
+ readline_write_history($this->history);
+
+ if (0 !== $ret = $this->application->run(new StringInput($command),
$this->output))
+ {
+ $this->output->write(sprintf('<error>The command terminated with an
error status (%s)</error>', $ret));
+ }
+ }
+ }
+
+ /**
+ * Tries to return autocompletion for the current entered text.
+ *
+ * @param string $text The last segment of the entered text
+ * @param integer $position The current position
+ */
+ protected function autocompleter($text, $position)
+ {
+ $info = readline_info();
+ $text = substr($info['line_buffer'], 0, $info['end']);
+
+ if ($info['point'] !== $info['end'])
+ {
+ return true;
+ }
+
+ // task name?
+ if (false === strpos($text, ' ') || !$text)
+ {
+ return array_keys($this->application->getCommands());
+ }
+
+ // options and arguments?
+ try
+ {
+ $command = $this->application->findCommand(substr($text, 0,
strpos($text, ' ')));
+ }
+ catch (\Exception $e)
+ {
+ return true;
+ }
+
+ $list = array('--help');
+ foreach ($command->getDefinition()->getOptions() as $option)
+ {
+ $list[] = '--'.$option->getName();
+ }
+
+ return $list;
+ }
+
+ /**
+ * Returns the shell header.
+ *
+ * @return string The header string
+ */
+ protected function getHeader()
+ {
+ return <<<EOF
+
+Welcome to the <info>{$this->application->getName()}</info> shell.
+
+At the prompt, type <comment>help</comment> for some help,
+or <comment>list</comment> to get a list available commands.
+
+To exit the shell, type <comment>^D</comment>.
+
+EOF;
+ }
+}
Modified: branches/2.0/tests/fixtures/Symfony/Components/Console/TestCommand.php
===================================================================
--- branches/2.0/tests/fixtures/Symfony/Components/Console/TestCommand.php
2010-01-14 11:00:59 UTC (rev 26612)
+++ branches/2.0/tests/fixtures/Symfony/Components/Console/TestCommand.php
2010-01-14 11:22:25 UTC (rev 26613)
@@ -26,11 +26,6 @@
return $this->application;
}
- public function getDefinition()
- {
- return $this->definition;
- }
-
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->write('execute called');
Modified:
branches/2.0/tests/unit/Symfony/Components/Console/Command/CommandTest.php
===================================================================
--- branches/2.0/tests/unit/Symfony/Components/Console/Command/CommandTest.php
2010-01-14 11:00:59 UTC (rev 26612)
+++ branches/2.0/tests/unit/Symfony/Components/Console/Command/CommandTest.php
2010-01-14 11:22:25 UTC (rev 26613)
@@ -50,8 +50,8 @@
$command->setApplication($application);
$t->is($command->getApplication(), $application, '->setApplication() sets the
current application');
-// ->setDefinition()
-$t->diag('->setDefinition()');
+// ->setDefinition() ->getDefinition()
+$t->diag('->setDefinition() ->getDefinition()');
$ret = $command->setDefinition($definition = new InputDefinition());
$t->is($ret, $command, '->setDefinition() implements a fluent interface');
$t->is($command->getDefinition(), $definition, '->setDefinition() sets the
current InputDefinition instance');
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.