Skip to content

Commit

Permalink
[Console] implemented helpers and moved formatter and ask* methods to…
Browse files Browse the repository at this point in the history
… their own helpers
  • Loading branch information
geoffrey authored and fabpot committed Jan 11, 2010
1 parent 125bf71 commit afcb3c9
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 103 deletions.
103 changes: 33 additions & 70 deletions src/Symfony/Components/Console/Command/Command.php
Expand Up @@ -7,8 +7,10 @@
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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -379,90 +393,39 @@ public function getSynopsis()
}

/**
* 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
* Set a helper set to be used with the command.
*
* @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.
*
* The question will be asked until the user answer by nothing, yes, or no.
* Get the helper set associated with the command
*
* @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);
}

/**
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace Symfony\Components\Console\Output;
namespace Symfony\Components\Console\Helper;

/*
* This file is part of the symfony framework.
Expand All @@ -18,16 +18,24 @@
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Formatter
class FormatterHelper extends Helper
{
/**
* Returns the helper's canonical name
*/
public function getName()
{
return 'formatter';
}

/**
* Formats a message within a section.
*
* @param string $section The section name
* @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> %s", $style, $section, $style, $message);
}
Expand All @@ -41,7 +49,7 @@ static public function formatSection($section, $message, $style = 'info')
*
* @return string The formatter message
*/
static public function formatBlock($messages, $style, $large = false)
public function formatBlock($messages, $style, $large = false)
{
if (!is_array($messages))
{
Expand All @@ -53,13 +61,13 @@ static public function formatBlock($messages, $style, $large = false)
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)
{
Expand All @@ -74,8 +82,9 @@ static public function formatBlock($messages, $style, $large = false)
return implode("\n", $messages);
}

static protected function strlen($string)
protected function strlen($string)
{
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
}
}

45 changes: 45 additions & 0 deletions src/Symfony/Components/Console/Helper/Helper.php
@@ -0,0 +1,45 @@
<?php

namespace Symfony\Components\Console\Helper;

/*
* This file is part of the symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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 <fabien.potencier@symfony-project.com>
*/
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;
}
}
43 changes: 43 additions & 0 deletions src/Symfony/Components/Console/Helper/HelperInterface.php
@@ -0,0 +1,43 @@
<?php

namespace Symfony\Components\Console\Helper;

/*
* This file is part of the symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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 <fabien.potencier@symfony-project.com>
*/
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();
}

0 comments on commit afcb3c9

Please sign in to comment.