From 125bf71fa3df2a03108ee6bc2f4fc4f3a2fa8135 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Jan 2010 14:39:52 +0100 Subject: [PATCH 01/10] [Console] changed the subpackage from cli to console --- src/Symfony/Components/Console/Application.php | 2 +- src/Symfony/Components/Console/Command/Command.php | 2 +- src/Symfony/Components/Console/Command/HelpCommand.php | 2 +- src/Symfony/Components/Console/Command/ListCommand.php | 2 +- src/Symfony/Components/Console/Input/ArgvInput.php | 2 +- src/Symfony/Components/Console/Input/ArrayInput.php | 2 +- src/Symfony/Components/Console/Input/Input.php | 2 +- src/Symfony/Components/Console/Input/InputArgument.php | 2 +- src/Symfony/Components/Console/Input/InputDefinition.php | 2 +- src/Symfony/Components/Console/Input/InputInterface.php | 2 +- src/Symfony/Components/Console/Input/InputOption.php | 2 +- src/Symfony/Components/Console/Input/StringInput.php | 2 +- src/Symfony/Components/Console/Output/ConsoleOutput.php | 2 +- src/Symfony/Components/Console/Output/Formatter.php | 2 +- src/Symfony/Components/Console/Output/NullOutput.php | 2 +- src/Symfony/Components/Console/Output/Output.php | 2 +- src/Symfony/Components/Console/Output/OutputInterface.php | 2 +- src/Symfony/Components/Console/Output/StreamOutput.php | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Components/Console/Application.php b/src/Symfony/Components/Console/Application.php index 6752a792a0..4afd90d9aa 100644 --- a/src/Symfony/Components/Console/Application.php +++ b/src/Symfony/Components/Console/Application.php @@ -38,7 +38,7 @@ use Symfony\Components\Console\Command\ListCommand; * $app->run(); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class Application diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 6489c27838..895e1e001b 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -23,7 +23,7 @@ use Symfony\Components\Console\Application; * Base class for all commands. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class Command diff --git a/src/Symfony/Components/Console/Command/HelpCommand.php b/src/Symfony/Components/Console/Command/HelpCommand.php index af8aa2926d..9b9a0942e5 100644 --- a/src/Symfony/Components/Console/Command/HelpCommand.php +++ b/src/Symfony/Components/Console/Command/HelpCommand.php @@ -22,7 +22,7 @@ use Symfony\Components\Console\Command\Command; * HelpCommand displays the help for a given command. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class HelpCommand extends Command diff --git a/src/Symfony/Components/Console/Command/ListCommand.php b/src/Symfony/Components/Console/Command/ListCommand.php index 293b20e9a7..998cda8f19 100644 --- a/src/Symfony/Components/Console/Command/ListCommand.php +++ b/src/Symfony/Components/Console/Command/ListCommand.php @@ -22,7 +22,7 @@ use Symfony\Components\Console\Command\Command; * ListCommand displays the list of all available commands for the application. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class ListCommand extends Command diff --git a/src/Symfony/Components/Console/Input/ArgvInput.php b/src/Symfony/Components/Console/Input/ArgvInput.php index 627d78b084..a140219bfd 100644 --- a/src/Symfony/Components/Console/Input/ArgvInput.php +++ b/src/Symfony/Components/Console/Input/ArgvInput.php @@ -32,7 +32,7 @@ namespace Symfony\Components\Console\Input; * `StringInput` when you want to provide your own input. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier * * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html diff --git a/src/Symfony/Components/Console/Input/ArrayInput.php b/src/Symfony/Components/Console/Input/ArrayInput.php index abea665100..3ae6154d30 100644 --- a/src/Symfony/Components/Console/Input/ArrayInput.php +++ b/src/Symfony/Components/Console/Input/ArrayInput.php @@ -19,7 +19,7 @@ namespace Symfony\Components\Console\Input; * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar')); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class ArrayInput extends Input diff --git a/src/Symfony/Components/Console/Input/Input.php b/src/Symfony/Components/Console/Input/Input.php index 53b1f87569..a203313dd1 100644 --- a/src/Symfony/Components/Console/Input/Input.php +++ b/src/Symfony/Components/Console/Input/Input.php @@ -21,7 +21,7 @@ namespace Symfony\Components\Console\Input; * * `ArrayInput`: The input is provided as an array * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ abstract class Input implements InputInterface diff --git a/src/Symfony/Components/Console/Input/InputArgument.php b/src/Symfony/Components/Console/Input/InputArgument.php index ff8a90e36d..9b5925b4bf 100644 --- a/src/Symfony/Components/Console/Input/InputArgument.php +++ b/src/Symfony/Components/Console/Input/InputArgument.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Input; * Represents a command line argument. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class InputArgument diff --git a/src/Symfony/Components/Console/Input/InputDefinition.php b/src/Symfony/Components/Console/Input/InputDefinition.php index bcfe2a7841..2977e7fd6d 100644 --- a/src/Symfony/Components/Console/Input/InputDefinition.php +++ b/src/Symfony/Components/Console/Input/InputDefinition.php @@ -22,7 +22,7 @@ namespace Symfony\Components\Console\Input; * )); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class InputDefinition diff --git a/src/Symfony/Components/Console/Input/InputInterface.php b/src/Symfony/Components/Console/Input/InputInterface.php index eaf01af7c6..953553e780 100644 --- a/src/Symfony/Components/Console/Input/InputInterface.php +++ b/src/Symfony/Components/Console/Input/InputInterface.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Input; * InputInterface is the interface implemented by all input classes. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ interface InputInterface diff --git a/src/Symfony/Components/Console/Input/InputOption.php b/src/Symfony/Components/Console/Input/InputOption.php index f5d7e98448..46a47d0d57 100644 --- a/src/Symfony/Components/Console/Input/InputOption.php +++ b/src/Symfony/Components/Console/Input/InputOption.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Input; * Represents a command line option. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class InputOption diff --git a/src/Symfony/Components/Console/Input/StringInput.php b/src/Symfony/Components/Console/Input/StringInput.php index 2275bd5d12..c66f7b9dc4 100644 --- a/src/Symfony/Components/Console/Input/StringInput.php +++ b/src/Symfony/Components/Console/Input/StringInput.php @@ -19,7 +19,7 @@ namespace Symfony\Components\Console\Input; * $input = new StringInput('foo --bar="foobar"'); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class StringInput extends ArgvInput diff --git a/src/Symfony/Components/Console/Output/ConsoleOutput.php b/src/Symfony/Components/Console/Output/ConsoleOutput.php index 332fa5125d..7950c25ec0 100644 --- a/src/Symfony/Components/Console/Output/ConsoleOutput.php +++ b/src/Symfony/Components/Console/Output/ConsoleOutput.php @@ -23,7 +23,7 @@ namespace Symfony\Components\Console\Output; * $output = new StreamOutput(fopen('php://stdout', 'w')); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class ConsoleOutput extends StreamOutput diff --git a/src/Symfony/Components/Console/Output/Formatter.php b/src/Symfony/Components/Console/Output/Formatter.php index 8a1efd0e4b..3ea0a911c6 100644 --- a/src/Symfony/Components/Console/Output/Formatter.php +++ b/src/Symfony/Components/Console/Output/Formatter.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Output; * The Formatter class provides helpers to format messages. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class Formatter diff --git a/src/Symfony/Components/Console/Output/NullOutput.php b/src/Symfony/Components/Console/Output/NullOutput.php index 1f0ec21806..b43a1c616a 100644 --- a/src/Symfony/Components/Console/Output/NullOutput.php +++ b/src/Symfony/Components/Console/Output/NullOutput.php @@ -17,7 +17,7 @@ namespace Symfony\Components\Console\Output; * $output = new NullOutput(); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class NullOutput extends Output diff --git a/src/Symfony/Components/Console/Output/Output.php b/src/Symfony/Components/Console/Output/Output.php index b487a63220..9917c3b534 100644 --- a/src/Symfony/Components/Console/Output/Output.php +++ b/src/Symfony/Components/Console/Output/Output.php @@ -21,7 +21,7 @@ namespace Symfony\Components\Console\Output; * * quiet: -q (no output) * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ abstract class Output implements OutputInterface diff --git a/src/Symfony/Components/Console/Output/OutputInterface.php b/src/Symfony/Components/Console/Output/OutputInterface.php index e619b3e861..26bc6b76ef 100644 --- a/src/Symfony/Components/Console/Output/OutputInterface.php +++ b/src/Symfony/Components/Console/Output/OutputInterface.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Output; * OutputInterface is the interface implemented by all Output classes. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ interface OutputInterface diff --git a/src/Symfony/Components/Console/Output/StreamOutput.php b/src/Symfony/Components/Console/Output/StreamOutput.php index c4bc2bd0df..4360776d75 100644 --- a/src/Symfony/Components/Console/Output/StreamOutput.php +++ b/src/Symfony/Components/Console/Output/StreamOutput.php @@ -23,7 +23,7 @@ namespace Symfony\Components\Console\Output; * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false)); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class StreamOutput extends Output From afcb3c906d0f4583da491dda51a3a9f01aa3b0b8 Mon Sep 17 00:00:00 2001 From: geoffrey Date: Mon, 11 Jan 2010 12:24:08 +0100 Subject: [PATCH 02/10] [Console] implemented helpers and moved formatter and ask* methods to their own helpers --- .../Components/Console/Command/Command.php | 103 +++++---------- .../FormatterHelper.php} | 23 ++-- .../Components/Console/Helper/Helper.php | 45 +++++++ .../Console/Helper/HelperInterface.php | 43 +++++++ .../Components/Console/Helper/HelperSet.php | 105 ++++++++++++++++ .../Console/Helper/InteractHelper.php | 119 ++++++++++++++++++ .../Console/Helper/FormatterHelperTest.php | 28 +++++ .../Console/Output/FormatterTest.php | 26 ---- 8 files changed, 389 insertions(+), 103 deletions(-) rename src/Symfony/Components/Console/{Output/Formatter.php => Helper/FormatterHelper.php} (77%) create mode 100644 src/Symfony/Components/Console/Helper/Helper.php create mode 100644 src/Symfony/Components/Console/Helper/HelperInterface.php create mode 100644 src/Symfony/Components/Console/Helper/HelperSet.php create mode 100644 src/Symfony/Components/Console/Helper/InteractHelper.php create mode 100644 tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Output/FormatterTest.php diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 895e1e001b..5b91c5b22b 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -7,8 +7,10 @@ use Symfony\Components\Console\Input\InputOption; use Symfony\Components\Console\Input\InputArgument; use Symfony\Components\Console\Input\InputInterface; use Symfony\Components\Console\Output\OutputInterface; -use Symfony\Components\Console\Output\Formatter; use Symfony\Components\Console\Application; +use Symfony\Components\Console\Helper\HelperSet; +use Symfony\Components\Console\Helper\FormatterHelper; +use Symfony\Components\Console\Helper\InteractHelper; /* * This file is part of the symfony framework. @@ -42,15 +44,27 @@ class Command /** * Constructor. + * + * @param string $name + * @param HelperSet $helperSet A helper set instance */ - public function __construct($name = null) + public function __construct($name = null, HelperSet $helperSet = null) { $this->definition = new InputDefinition(); $this->ignoreValidationErrors = false; $this->applicationDefinitionMerged = false; - $this->formatter = new Formatter(); $this->aliases = array(); + if (null === $helperSet) + { + $helperSet = new HelperSet(array( + new FormatterHelper(), + new InteractHelper(), + )); + } + + $this->setHelperSet($helperSet); + if (null !== $name) { $this->setName($name); @@ -379,90 +393,39 @@ class Command } /** - * Asks a question to the user. + * Set a helper set to be used with the command. * - * @param OutputInterface $output - * @param string|array $question The question to ask - * @param string $default The default answer if none is given by the user - * - * @param string The user answer + * @param HelperSet $helperSet The helper set */ - static public function ask(OutputInterface $output, $question, $default = null) + public function setHelperSet(HelperSet $helperSet) { - // @codeCoverageIgnoreStart - $output->write($question); + $this->helperSet = $helperSet; - $ret = trim(fgets(STDIN)); - - return $ret ? $ret : $default; - // @codeCoverageIgnoreEnd + $helperSet->setCommand($this); } /** - * Asks a confirmation to the user. + * Get the helper set associated with the command * - * The question will be asked until the user answer by nothing, yes, or no. - * - * @param OutputInterface $output - * @param string|array $question The question to ask - * @param Boolean $default The default answer if the user enters nothing - * - * @param Boolean true if the user has confirmed, false otherwise + * @return HelperSet */ - static public function askConfirmation(OutputInterface $output, $question, $default = true) + public function getHelperSet() { - // @codeCoverageIgnoreStart - $answer = 'z'; - while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) - { - $answer = static::ask($output, $question); - } - - if (false === $default) - { - return $answer && 'y' == strtolower($answer[0]); - } - else - { - return !$answer || 'y' == strtolower($answer[0]); - } - // @codeCoverageIgnoreEnd + return $this->helperSet; } /** - * Asks for a value and validates the response. + * Gets a helper value. * - * @param OutputInterface $output - * @param string|array $question - * @param Closure $validator - * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite) + * @param string $name The helper name * - * @return mixed + * @return mixed The helper value + * + * @throws \InvalidArgumentException if the helper is not defined */ - static public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false) + public function __get($name) { - // @codeCoverageIgnoreStart - $error = null; - while (false === $attempts || $attempts--) - { - if (null !== $error) - { - $output->write($this->formatter->formatBlock($error->getMessage(), 'error')); - } - - $value = static::ask($output, $question, null); - - try - { - return $validator($value); - } - catch (\Exception $error) - { - } - } - - throw $error; - // @codeCoverageIgnoreEnd + return $this->helperSet->get($name); } /** diff --git a/src/Symfony/Components/Console/Output/Formatter.php b/src/Symfony/Components/Console/Helper/FormatterHelper.php similarity index 77% rename from src/Symfony/Components/Console/Output/Formatter.php rename to src/Symfony/Components/Console/Helper/FormatterHelper.php index 3ea0a911c6..a91064b181 100644 --- a/src/Symfony/Components/Console/Output/Formatter.php +++ b/src/Symfony/Components/Console/Helper/FormatterHelper.php @@ -1,6 +1,6 @@ */ -class Formatter +class FormatterHelper extends Helper { + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'formatter'; + } + /** * Formats a message within a section. * @@ -27,7 +35,7 @@ class Formatter * @param string $message The message * @param string $style The style to apply to the section */ - static public function formatSection($section, $message, $style = 'info') + public function formatSection($section, $message, $style = 'info') { return sprintf("<%s>[%s] %s", $style, $section, $style, $message); } @@ -41,7 +49,7 @@ class Formatter * * @return string The formatter message */ - static public function formatBlock($messages, $style, $large = false) + public function formatBlock($messages, $style, $large = false) { if (!is_array($messages)) { @@ -53,13 +61,13 @@ class Formatter foreach ($messages as $message) { $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); - $len = max(static::strlen($message) + ($large ? 4 : 2), $len); + $len = max($this->strlen($message) + ($large ? 4 : 2), $len); } $messages = $large ? array(str_repeat(' ', $len)) : array(); foreach ($lines as $line) { - $messages[] = $line.str_repeat(' ', $len - static::strlen($line)); + $messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); } if ($large) { @@ -74,8 +82,9 @@ class Formatter return implode("\n", $messages); } - static protected function strlen($string) + protected function strlen($string) { return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string); } } + diff --git a/src/Symfony/Components/Console/Helper/Helper.php b/src/Symfony/Components/Console/Helper/Helper.php new file mode 100644 index 0000000000..642e7a67c7 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/Helper.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Helper is the base class for all helper classes. + * + * @package symfony + * @subpackage templating + * @author Fabien Potencier + */ +abstract class Helper implements HelperInterface +{ + protected + $helperSet = null; + + /** + * Sets the helper set associated with this helper. + * + * @param HelperSet $helperSet A HelperSet instance + */ + public function setHelperSet(HelperSet $helperSet = null) + { + $this->helperSet = $helperSet; + } + + /** + * Gets the helper set associated with this helper. + * + * @return HelperSet A HelperSet instance + */ + public function getHelperSet() + { + return $this->helperSet; + } +} diff --git a/src/Symfony/Components/Console/Helper/HelperInterface.php b/src/Symfony/Components/Console/Helper/HelperInterface.php new file mode 100644 index 0000000000..a3474fc610 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/HelperInterface.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * HelperInterface is the interface all helpers must implement. + * + * @package symfony + * @subpackage templating + * @author Fabien Potencier + */ +interface HelperInterface +{ + /** + * Returns the canonical name of this helper. + * + * @return string The canonical name + */ + function getName(); + + /** + * Sets the helper set associated with this helper. + * + * @param HelperSet $helperSet A HelperSet instance + */ + function setHelperSet(HelperSet $helperSet = null); + + /** + * Gets the helper set associated with this helper. + * + * @return HelperSet A HelperSet instance + */ + function getHelperSet(); +} diff --git a/src/Symfony/Components/Console/Helper/HelperSet.php b/src/Symfony/Components/Console/Helper/HelperSet.php new file mode 100644 index 0000000000..b71e9bd1b7 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/HelperSet.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * HelperSet represents a set of helpers to be used with a command. + * + * @package symfony + * @subpackage templating + * @author Fabien Potencier + */ +class HelperSet +{ + protected + $helpers = array(), + $command = null; + + public function __construct(array $helpers = array()) + { + foreach ($helpers as $alias => $helper) + { + $this->set($helper, is_int($alias) ? null : $alias); + } + } + + /** + * Sets a helper. + * + * @param HelperInterface $value The helper instance + * @param string $alias An alias + */ + public function set(HelperInterface $helper, $alias = null) + { + $this->helpers[$helper->getName()] = $helper; + if (null !== $alias) + { + $this->helpers[$alias] = $helper; + } + + $helper->setHelperSet($this); + } + + /** + * Returns true if the helper if defined. + * + * @param string $name The helper name + * + * @return Boolean true if the helper is defined, false otherwise + */ + public function has($name) + { + return isset($this->helpers[$name]); + } + + /** + * Gets a helper value. + * + * @param string $name The helper name + * + * @return HelperInterface The helper instance + * + * @throws \InvalidArgumentException if the helper is not defined + */ + public function get($name) + { + if (!$this->has($name)) + { + throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); + } + + return $this->helpers[$name]; + } + + /** + * Sets the command associated with this helper set. + * + * @param Command $command A Command instance + */ + public function setCommand(Command $command = null) + { + $this->command = $command; + } + + /** + * Gets the command associated with this helper set. + * + * @return Command A Command instance + */ + public function getCommand() + { + return $this->command; + } +} + diff --git a/src/Symfony/Components/Console/Helper/InteractHelper.php b/src/Symfony/Components/Console/Helper/InteractHelper.php new file mode 100644 index 0000000000..0054ab08d3 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/InteractHelper.php @@ -0,0 +1,119 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * The Interact class provides helpers to interact with the user. + * + * @package symfony + * @subpackage cli + * @author Fabien Potencier + */ +class InteractHelper extends Helper +{ + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'interact'; + } + + /** + * Asks a question to the user. + * + * @param OutputInterface $output + * @param string|array $question The question to ask + * @param string $default The default answer if none is given by the user + * + * @param string The user answer + */ + public function ask(OutputInterface $output, $question, $default = null) + { + // @codeCoverageIgnoreStart + $output->write($question); + + $ret = trim(fgets(STDIN)); + + return $ret ? $ret : $default; + // @codeCoverageIgnoreEnd + } + + /** + * Asks a confirmation to the user. + * + * The question will be asked until the user answer by nothing, yes, or no. + * + * @param OutputInterface $output + * @param string|array $question The question to ask + * @param Boolean $default The default answer if the user enters nothing + * + * @param Boolean true if the user has confirmed, false otherwise + */ + public function askConfirmation(OutputInterface $output, $question, $default = true) + { + // @codeCoverageIgnoreStart + $answer = 'z'; + while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) + { + $answer = $this->ask($output, $question); + } + + if (false === $default) + { + return $answer && 'y' == strtolower($answer[0]); + } + else + { + return !$answer || 'y' == strtolower($answer[0]); + } + // @codeCoverageIgnoreEnd + } + + /** + * Asks for a value and validates the response. + * + * @param OutputInterface $output + * @param string|array $question + * @param Closure $validator + * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite) + * + * @return mixed + */ + public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false) + { + // @codeCoverageIgnoreStart + $error = null; + while (false === $attempts || $attempts--) + { + if (null !== $error) + { + $output->write($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); + } + + $value = $this->ask($output, $question, null); + + try + { + return $validator($value); + } + catch (\Exception $error) + { + } + } + + throw $error; + // @codeCoverageIgnoreEnd + } +} diff --git a/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php b/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php new file mode 100644 index 0000000000..0fd2199b61 --- /dev/null +++ b/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once __DIR__.'/../../../../bootstrap.php'; + +use Symfony\Components\Console\Helper\FormatterHelper; + +$formatter = new FormatterHelper(); + +$t = new LimeTest(4); + +// ::formatSection() +$t->diag('::formatSection()'); +$t->is($formatter->formatSection('cli', 'Some text to display'), '[cli] Some text to display', '::formatSection() formats a message in a section'); + +// ::formatBlock() +$t->diag('::formatBlock()'); +$t->is($formatter->formatBlock('Some text to display', 'error'), ' Some text to display ', '::formatBlock() formats a message in a block'); +$t->is($formatter->formatBlock(array('Some text to display', 'foo bar'), 'error'), " Some text to display \n foo bar ", '::formatBlock() formats a message in a block'); + +$t->is($formatter->formatBlock('Some text to display', 'error', true), " \n Some text to display \n ", '::formatBlock() formats a message in a block'); diff --git a/tests/unit/Symfony/Components/Console/Output/FormatterTest.php b/tests/unit/Symfony/Components/Console/Output/FormatterTest.php deleted file mode 100644 index 401706e13d..0000000000 --- a/tests/unit/Symfony/Components/Console/Output/FormatterTest.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Output\Formatter; - -$t = new LimeTest(4); - -// ::formatSection() -$t->diag('::formatSection()'); -$t->is(Formatter::formatSection('cli', 'Some text to display'), '[cli] Some text to display', '::formatSection() formats a message in a section'); - -// ::formatBlock() -$t->diag('::formatBlock()'); -$t->is(Formatter::formatBlock('Some text to display', 'error'), ' Some text to display ', '::formatBlock() formats a message in a block'); -$t->is(Formatter::formatBlock(array('Some text to display', 'foo bar'), 'error'), " Some text to display \n foo bar ", '::formatBlock() formats a message in a block'); - -$t->is(Formatter::formatBlock('Some text to display', 'error', true), " \n Some text to display \n ", '::formatBlock() formats a message in a block'); From 0c2b2bdbbb91dc39197a297cf567152fe7ab2c90 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Jan 2010 14:46:11 +0100 Subject: [PATCH 03/10] [Console] refactored helpers --- .../Components/Console/Application.php | 28 +++++++++++ .../Components/Console/Command/Command.php | 48 +++---------------- .../{InteractHelper.php => DialogHelper.php} | 22 ++++----- .../Console/Helper/FormatterHelper.php | 17 ++++--- .../Components/Console/Helper/Helper.php | 8 ++-- .../Console/Helper/HelperInterface.php | 22 ++++----- .../Components/Console/Helper/HelperSet.php | 9 ++-- 7 files changed, 72 insertions(+), 82 deletions(-) rename src/Symfony/Components/Console/Helper/{InteractHelper.php => DialogHelper.php} (95%) diff --git a/src/Symfony/Components/Console/Application.php b/src/Symfony/Components/Console/Application.php index 4afd90d9aa..7b1a8187fc 100644 --- a/src/Symfony/Components/Console/Application.php +++ b/src/Symfony/Components/Console/Application.php @@ -14,6 +14,9 @@ use Symfony\Components\Console\Output\ConsoleOutput; use Symfony\Components\Console\Command\Command; use Symfony\Components\Console\Command\HelpCommand; use Symfony\Components\Console\Command\ListCommand; +use Symfony\Components\Console\Helper\HelperSet; +use Symfony\Components\Console\Helper\FormatterHelper; +use Symfony\Components\Console\Helper\DialogHelper; /* * This file is part of the symfony framework. @@ -53,6 +56,7 @@ class Application protected $catchExceptions; protected $autoExit; protected $definition; + protected $helperSet; /** * Constructor. @@ -68,6 +72,10 @@ class Application $this->autoExit = true; $this->commands = array(); $this->aliases = array(); + $this->helperSet = new HelperSet(array( + new FormatterHelper(), + new DialogHelper(), + )); $this->addCommand(new HelpCommand()); $this->addCommand(new ListCommand()); @@ -200,6 +208,26 @@ class Application return is_numeric($statusCode) ? $statusCode : 0; } + /** + * Set a helper set to be used with the command. + * + * @param HelperSet $helperSet The helper set + */ + public function setHelperSet(HelperSet $helperSet) + { + $this->helperSet = $helperSet; + } + + /** + * Get the helper set associated with the command + * + * @return HelperSet The HelperSet isntance associated with this command + */ + public function getHelperSet() + { + return $this->helperSet; + } + /** * Gets the InputDefinition related to this Application. * diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 5b91c5b22b..19b42a131d 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -8,9 +8,6 @@ use Symfony\Components\Console\Input\InputArgument; use Symfony\Components\Console\Input\InputInterface; use Symfony\Components\Console\Output\OutputInterface; use Symfony\Components\Console\Application; -use Symfony\Components\Console\Helper\HelperSet; -use Symfony\Components\Console\Helper\FormatterHelper; -use Symfony\Components\Console\Helper\InteractHelper; /* * This file is part of the symfony framework. @@ -45,26 +42,15 @@ class Command /** * Constructor. * - * @param string $name - * @param HelperSet $helperSet A helper set instance + * @param string $name The name of the command */ - public function __construct($name = null, HelperSet $helperSet = null) + public function __construct($name = null) { $this->definition = new InputDefinition(); $this->ignoreValidationErrors = false; $this->applicationDefinitionMerged = false; $this->aliases = array(); - if (null === $helperSet) - { - $helperSet = new HelperSet(array( - new FormatterHelper(), - new InteractHelper(), - )); - } - - $this->setHelperSet($helperSet); - if (null !== $name) { $this->setName($name); @@ -393,39 +379,17 @@ class Command } /** - * Set a helper set to be used with the command. + * Gets a helper instance by name. * - * @param HelperSet $helperSet The helper set - */ - public function setHelperSet(HelperSet $helperSet) - { - $this->helperSet = $helperSet; - - $helperSet->setCommand($this); - } - - /** - * Get the helper set associated with the command - * - * @return HelperSet - */ - public function getHelperSet() - { - return $this->helperSet; - } - - /** - * Gets a helper value. - * - * @param string $name The helper name + * @param string $name The helper name * * @return mixed The helper value * * @throws \InvalidArgumentException if the helper is not defined */ - public function __get($name) + protected function getHelper($name) { - return $this->helperSet->get($name); + return $this->application->getHelperSet()->get($name); } /** diff --git a/src/Symfony/Components/Console/Helper/InteractHelper.php b/src/Symfony/Components/Console/Helper/DialogHelper.php similarity index 95% rename from src/Symfony/Components/Console/Helper/InteractHelper.php rename to src/Symfony/Components/Console/Helper/DialogHelper.php index 0054ab08d3..21b55cef03 100644 --- a/src/Symfony/Components/Console/Helper/InteractHelper.php +++ b/src/Symfony/Components/Console/Helper/DialogHelper.php @@ -14,22 +14,14 @@ use Symfony\Components\Console\Output\OutputInterface; */ /** - * The Interact class provides helpers to interact with the user. + * The Dialog class provides helpers to interact with the user. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ -class InteractHelper extends Helper +class DialogHelper extends Helper { - /** - * Returns the helper's canonical name - */ - public function getName() - { - return 'interact'; - } - /** * Asks a question to the user. * @@ -116,4 +108,12 @@ class InteractHelper extends Helper throw $error; // @codeCoverageIgnoreEnd } + + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'dialog'; + } } diff --git a/src/Symfony/Components/Console/Helper/FormatterHelper.php b/src/Symfony/Components/Console/Helper/FormatterHelper.php index a91064b181..6eedca733a 100644 --- a/src/Symfony/Components/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Components/Console/Helper/FormatterHelper.php @@ -20,14 +20,6 @@ namespace Symfony\Components\Console\Helper; */ class FormatterHelper extends Helper { - /** - * Returns the helper's canonical name - */ - public function getName() - { - return 'formatter'; - } - /** * Formats a message within a section. * @@ -86,5 +78,12 @@ class FormatterHelper extends Helper { return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string); } -} + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'formatter'; + } +} diff --git a/src/Symfony/Components/Console/Helper/Helper.php b/src/Symfony/Components/Console/Helper/Helper.php index 642e7a67c7..f083b47abe 100644 --- a/src/Symfony/Components/Console/Helper/Helper.php +++ b/src/Symfony/Components/Console/Helper/Helper.php @@ -3,19 +3,19 @@ namespace Symfony\Components\Console\Helper; /* - * This file is part of the symfony package. + * This file is part of the symfony framework. * * (c) Fabien Potencier * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. */ /** * Helper is the base class for all helper classes. * * @package symfony - * @subpackage templating + * @subpackage console * @author Fabien Potencier */ abstract class Helper implements HelperInterface diff --git a/src/Symfony/Components/Console/Helper/HelperInterface.php b/src/Symfony/Components/Console/Helper/HelperInterface.php index a3474fc610..f3de9aa65e 100644 --- a/src/Symfony/Components/Console/Helper/HelperInterface.php +++ b/src/Symfony/Components/Console/Helper/HelperInterface.php @@ -3,30 +3,23 @@ namespace Symfony\Components\Console\Helper; /* - * This file is part of the symfony package. + * This file is part of the symfony framework. * * (c) Fabien Potencier * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. */ /** * HelperInterface is the interface all helpers must implement. * * @package symfony - * @subpackage templating + * @subpackage console * @author Fabien Potencier */ interface HelperInterface { - /** - * Returns the canonical name of this helper. - * - * @return string The canonical name - */ - function getName(); - /** * Sets the helper set associated with this helper. * @@ -40,4 +33,11 @@ interface HelperInterface * @return HelperSet A HelperSet instance */ function getHelperSet(); + + /** + * Returns the canonical name of this helper. + * + * @return string The canonical name + */ + function getName(); } diff --git a/src/Symfony/Components/Console/Helper/HelperSet.php b/src/Symfony/Components/Console/Helper/HelperSet.php index b71e9bd1b7..67d764f52e 100644 --- a/src/Symfony/Components/Console/Helper/HelperSet.php +++ b/src/Symfony/Components/Console/Helper/HelperSet.php @@ -5,19 +5,19 @@ namespace Symfony\Components\Console\Helper; use Symfony\Components\Console\Command\Command; /* - * This file is part of the symfony package. + * This file is part of the symfony framework. * * (c) Fabien Potencier * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. */ /** * HelperSet represents a set of helpers to be used with a command. * * @package symfony - * @subpackage templating + * @subpackage console * @author Fabien Potencier */ class HelperSet @@ -102,4 +102,3 @@ class HelperSet return $this->command; } } - From a0551d525c50707b46ea85edb3a147f95bff0bd9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Jan 2010 15:56:49 +0100 Subject: [PATCH 04/10] [DependencyInjection] made the loader extensions much more reliable and robust --- .../DependencyInjection/Dumper/XmlDumper.php | 4 +- .../DependencyInjection/Loader/Loader.php | 14 +- .../Loader/LoaderExtensionInterface.php | 13 +- .../Loader/XmlFileLoader.php | 75 ++++++--- .../Loader/schema/services/services-1.0.xsd | 155 ++++++++++++++++++ .../DependencyInjection/Loader/services.xsd | 113 ------------- .../includes/ProjectExtension.php | 5 + .../DependencyInjection/xml/services1.xml | 4 +- .../DependencyInjection/xml/services10.xml | 4 +- .../DependencyInjection/xml/services11.xml | 4 +- .../DependencyInjection/xml/services12.xml | 4 +- .../DependencyInjection/xml/services2.xml | 4 +- .../DependencyInjection/xml/services3.xml | 4 +- .../DependencyInjection/xml/services4.xml | 4 +- .../DependencyInjection/xml/services5.xml | 4 +- .../DependencyInjection/xml/services6.xml | 4 +- .../DependencyInjection/xml/services7.xml | 4 +- .../DependencyInjection/xml/services8.xml | 4 +- .../DependencyInjection/xml/services9.xml | 4 +- 19 files changed, 276 insertions(+), 151 deletions(-) create mode 100644 src/Symfony/Components/DependencyInjection/Loader/schema/services/services-1.0.xsd delete mode 100644 src/Symfony/Components/DependencyInjection/Loader/services.xsd diff --git a/src/Symfony/Components/DependencyInjection/Dumper/XmlDumper.php b/src/Symfony/Components/DependencyInjection/Dumper/XmlDumper.php index bfe0f27392..4c0ac3bb18 100644 --- a/src/Symfony/Components/DependencyInjection/Dumper/XmlDumper.php +++ b/src/Symfony/Components/DependencyInjection/Dumper/XmlDumper.php @@ -165,7 +165,9 @@ class XmlDumper extends Dumper return << - + EOF; } diff --git a/src/Symfony/Components/DependencyInjection/Loader/Loader.php b/src/Symfony/Components/DependencyInjection/Loader/Loader.php index 2b721b2550..aff4c24efc 100644 --- a/src/Symfony/Components/DependencyInjection/Loader/Loader.php +++ b/src/Symfony/Components/DependencyInjection/Loader/Loader.php @@ -22,11 +22,23 @@ abstract class Loader implements LoaderInterface { static protected $extensions = array(); + /** + * Registers an extension. + * + * @param LoaderExtensionInterface $extension An extension instance + */ static public function registerExtension(LoaderExtensionInterface $extension) { - static::$extensions[$extension->getNamespace()] = $extension; + static::$extensions[$extension->getAlias()] = static::$extensions[$extension->getNamespace()] = $extension; } + /** + * Returns an extension by alias or namespace. + * + * @param string $name An alias or a namespace + * + * @return LoaderExtensionInterface An extension instance + */ static public function getExtension($name) { return isset(static::$extensions[$name]) ? static::$extensions[$name] : null; diff --git a/src/Symfony/Components/DependencyInjection/Loader/LoaderExtensionInterface.php b/src/Symfony/Components/DependencyInjection/Loader/LoaderExtensionInterface.php index a660bf4c8d..26b2f04cb1 100644 --- a/src/Symfony/Components/DependencyInjection/Loader/LoaderExtensionInterface.php +++ b/src/Symfony/Components/DependencyInjection/Loader/LoaderExtensionInterface.php @@ -31,9 +31,18 @@ interface LoaderExtensionInterface public function load($tag, array $config); /** - * Returns the namespace to be used for this extension. + * Returns the namespace to be used for this extension (XML namespace). * - * @return string The namespace + * @return string The XML namespace */ public function getNamespace(); + + /** + * Returns the recommanded alias to use in XML. + * + * This alias is also the mandatory prefix to use when using YAML. + * + * @return string The alias + */ + public function getAlias(); } diff --git a/src/Symfony/Components/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Components/DependencyInjection/Loader/XmlFileLoader.php index 492f472bcd..688202067f 100644 --- a/src/Symfony/Components/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Components/DependencyInjection/Loader/XmlFileLoader.php @@ -195,10 +195,12 @@ class XmlFileLoader extends FileLoader { throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); } + $dom->validateOnParse = true; + $dom->normalizeDocument(); libxml_use_internal_errors(false); $this->validate($dom, $path); - $xmls[$path] = simplexml_import_dom($dom, 'Symfony\Components\DependencyInjection\SimpleXMLElement'); + $xmls[$path] = simplexml_import_dom($dom, 'Symfony\\Components\\DependencyInjection\\SimpleXMLElement'); } return $xmls; @@ -210,7 +212,7 @@ class XmlFileLoader extends FileLoader $count = 0; // find anonymous service definitions - $xml->registerXPathNamespace('container', 'http://symfony-project.org/2.0/container'); + $xml->registerXPathNamespace('container', 'http://www.symfony-project.org/schema/services'); $nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]'); foreach ($nodes as $node) { @@ -236,14 +238,52 @@ class XmlFileLoader extends FileLoader protected function validate($dom, $file) { + $this->validateSchema($dom, $file); + $this->validateExtensions($dom, $file); + } + + protected function validateSchema($dom, $file) + { + $schemaLocations = array('http://www.symfony-project.org/schema/services' => __DIR__.'/schema/services/services-1.0.xsd'); + + if ($element = $dom->documentElement->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')) + { + $items = preg_split('/\s+/', $element); + for ($i = 0, $nb = count($items); $i < $nb; $i += 2) + { + $schemaLocations[$items[$i]] = str_replace('http://www.symfony-project.org/', __DIR__.'/', $items[$i + 1]); + } + } + + $imports = ''; + foreach ($schemaLocations as $namespace => $location) + { + $imports .= sprintf(' '."\n", $namespace, $location); + } + + $source = << + + + +$imports + +EOF + ; + libxml_use_internal_errors(true); - if (!$dom->schemaValidate(__DIR__.'/services.xsd')) + if (!$dom->schemaValidateSource($source)) { throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); } libxml_use_internal_errors(false); + } - // validate extensions + protected function validateExtensions($dom, $file) + { foreach ($dom->documentElement->childNodes as $node) { if (!$node instanceof \DOMElement || in_array($node->tagName, array('imports', 'parameters', 'services'))) @@ -251,19 +291,16 @@ class XmlFileLoader extends FileLoader continue; } - // can it be handled by an extension? - if (false !== strpos($node->tagName, ':')) + if ($node->namespaceURI === 'http://www.symfony-project.org/schema/services') { - list($namespace, $tag) = explode(':', $node->tagName); - if (!static::getExtension($namespace)) - { - throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $node->tagName, $file)); - } - - continue; + throw new \InvalidArgumentException(sprintf('The "%s" tag is not valid (in %s).', $node->tagName, $file)); } - throw new \InvalidArgumentException(sprintf('The "%s" tag is not valid (in %s).', $node->tagName, $file)); + // can it be handled by an extension? + if (!static::getExtension($node->namespaceURI)) + { + throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $node->tagName, $file)); + } } } @@ -289,15 +326,15 @@ class XmlFileLoader extends FileLoader protected function loadFromExtensions(BuilderConfiguration $configuration, $xml) { - foreach (dom_import_simplexml($xml)->getElementsByTagNameNS('*', '*') as $element) + foreach (dom_import_simplexml($xml)->childNodes as $node) { - if (!$element->prefix) + if (!$node instanceof \DOMElement || $node->namespaceURI === 'http://www.symfony-project.org/schema/services') { continue; } - $values = static::convertDomElementToArray($element); - $config = $this->getExtension($element->prefix)->load($element->localName, is_array($values) ? $values : array($values)); + $values = static::convertDomElementToArray($node); + $config = $this->getExtension($node->namespaceURI)->load($node->localName, is_array($values) ? $values : array($values)); $configuration->merge($config); } @@ -345,7 +382,7 @@ class XmlFileLoader extends FileLoader } elseif (!$node instanceof \DOMComment) { - $config[$node->tagName] = static::convertDomElementToArray($node); + $config[$node->localName] = static::convertDomElementToArray($node); $empty = false; } } diff --git a/src/Symfony/Components/DependencyInjection/Loader/schema/services/services-1.0.xsd b/src/Symfony/Components/DependencyInjection/Loader/schema/services/services-1.0.xsd new file mode 100644 index 0000000000..b86a54cb5b --- /dev/null +++ b/src/Symfony/Components/DependencyInjection/Loader/schema/services/services-1.0.xsd @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Symfony/Components/DependencyInjection/Loader/services.xsd b/src/Symfony/Components/DependencyInjection/Loader/services.xsd deleted file mode 100644 index 45fbc989ab..0000000000 --- a/src/Symfony/Components/DependencyInjection/Loader/services.xsd +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php b/tests/fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php index 409d1aa003..82b636186c 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php +++ b/tests/fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php @@ -17,6 +17,11 @@ class ProjectExtension extends LoaderExtension } public function getNamespace() + { + return 'http://www.example.com/schema/project'; + } + + public function getAlias() { return 'project'; } diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services1.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services1.xml index 204c70eaa9..7fed40e139 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services1.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services1.xml @@ -1,4 +1,6 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services10.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services10.xml index b0336ba996..da33906233 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services10.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services10.xml @@ -1,7 +1,7 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services11.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services11.xml index d31cfc1aea..615efeae5f 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services11.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services11.xml @@ -1,7 +1,7 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services12.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services12.xml index 97268df7d2..94ba0c6757 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services12.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services12.xml @@ -1,7 +1,7 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services2.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services2.xml index e28d729625..11031cadb0 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services2.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services2.xml @@ -1,6 +1,8 @@ - + a string bar diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services3.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services3.xml index e10c772469..16bfb721d3 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services3.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services3.xml @@ -1,6 +1,8 @@ - + foo diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services4.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services4.xml index 1f865938c8..42f76abc31 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services4.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services4.xml @@ -1,6 +1,8 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services5.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services5.xml index c2ef9ddd0b..f21ce2289d 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services5.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services5.xml @@ -1,6 +1,8 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services6.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services6.xml index 03ceaf2653..c14e21f477 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services6.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services6.xml @@ -1,6 +1,8 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services7.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services7.xml index edda0a1993..5387ff7f45 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services7.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services7.xml @@ -1,6 +1,8 @@ - + diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services8.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services8.xml index 342c7d4a23..ba3eeb36d6 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services8.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services8.xml @@ -1,6 +1,8 @@ - + bar foo is %%foo bar diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services9.xml b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services9.xml index 338a61ad28..6973da85ca 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/xml/services9.xml +++ b/tests/fixtures/Symfony/Components/DependencyInjection/xml/services9.xml @@ -1,6 +1,8 @@ - + BazClass FooClass From a1d2230c6122f9149904b651cbf1337e76b1a39d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 12 Jan 2010 16:00:32 +0100 Subject: [PATCH 05/10] [Foundation] updated the autoloader, added a new one --- src/Symfony/Foundation/ClassLoader.php | 77 +++------ .../Foundation/UniversalClassLoader.php | 147 ++++++++++++++++++ 2 files changed, 166 insertions(+), 58 deletions(-) create mode 100644 src/Symfony/Foundation/UniversalClassLoader.php diff --git a/src/Symfony/Foundation/ClassLoader.php b/src/Symfony/Foundation/ClassLoader.php index 081776a2ec..27eaad343c 100644 --- a/src/Symfony/Foundation/ClassLoader.php +++ b/src/Symfony/Foundation/ClassLoader.php @@ -15,7 +15,7 @@ namespace Symfony\Foundation; * ClassLoader implementation that implements the technical interoperability * standards for PHP 5.3 namespaces and class names. * - * Based on http://groups.google.com/group/php-standards/web/final-proposal + * Based on http://groups.google.com/group/php-standards/web/psr-0-final-proposal * * Example usage: * @@ -42,19 +42,7 @@ class ClassLoader */ public function registerNamespace($namespace, $includePath = null) { - if (!isset($this->namespaces[$namespace])) - { - $this->namespaces[$namespace] = $includePath; - } - else - { - if (!is_array($this->namespaces[$namespace])) - { - $this->namespaces[$namespace] = array($this->namespaces[$namespace]); - } - - $this->namespaces[$namespace][] = $includePath; - } + $this->namespaces[$namespace] = $includePath; } /** @@ -65,14 +53,6 @@ class ClassLoader spl_autoload_register(array($this, 'loadClass')); } - /** - * Uninstalls this class loader from the SPL autoloader stack. - */ - public function unregister() - { - spl_autoload_unregister(array($this, 'loadClass')); - } - /** * Loads the given class or interface. * @@ -81,43 +61,24 @@ class ClassLoader public function loadClass($className) { $vendor = substr($className, 0, stripos($className, '\\')); - if ($vendor || isset($this->namespaces[''])) + if (!isset($this->namespaces[$vendor])) { - $fileName = ''; - $namespace = ''; - if (false !== ($lastNsPos = strripos($className, '\\'))) - { - $namespace = substr($className, 0, $lastNsPos); - $className = substr($className, $lastNsPos + 1); - $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR; - } - $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php'; - - if (null !== $this->namespaces[$vendor]) - { - if (is_array($this->namespaces[$vendor])) - { - foreach ($this->namespaces[$vendor] as $dir) - { - if (!file_exists($dir.DIRECTORY_SEPARATOR.$fileName)) - { - continue; - } - - require $dir.DIRECTORY_SEPARATOR.$fileName; - - break; - } - } - else - { - require $this->namespaces[$vendor].DIRECTORY_SEPARATOR.$fileName; - } - } - else - { - require $fileName; - } + return; } + + if (false !== ($lastNsPos = strripos($className, '\\'))) + { + $namespace = substr($className, 0, $lastNsPos); + $className = substr($className, $lastNsPos + 1); + $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR; + } + else + { + $namespace = ''; + $fileName = ''; + } + $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php'; + + require $this->namespaces[$vendor].DIRECTORY_SEPARATOR.$fileName; } } diff --git a/src/Symfony/Foundation/UniversalClassLoader.php b/src/Symfony/Foundation/UniversalClassLoader.php new file mode 100644 index 0000000000..5cffc6b0e0 --- /dev/null +++ b/src/Symfony/Foundation/UniversalClassLoader.php @@ -0,0 +1,147 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * UniversalClassLoader implements a "universal" autoloder for PHP 5.3. + * + * It is able to load classes that use either: + * + * * The technical interoperability standards for PHP 5.3 namespaces and + * class names (http://groups.google.com/group/php-standards/web/final-proposal); + * + * * The PEAR naming convention for classes (http://pear.php.net/). + * + * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be + * looked for in a list of locations to ease the vendoring of a sub-set of + * classes for large projects. + * + * Example usage: + * + * $loader = new ClassLoader(); + * + * // register classes with namespaces + * $loader->registerNamespaces(array( + * 'Symfony\Components' => __DIR__.'/components', + * 'Symfony' => __DIR__.'/framework', + * )); + * + * // register a library using the PEAR naming convention + * $loader->registerClasses(array( + * 'Swift_' => __DIR__.'/Swift', + * )); + * + * // activate the autoloader + * $loader->register(); + * + * In this example, if you try to use a class in the Symfony\Components + * namespace or one of its children (Symfony\Components\Console for instance), + * the autoloader will first look for the class under the components/ + * directory, and it will then fallback to the framework/ directory if not + * found before giving up. + * + * @package symfony + * @subpackage foundation + * @author Fabien Potencier + */ +class UniversalClassLoader +{ + protected $namespaces = array(); + protected $prefixes = array(); + + /** + * Registers an array of namespaces + * + * @param array $namespaces An array of namespaces (namespaces as keys and locations as values) + */ + public function registerNamespaces(array $namespaces) + { + $this->namespaces = array_merge($this->namespaces, $namespaces); + } + + /** + * Registers a namespace. + * + * @param string $namespace The namespace + * @param string $path The location of the namespace + */ + public function registerNamespace($namespace, $path) + { + $this->namespaces[$namespace] = $path; + } + + /** + * Registers an array of classes using the PEAR naming convention. + * + * @param array $classes An array of classes (prefixes as keys and locations as values) + */ + public function registerPrefixes(array $classes) + { + $this->prefixes = array_merge($this->prefixes, $classes); + } + + /** + * Registers a set of classes using the PEAR naming convention. + * + * @param string $prefix The classes prefix + * @param string $path The location of the classes + */ + public function registerPrefix($prefix, $path) + { + $this->prefixes[$prefix] = $path; + } + + /** + * Registers this instance as an autoloader. + */ + public function register() + { + spl_autoload_register(array($this, 'loadClass')); + } + + /** + * Loads the given class or interface. + * + * @param string $class The name of the class + */ + public function loadClass($class) + { + if (false !== ($pos = strripos($class, '\\'))) + { + // namespaced class name + $namespace = substr($class, 0, $pos); + foreach ($this->namespaces as $ns => $dir) + { + if (0 === strpos($namespace, $ns)) + { + $class = substr($class, $pos + 1); + require $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php'; + + return; + } + } + } + else + { + // PEAR-like class name + foreach ($this->prefixes as $prefix => $dir) + { + if (0 === strpos($class, $prefix)) + { + require $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php'; + + return; + } + } + } + } +} From 59e38a7460bee86eadbdc2d3dd15542b704e6d1e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 14 Jan 2010 10:45:37 +0100 Subject: [PATCH 06/10] [Console] added a Shell --- .../Components/Console/Command/Command.php | 10 ++ src/Symfony/Components/Console/Shell.php | 146 ++++++++++++++++++ .../Components/Console/TestCommand.php | 5 - .../Console/Command/CommandTest.php | 4 +- 4 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Components/Console/Shell.php diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 19b42a131d..013b239cbf 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -196,6 +196,16 @@ class Command return $this; } + /** + * Gets the InputDefinition attached to this Command. + * + * @return InputDefinition $definition An InputDefinition instance + */ + public function getDefinition() + { + return $this->definition; + } + /** * Adds an argument. * diff --git a/src/Symfony/Components/Console/Shell.php b/src/Symfony/Components/Console/Shell.php new file mode 100644 index 0000000000..08e0152ffc --- /dev/null +++ b/src/Symfony/Components/Console/Shell.php @@ -0,0 +1,146 @@ + + * + * 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 + */ +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('The command terminated with an error status (%s)', $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 <<{$this->application->getName()} shell. + +At the prompt, type help for some help, +or list to get a list available commands. + +To exist the shell, type ^D. + +EOF; + } +} diff --git a/tests/fixtures/Symfony/Components/Console/TestCommand.php b/tests/fixtures/Symfony/Components/Console/TestCommand.php index b42544f753..c0c02dd280 100644 --- a/tests/fixtures/Symfony/Components/Console/TestCommand.php +++ b/tests/fixtures/Symfony/Components/Console/TestCommand.php @@ -26,11 +26,6 @@ class TestCommand extends Command return $this->application; } - public function getDefinition() - { - return $this->definition; - } - protected function execute(InputInterface $input, OutputInterface $output) { $output->write('execute called'); diff --git a/tests/unit/Symfony/Components/Console/Command/CommandTest.php b/tests/unit/Symfony/Components/Console/Command/CommandTest.php index e3c9215775..b3f734b46f 100644 --- a/tests/unit/Symfony/Components/Console/Command/CommandTest.php +++ b/tests/unit/Symfony/Components/Console/Command/CommandTest.php @@ -50,8 +50,8 @@ $command = new TestCommand(); $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'); From 4dd35ff8cc6811b381d195c97185c57003958ec0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Jan 2010 14:39:52 +0100 Subject: [PATCH 07/10] [Console] changed the subpackage from cli to console --- src/Symfony/Components/Console/Application.php | 2 +- src/Symfony/Components/Console/Command/Command.php | 2 +- src/Symfony/Components/Console/Command/HelpCommand.php | 2 +- src/Symfony/Components/Console/Command/ListCommand.php | 2 +- src/Symfony/Components/Console/Input/ArgvInput.php | 2 +- src/Symfony/Components/Console/Input/ArrayInput.php | 2 +- src/Symfony/Components/Console/Input/Input.php | 2 +- src/Symfony/Components/Console/Input/InputArgument.php | 2 +- src/Symfony/Components/Console/Input/InputDefinition.php | 2 +- src/Symfony/Components/Console/Input/InputInterface.php | 2 +- src/Symfony/Components/Console/Input/InputOption.php | 2 +- src/Symfony/Components/Console/Input/StringInput.php | 2 +- src/Symfony/Components/Console/Output/ConsoleOutput.php | 2 +- src/Symfony/Components/Console/Output/Formatter.php | 2 +- src/Symfony/Components/Console/Output/NullOutput.php | 2 +- src/Symfony/Components/Console/Output/Output.php | 2 +- src/Symfony/Components/Console/Output/OutputInterface.php | 2 +- src/Symfony/Components/Console/Output/StreamOutput.php | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Components/Console/Application.php b/src/Symfony/Components/Console/Application.php index 6752a792a0..4afd90d9aa 100644 --- a/src/Symfony/Components/Console/Application.php +++ b/src/Symfony/Components/Console/Application.php @@ -38,7 +38,7 @@ use Symfony\Components\Console\Command\ListCommand; * $app->run(); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class Application diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 68385097ef..67dca262d6 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -23,7 +23,7 @@ use Symfony\Components\Console\Application; * Base class for all commands. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class Command diff --git a/src/Symfony/Components/Console/Command/HelpCommand.php b/src/Symfony/Components/Console/Command/HelpCommand.php index af8aa2926d..9b9a0942e5 100644 --- a/src/Symfony/Components/Console/Command/HelpCommand.php +++ b/src/Symfony/Components/Console/Command/HelpCommand.php @@ -22,7 +22,7 @@ use Symfony\Components\Console\Command\Command; * HelpCommand displays the help for a given command. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class HelpCommand extends Command diff --git a/src/Symfony/Components/Console/Command/ListCommand.php b/src/Symfony/Components/Console/Command/ListCommand.php index 293b20e9a7..998cda8f19 100644 --- a/src/Symfony/Components/Console/Command/ListCommand.php +++ b/src/Symfony/Components/Console/Command/ListCommand.php @@ -22,7 +22,7 @@ use Symfony\Components\Console\Command\Command; * ListCommand displays the list of all available commands for the application. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class ListCommand extends Command diff --git a/src/Symfony/Components/Console/Input/ArgvInput.php b/src/Symfony/Components/Console/Input/ArgvInput.php index 627d78b084..a140219bfd 100644 --- a/src/Symfony/Components/Console/Input/ArgvInput.php +++ b/src/Symfony/Components/Console/Input/ArgvInput.php @@ -32,7 +32,7 @@ namespace Symfony\Components\Console\Input; * `StringInput` when you want to provide your own input. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier * * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html diff --git a/src/Symfony/Components/Console/Input/ArrayInput.php b/src/Symfony/Components/Console/Input/ArrayInput.php index abea665100..3ae6154d30 100644 --- a/src/Symfony/Components/Console/Input/ArrayInput.php +++ b/src/Symfony/Components/Console/Input/ArrayInput.php @@ -19,7 +19,7 @@ namespace Symfony\Components\Console\Input; * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar')); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class ArrayInput extends Input diff --git a/src/Symfony/Components/Console/Input/Input.php b/src/Symfony/Components/Console/Input/Input.php index 53b1f87569..a203313dd1 100644 --- a/src/Symfony/Components/Console/Input/Input.php +++ b/src/Symfony/Components/Console/Input/Input.php @@ -21,7 +21,7 @@ namespace Symfony\Components\Console\Input; * * `ArrayInput`: The input is provided as an array * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ abstract class Input implements InputInterface diff --git a/src/Symfony/Components/Console/Input/InputArgument.php b/src/Symfony/Components/Console/Input/InputArgument.php index ff8a90e36d..9b5925b4bf 100644 --- a/src/Symfony/Components/Console/Input/InputArgument.php +++ b/src/Symfony/Components/Console/Input/InputArgument.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Input; * Represents a command line argument. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class InputArgument diff --git a/src/Symfony/Components/Console/Input/InputDefinition.php b/src/Symfony/Components/Console/Input/InputDefinition.php index bcfe2a7841..2977e7fd6d 100644 --- a/src/Symfony/Components/Console/Input/InputDefinition.php +++ b/src/Symfony/Components/Console/Input/InputDefinition.php @@ -22,7 +22,7 @@ namespace Symfony\Components\Console\Input; * )); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class InputDefinition diff --git a/src/Symfony/Components/Console/Input/InputInterface.php b/src/Symfony/Components/Console/Input/InputInterface.php index eaf01af7c6..953553e780 100644 --- a/src/Symfony/Components/Console/Input/InputInterface.php +++ b/src/Symfony/Components/Console/Input/InputInterface.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Input; * InputInterface is the interface implemented by all input classes. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ interface InputInterface diff --git a/src/Symfony/Components/Console/Input/InputOption.php b/src/Symfony/Components/Console/Input/InputOption.php index f5d7e98448..46a47d0d57 100644 --- a/src/Symfony/Components/Console/Input/InputOption.php +++ b/src/Symfony/Components/Console/Input/InputOption.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Input; * Represents a command line option. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class InputOption diff --git a/src/Symfony/Components/Console/Input/StringInput.php b/src/Symfony/Components/Console/Input/StringInput.php index 2275bd5d12..c66f7b9dc4 100644 --- a/src/Symfony/Components/Console/Input/StringInput.php +++ b/src/Symfony/Components/Console/Input/StringInput.php @@ -19,7 +19,7 @@ namespace Symfony\Components\Console\Input; * $input = new StringInput('foo --bar="foobar"'); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class StringInput extends ArgvInput diff --git a/src/Symfony/Components/Console/Output/ConsoleOutput.php b/src/Symfony/Components/Console/Output/ConsoleOutput.php index 332fa5125d..7950c25ec0 100644 --- a/src/Symfony/Components/Console/Output/ConsoleOutput.php +++ b/src/Symfony/Components/Console/Output/ConsoleOutput.php @@ -23,7 +23,7 @@ namespace Symfony\Components\Console\Output; * $output = new StreamOutput(fopen('php://stdout', 'w')); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class ConsoleOutput extends StreamOutput diff --git a/src/Symfony/Components/Console/Output/Formatter.php b/src/Symfony/Components/Console/Output/Formatter.php index 8a1efd0e4b..3ea0a911c6 100644 --- a/src/Symfony/Components/Console/Output/Formatter.php +++ b/src/Symfony/Components/Console/Output/Formatter.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Output; * The Formatter class provides helpers to format messages. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class Formatter diff --git a/src/Symfony/Components/Console/Output/NullOutput.php b/src/Symfony/Components/Console/Output/NullOutput.php index 1f0ec21806..b43a1c616a 100644 --- a/src/Symfony/Components/Console/Output/NullOutput.php +++ b/src/Symfony/Components/Console/Output/NullOutput.php @@ -17,7 +17,7 @@ namespace Symfony\Components\Console\Output; * $output = new NullOutput(); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class NullOutput extends Output diff --git a/src/Symfony/Components/Console/Output/Output.php b/src/Symfony/Components/Console/Output/Output.php index b487a63220..9917c3b534 100644 --- a/src/Symfony/Components/Console/Output/Output.php +++ b/src/Symfony/Components/Console/Output/Output.php @@ -21,7 +21,7 @@ namespace Symfony\Components\Console\Output; * * quiet: -q (no output) * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ abstract class Output implements OutputInterface diff --git a/src/Symfony/Components/Console/Output/OutputInterface.php b/src/Symfony/Components/Console/Output/OutputInterface.php index e619b3e861..26bc6b76ef 100644 --- a/src/Symfony/Components/Console/Output/OutputInterface.php +++ b/src/Symfony/Components/Console/Output/OutputInterface.php @@ -15,7 +15,7 @@ namespace Symfony\Components\Console\Output; * OutputInterface is the interface implemented by all Output classes. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ interface OutputInterface diff --git a/src/Symfony/Components/Console/Output/StreamOutput.php b/src/Symfony/Components/Console/Output/StreamOutput.php index c4bc2bd0df..4360776d75 100644 --- a/src/Symfony/Components/Console/Output/StreamOutput.php +++ b/src/Symfony/Components/Console/Output/StreamOutput.php @@ -23,7 +23,7 @@ namespace Symfony\Components\Console\Output; * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false)); * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ class StreamOutput extends Output From af0f3db1992a1f0e56775892fd0caf5993355401 Mon Sep 17 00:00:00 2001 From: geoffrey Date: Mon, 11 Jan 2010 12:24:08 +0100 Subject: [PATCH 08/10] [Console] implemented helpers and moved formatter and ask* methods to their own helpers --- .../Components/Console/Command/Command.php | 103 +++++---------- .../FormatterHelper.php} | 23 ++-- .../Components/Console/Helper/Helper.php | 45 +++++++ .../Console/Helper/HelperInterface.php | 43 +++++++ .../Components/Console/Helper/HelperSet.php | 105 ++++++++++++++++ .../Console/Helper/InteractHelper.php | 119 ++++++++++++++++++ .../Console/Helper/FormatterHelperTest.php | 28 +++++ .../Console/Output/FormatterTest.php | 26 ---- 8 files changed, 389 insertions(+), 103 deletions(-) rename src/Symfony/Components/Console/{Output/Formatter.php => Helper/FormatterHelper.php} (77%) create mode 100644 src/Symfony/Components/Console/Helper/Helper.php create mode 100644 src/Symfony/Components/Console/Helper/HelperInterface.php create mode 100644 src/Symfony/Components/Console/Helper/HelperSet.php create mode 100644 src/Symfony/Components/Console/Helper/InteractHelper.php create mode 100644 tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Output/FormatterTest.php diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 67dca262d6..7fb727b090 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -7,8 +7,10 @@ use Symfony\Components\Console\Input\InputOption; use Symfony\Components\Console\Input\InputArgument; use Symfony\Components\Console\Input\InputInterface; use Symfony\Components\Console\Output\OutputInterface; -use Symfony\Components\Console\Output\Formatter; use Symfony\Components\Console\Application; +use Symfony\Components\Console\Helper\HelperSet; +use Symfony\Components\Console\Helper\FormatterHelper; +use Symfony\Components\Console\Helper\InteractHelper; /* * This file is part of the symfony framework. @@ -42,15 +44,27 @@ class Command /** * Constructor. + * + * @param string $name + * @param HelperSet $helperSet A helper set instance */ - public function __construct($name = null) + public function __construct($name = null, HelperSet $helperSet = null) { $this->definition = new InputDefinition(); $this->ignoreValidationErrors = false; $this->applicationDefinitionMerged = false; - $this->formatter = new Formatter(); $this->aliases = array(); + if (null === $helperSet) + { + $helperSet = new HelperSet(array( + new FormatterHelper(), + new InteractHelper(), + )); + } + + $this->setHelperSet($helperSet); + if (null !== $name) { $this->setName($name); @@ -389,90 +403,39 @@ class Command } /** - * Asks a question to the user. + * Set a helper set to be used with the command. * - * @param OutputInterface $output - * @param string|array $question The question to ask - * @param string $default The default answer if none is given by the user - * - * @param string The user answer + * @param HelperSet $helperSet The helper set */ - static public function ask(OutputInterface $output, $question, $default = null) + public function setHelperSet(HelperSet $helperSet) { - // @codeCoverageIgnoreStart - $output->write($question); + $this->helperSet = $helperSet; - $ret = trim(fgets(STDIN)); - - return $ret ? $ret : $default; - // @codeCoverageIgnoreEnd + $helperSet->setCommand($this); } /** - * Asks a confirmation to the user. + * Get the helper set associated with the command * - * The question will be asked until the user answer by nothing, yes, or no. - * - * @param OutputInterface $output - * @param string|array $question The question to ask - * @param Boolean $default The default answer if the user enters nothing - * - * @param Boolean true if the user has confirmed, false otherwise + * @return HelperSet */ - static public function askConfirmation(OutputInterface $output, $question, $default = true) + public function getHelperSet() { - // @codeCoverageIgnoreStart - $answer = 'z'; - while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) - { - $answer = static::ask($output, $question); - } - - if (false === $default) - { - return $answer && 'y' == strtolower($answer[0]); - } - else - { - return !$answer || 'y' == strtolower($answer[0]); - } - // @codeCoverageIgnoreEnd + return $this->helperSet; } /** - * Asks for a value and validates the response. + * Gets a helper value. * - * @param OutputInterface $output - * @param string|array $question - * @param Closure $validator - * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite) + * @param string $name The helper name * - * @return mixed + * @return mixed The helper value + * + * @throws \InvalidArgumentException if the helper is not defined */ - static public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false) + public function __get($name) { - // @codeCoverageIgnoreStart - $error = null; - while (false === $attempts || $attempts--) - { - if (null !== $error) - { - $output->write($this->formatter->formatBlock($error->getMessage(), 'error')); - } - - $value = static::ask($output, $question, null); - - try - { - return $validator($value); - } - catch (\Exception $error) - { - } - } - - throw $error; - // @codeCoverageIgnoreEnd + return $this->helperSet->get($name); } /** diff --git a/src/Symfony/Components/Console/Output/Formatter.php b/src/Symfony/Components/Console/Helper/FormatterHelper.php similarity index 77% rename from src/Symfony/Components/Console/Output/Formatter.php rename to src/Symfony/Components/Console/Helper/FormatterHelper.php index 3ea0a911c6..a91064b181 100644 --- a/src/Symfony/Components/Console/Output/Formatter.php +++ b/src/Symfony/Components/Console/Helper/FormatterHelper.php @@ -1,6 +1,6 @@ */ -class Formatter +class FormatterHelper extends Helper { + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'formatter'; + } + /** * Formats a message within a section. * @@ -27,7 +35,7 @@ class Formatter * @param string $message The message * @param string $style The style to apply to the section */ - static public function formatSection($section, $message, $style = 'info') + public function formatSection($section, $message, $style = 'info') { return sprintf("<%s>[%s] %s", $style, $section, $style, $message); } @@ -41,7 +49,7 @@ class Formatter * * @return string The formatter message */ - static public function formatBlock($messages, $style, $large = false) + public function formatBlock($messages, $style, $large = false) { if (!is_array($messages)) { @@ -53,13 +61,13 @@ class Formatter foreach ($messages as $message) { $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); - $len = max(static::strlen($message) + ($large ? 4 : 2), $len); + $len = max($this->strlen($message) + ($large ? 4 : 2), $len); } $messages = $large ? array(str_repeat(' ', $len)) : array(); foreach ($lines as $line) { - $messages[] = $line.str_repeat(' ', $len - static::strlen($line)); + $messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); } if ($large) { @@ -74,8 +82,9 @@ class Formatter return implode("\n", $messages); } - static protected function strlen($string) + protected function strlen($string) { return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string); } } + diff --git a/src/Symfony/Components/Console/Helper/Helper.php b/src/Symfony/Components/Console/Helper/Helper.php new file mode 100644 index 0000000000..642e7a67c7 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/Helper.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Helper is the base class for all helper classes. + * + * @package symfony + * @subpackage templating + * @author Fabien Potencier + */ +abstract class Helper implements HelperInterface +{ + protected + $helperSet = null; + + /** + * Sets the helper set associated with this helper. + * + * @param HelperSet $helperSet A HelperSet instance + */ + public function setHelperSet(HelperSet $helperSet = null) + { + $this->helperSet = $helperSet; + } + + /** + * Gets the helper set associated with this helper. + * + * @return HelperSet A HelperSet instance + */ + public function getHelperSet() + { + return $this->helperSet; + } +} diff --git a/src/Symfony/Components/Console/Helper/HelperInterface.php b/src/Symfony/Components/Console/Helper/HelperInterface.php new file mode 100644 index 0000000000..a3474fc610 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/HelperInterface.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * HelperInterface is the interface all helpers must implement. + * + * @package symfony + * @subpackage templating + * @author Fabien Potencier + */ +interface HelperInterface +{ + /** + * Returns the canonical name of this helper. + * + * @return string The canonical name + */ + function getName(); + + /** + * Sets the helper set associated with this helper. + * + * @param HelperSet $helperSet A HelperSet instance + */ + function setHelperSet(HelperSet $helperSet = null); + + /** + * Gets the helper set associated with this helper. + * + * @return HelperSet A HelperSet instance + */ + function getHelperSet(); +} diff --git a/src/Symfony/Components/Console/Helper/HelperSet.php b/src/Symfony/Components/Console/Helper/HelperSet.php new file mode 100644 index 0000000000..b71e9bd1b7 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/HelperSet.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * HelperSet represents a set of helpers to be used with a command. + * + * @package symfony + * @subpackage templating + * @author Fabien Potencier + */ +class HelperSet +{ + protected + $helpers = array(), + $command = null; + + public function __construct(array $helpers = array()) + { + foreach ($helpers as $alias => $helper) + { + $this->set($helper, is_int($alias) ? null : $alias); + } + } + + /** + * Sets a helper. + * + * @param HelperInterface $value The helper instance + * @param string $alias An alias + */ + public function set(HelperInterface $helper, $alias = null) + { + $this->helpers[$helper->getName()] = $helper; + if (null !== $alias) + { + $this->helpers[$alias] = $helper; + } + + $helper->setHelperSet($this); + } + + /** + * Returns true if the helper if defined. + * + * @param string $name The helper name + * + * @return Boolean true if the helper is defined, false otherwise + */ + public function has($name) + { + return isset($this->helpers[$name]); + } + + /** + * Gets a helper value. + * + * @param string $name The helper name + * + * @return HelperInterface The helper instance + * + * @throws \InvalidArgumentException if the helper is not defined + */ + public function get($name) + { + if (!$this->has($name)) + { + throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); + } + + return $this->helpers[$name]; + } + + /** + * Sets the command associated with this helper set. + * + * @param Command $command A Command instance + */ + public function setCommand(Command $command = null) + { + $this->command = $command; + } + + /** + * Gets the command associated with this helper set. + * + * @return Command A Command instance + */ + public function getCommand() + { + return $this->command; + } +} + diff --git a/src/Symfony/Components/Console/Helper/InteractHelper.php b/src/Symfony/Components/Console/Helper/InteractHelper.php new file mode 100644 index 0000000000..0054ab08d3 --- /dev/null +++ b/src/Symfony/Components/Console/Helper/InteractHelper.php @@ -0,0 +1,119 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * The Interact class provides helpers to interact with the user. + * + * @package symfony + * @subpackage cli + * @author Fabien Potencier + */ +class InteractHelper extends Helper +{ + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'interact'; + } + + /** + * Asks a question to the user. + * + * @param OutputInterface $output + * @param string|array $question The question to ask + * @param string $default The default answer if none is given by the user + * + * @param string The user answer + */ + public function ask(OutputInterface $output, $question, $default = null) + { + // @codeCoverageIgnoreStart + $output->write($question); + + $ret = trim(fgets(STDIN)); + + return $ret ? $ret : $default; + // @codeCoverageIgnoreEnd + } + + /** + * Asks a confirmation to the user. + * + * The question will be asked until the user answer by nothing, yes, or no. + * + * @param OutputInterface $output + * @param string|array $question The question to ask + * @param Boolean $default The default answer if the user enters nothing + * + * @param Boolean true if the user has confirmed, false otherwise + */ + public function askConfirmation(OutputInterface $output, $question, $default = true) + { + // @codeCoverageIgnoreStart + $answer = 'z'; + while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) + { + $answer = $this->ask($output, $question); + } + + if (false === $default) + { + return $answer && 'y' == strtolower($answer[0]); + } + else + { + return !$answer || 'y' == strtolower($answer[0]); + } + // @codeCoverageIgnoreEnd + } + + /** + * Asks for a value and validates the response. + * + * @param OutputInterface $output + * @param string|array $question + * @param Closure $validator + * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite) + * + * @return mixed + */ + public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false) + { + // @codeCoverageIgnoreStart + $error = null; + while (false === $attempts || $attempts--) + { + if (null !== $error) + { + $output->write($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); + } + + $value = $this->ask($output, $question, null); + + try + { + return $validator($value); + } + catch (\Exception $error) + { + } + } + + throw $error; + // @codeCoverageIgnoreEnd + } +} diff --git a/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php b/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php new file mode 100644 index 0000000000..0fd2199b61 --- /dev/null +++ b/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once __DIR__.'/../../../../bootstrap.php'; + +use Symfony\Components\Console\Helper\FormatterHelper; + +$formatter = new FormatterHelper(); + +$t = new LimeTest(4); + +// ::formatSection() +$t->diag('::formatSection()'); +$t->is($formatter->formatSection('cli', 'Some text to display'), '[cli] Some text to display', '::formatSection() formats a message in a section'); + +// ::formatBlock() +$t->diag('::formatBlock()'); +$t->is($formatter->formatBlock('Some text to display', 'error'), ' Some text to display ', '::formatBlock() formats a message in a block'); +$t->is($formatter->formatBlock(array('Some text to display', 'foo bar'), 'error'), " Some text to display \n foo bar ", '::formatBlock() formats a message in a block'); + +$t->is($formatter->formatBlock('Some text to display', 'error', true), " \n Some text to display \n ", '::formatBlock() formats a message in a block'); diff --git a/tests/unit/Symfony/Components/Console/Output/FormatterTest.php b/tests/unit/Symfony/Components/Console/Output/FormatterTest.php deleted file mode 100644 index 401706e13d..0000000000 --- a/tests/unit/Symfony/Components/Console/Output/FormatterTest.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Output\Formatter; - -$t = new LimeTest(4); - -// ::formatSection() -$t->diag('::formatSection()'); -$t->is(Formatter::formatSection('cli', 'Some text to display'), '[cli] Some text to display', '::formatSection() formats a message in a section'); - -// ::formatBlock() -$t->diag('::formatBlock()'); -$t->is(Formatter::formatBlock('Some text to display', 'error'), ' Some text to display ', '::formatBlock() formats a message in a block'); -$t->is(Formatter::formatBlock(array('Some text to display', 'foo bar'), 'error'), " Some text to display \n foo bar ", '::formatBlock() formats a message in a block'); - -$t->is(Formatter::formatBlock('Some text to display', 'error', true), " \n Some text to display \n ", '::formatBlock() formats a message in a block'); From ba446d58c729a0661c277fa102079b6db405a4f4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Jan 2010 14:46:11 +0100 Subject: [PATCH 09/10] [Console] refactored helpers --- .../Components/Console/Application.php | 28 +++++++++++ .../Components/Console/Command/Command.php | 48 +++---------------- .../{InteractHelper.php => DialogHelper.php} | 22 ++++----- .../Console/Helper/FormatterHelper.php | 17 ++++--- .../Components/Console/Helper/Helper.php | 8 ++-- .../Console/Helper/HelperInterface.php | 22 ++++----- .../Components/Console/Helper/HelperSet.php | 9 ++-- 7 files changed, 72 insertions(+), 82 deletions(-) rename src/Symfony/Components/Console/Helper/{InteractHelper.php => DialogHelper.php} (95%) diff --git a/src/Symfony/Components/Console/Application.php b/src/Symfony/Components/Console/Application.php index 4afd90d9aa..7b1a8187fc 100644 --- a/src/Symfony/Components/Console/Application.php +++ b/src/Symfony/Components/Console/Application.php @@ -14,6 +14,9 @@ use Symfony\Components\Console\Output\ConsoleOutput; use Symfony\Components\Console\Command\Command; use Symfony\Components\Console\Command\HelpCommand; use Symfony\Components\Console\Command\ListCommand; +use Symfony\Components\Console\Helper\HelperSet; +use Symfony\Components\Console\Helper\FormatterHelper; +use Symfony\Components\Console\Helper\DialogHelper; /* * This file is part of the symfony framework. @@ -53,6 +56,7 @@ class Application protected $catchExceptions; protected $autoExit; protected $definition; + protected $helperSet; /** * Constructor. @@ -68,6 +72,10 @@ class Application $this->autoExit = true; $this->commands = array(); $this->aliases = array(); + $this->helperSet = new HelperSet(array( + new FormatterHelper(), + new DialogHelper(), + )); $this->addCommand(new HelpCommand()); $this->addCommand(new ListCommand()); @@ -200,6 +208,26 @@ class Application return is_numeric($statusCode) ? $statusCode : 0; } + /** + * Set a helper set to be used with the command. + * + * @param HelperSet $helperSet The helper set + */ + public function setHelperSet(HelperSet $helperSet) + { + $this->helperSet = $helperSet; + } + + /** + * Get the helper set associated with the command + * + * @return HelperSet The HelperSet isntance associated with this command + */ + public function getHelperSet() + { + return $this->helperSet; + } + /** * Gets the InputDefinition related to this Application. * diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index 7fb727b090..013b239cbf 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -8,9 +8,6 @@ use Symfony\Components\Console\Input\InputArgument; use Symfony\Components\Console\Input\InputInterface; use Symfony\Components\Console\Output\OutputInterface; use Symfony\Components\Console\Application; -use Symfony\Components\Console\Helper\HelperSet; -use Symfony\Components\Console\Helper\FormatterHelper; -use Symfony\Components\Console\Helper\InteractHelper; /* * This file is part of the symfony framework. @@ -45,26 +42,15 @@ class Command /** * Constructor. * - * @param string $name - * @param HelperSet $helperSet A helper set instance + * @param string $name The name of the command */ - public function __construct($name = null, HelperSet $helperSet = null) + public function __construct($name = null) { $this->definition = new InputDefinition(); $this->ignoreValidationErrors = false; $this->applicationDefinitionMerged = false; $this->aliases = array(); - if (null === $helperSet) - { - $helperSet = new HelperSet(array( - new FormatterHelper(), - new InteractHelper(), - )); - } - - $this->setHelperSet($helperSet); - if (null !== $name) { $this->setName($name); @@ -403,39 +389,17 @@ class Command } /** - * Set a helper set to be used with the command. + * Gets a helper instance by name. * - * @param HelperSet $helperSet The helper set - */ - public function setHelperSet(HelperSet $helperSet) - { - $this->helperSet = $helperSet; - - $helperSet->setCommand($this); - } - - /** - * Get the helper set associated with the command - * - * @return HelperSet - */ - public function getHelperSet() - { - return $this->helperSet; - } - - /** - * Gets a helper value. - * - * @param string $name The helper name + * @param string $name The helper name * * @return mixed The helper value * * @throws \InvalidArgumentException if the helper is not defined */ - public function __get($name) + protected function getHelper($name) { - return $this->helperSet->get($name); + return $this->application->getHelperSet()->get($name); } /** diff --git a/src/Symfony/Components/Console/Helper/InteractHelper.php b/src/Symfony/Components/Console/Helper/DialogHelper.php similarity index 95% rename from src/Symfony/Components/Console/Helper/InteractHelper.php rename to src/Symfony/Components/Console/Helper/DialogHelper.php index 0054ab08d3..21b55cef03 100644 --- a/src/Symfony/Components/Console/Helper/InteractHelper.php +++ b/src/Symfony/Components/Console/Helper/DialogHelper.php @@ -14,22 +14,14 @@ use Symfony\Components\Console\Output\OutputInterface; */ /** - * The Interact class provides helpers to interact with the user. + * The Dialog class provides helpers to interact with the user. * * @package symfony - * @subpackage cli + * @subpackage console * @author Fabien Potencier */ -class InteractHelper extends Helper +class DialogHelper extends Helper { - /** - * Returns the helper's canonical name - */ - public function getName() - { - return 'interact'; - } - /** * Asks a question to the user. * @@ -116,4 +108,12 @@ class InteractHelper extends Helper throw $error; // @codeCoverageIgnoreEnd } + + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'dialog'; + } } diff --git a/src/Symfony/Components/Console/Helper/FormatterHelper.php b/src/Symfony/Components/Console/Helper/FormatterHelper.php index a91064b181..6eedca733a 100644 --- a/src/Symfony/Components/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Components/Console/Helper/FormatterHelper.php @@ -20,14 +20,6 @@ namespace Symfony\Components\Console\Helper; */ class FormatterHelper extends Helper { - /** - * Returns the helper's canonical name - */ - public function getName() - { - return 'formatter'; - } - /** * Formats a message within a section. * @@ -86,5 +78,12 @@ class FormatterHelper extends Helper { return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string); } -} + /** + * Returns the helper's canonical name + */ + public function getName() + { + return 'formatter'; + } +} diff --git a/src/Symfony/Components/Console/Helper/Helper.php b/src/Symfony/Components/Console/Helper/Helper.php index 642e7a67c7..f083b47abe 100644 --- a/src/Symfony/Components/Console/Helper/Helper.php +++ b/src/Symfony/Components/Console/Helper/Helper.php @@ -3,19 +3,19 @@ namespace Symfony\Components\Console\Helper; /* - * This file is part of the symfony package. + * This file is part of the symfony framework. * * (c) Fabien Potencier * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. */ /** * Helper is the base class for all helper classes. * * @package symfony - * @subpackage templating + * @subpackage console * @author Fabien Potencier */ abstract class Helper implements HelperInterface diff --git a/src/Symfony/Components/Console/Helper/HelperInterface.php b/src/Symfony/Components/Console/Helper/HelperInterface.php index a3474fc610..f3de9aa65e 100644 --- a/src/Symfony/Components/Console/Helper/HelperInterface.php +++ b/src/Symfony/Components/Console/Helper/HelperInterface.php @@ -3,30 +3,23 @@ namespace Symfony\Components\Console\Helper; /* - * This file is part of the symfony package. + * This file is part of the symfony framework. * * (c) Fabien Potencier * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. */ /** * HelperInterface is the interface all helpers must implement. * * @package symfony - * @subpackage templating + * @subpackage console * @author Fabien Potencier */ interface HelperInterface { - /** - * Returns the canonical name of this helper. - * - * @return string The canonical name - */ - function getName(); - /** * Sets the helper set associated with this helper. * @@ -40,4 +33,11 @@ interface HelperInterface * @return HelperSet A HelperSet instance */ function getHelperSet(); + + /** + * Returns the canonical name of this helper. + * + * @return string The canonical name + */ + function getName(); } diff --git a/src/Symfony/Components/Console/Helper/HelperSet.php b/src/Symfony/Components/Console/Helper/HelperSet.php index b71e9bd1b7..67d764f52e 100644 --- a/src/Symfony/Components/Console/Helper/HelperSet.php +++ b/src/Symfony/Components/Console/Helper/HelperSet.php @@ -5,19 +5,19 @@ namespace Symfony\Components\Console\Helper; use Symfony\Components\Console\Command\Command; /* - * This file is part of the symfony package. + * This file is part of the symfony framework. * * (c) Fabien Potencier * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. */ /** * HelperSet represents a set of helpers to be used with a command. * * @package symfony - * @subpackage templating + * @subpackage console * @author Fabien Potencier */ class HelperSet @@ -102,4 +102,3 @@ class HelperSet return $this->command; } } - From c5004746d02b4aed0c1977f85c0bb2e211d2bbcd Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 14 Jan 2010 10:45:37 +0100 Subject: [PATCH 10/10] [Console] added a Shell --- src/Symfony/Components/Console/Shell.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Components/Console/Shell.php b/src/Symfony/Components/Console/Shell.php index a57ce6453f..08e0152ffc 100644 --- a/src/Symfony/Components/Console/Shell.php +++ b/src/Symfony/Components/Console/Shell.php @@ -139,7 +139,7 @@ Welcome to the {$this->application->getName()} shell. At the prompt, type help for some help, or list to get a list available commands. -To exit the shell, type ^D. +To exist the shell, type ^D. EOF; }