Author: fabien
Date: 2010-01-14 12:22:29 +0100 (Thu, 14 Jan 2010)
New Revision: 26614

Added:
   branches/2.0/src/Symfony/Components/Console/Helper/
   branches/2.0/src/Symfony/Components/Console/Helper/DialogHelper.php
   branches/2.0/src/Symfony/Components/Console/Helper/FormatterHelper.php
   branches/2.0/src/Symfony/Components/Console/Helper/Helper.php
   branches/2.0/src/Symfony/Components/Console/Helper/HelperInterface.php
   branches/2.0/src/Symfony/Components/Console/Helper/HelperSet.php
   branches/2.0/tests/unit/Symfony/Components/Console/Helper/
   
branches/2.0/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php
Removed:
   branches/2.0/src/Symfony/Components/Console/Output/Formatter.php
   branches/2.0/tests/unit/Symfony/Components/Console/Output/FormatterTest.php
Modified:
   branches/2.0/src/Symfony/Components/Console/Application.php
   branches/2.0/src/Symfony/Components/Console/Command/Command.php
Log:
Merge branch 'master' of git://github.com/symfony/symfony

Modified: branches/2.0/src/Symfony/Components/Console/Application.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Application.php 2010-01-14 
11:22:25 UTC (rev 26613)
+++ branches/2.0/src/Symfony/Components/Console/Application.php 2010-01-14 
11:22:29 UTC (rev 26614)
@@ -14,6 +14,9 @@
 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 @@
   protected $catchExceptions;
   protected $autoExit;
   protected $definition;
+  protected $helperSet;
 
   /**
    * Constructor.
@@ -68,6 +72,10 @@
     $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());
@@ -201,6 +209,26 @@
   }
 
   /**
+   * 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.
    *
    * @return InputDefinition The InputDefinition instance

Modified: branches/2.0/src/Symfony/Components/Console/Command/Command.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Command/Command.php     
2010-01-14 11:22:25 UTC (rev 26613)
+++ branches/2.0/src/Symfony/Components/Console/Command/Command.php     
2010-01-14 11:22:29 UTC (rev 26614)
@@ -7,7 +7,6 @@
 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;
 
 /*
@@ -42,13 +41,14 @@
 
   /**
    * Constructor.
+   *
+   * @param string $name The name of the command
    */
   public function __construct($name = null)
   {
     $this->definition = new InputDefinition();
     $this->ignoreValidationErrors = false;
     $this->applicationDefinitionMerged = false;
-    $this->formatter = new Formatter();
     $this->aliases = array();
 
     if (null !== $name)
@@ -389,93 +389,20 @@
   }
 
   /**
-   * Asks a question to the user.
+   * Gets a helper instance by name.
    *
-   * @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 $name The helper name
    *
-   * @param string The user answer
-   */
-  static 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.
+   * @return mixed The helper value
    *
-   * 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
+   * @throws \InvalidArgumentException if the helper is not defined
    */
-  static public function askConfirmation(OutputInterface $output, $question, 
$default = true)
+  protected function getHelper($name)
   {
-    // @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->application->getHelperSet()->get($name);
   }
 
   /**
-   * 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
-   */
-  static public function askAndValidate(OutputInterface $output, $question, 
\Closure $validator, $attempts = false)
-  {
-    // @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
-  }
-
-  /**
    * Returns a text representation of the command.
    *
    * @return string A string representing the command

Added: branches/2.0/src/Symfony/Components/Console/Helper/DialogHelper.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Helper/DialogHelper.php         
                (rev 0)
+++ branches/2.0/src/Symfony/Components/Console/Helper/DialogHelper.php 
2010-01-14 11:22:29 UTC (rev 26614)
@@ -0,0 +1,119 @@
+<?php
+
+namespace Symfony\Components\Console\Helper;
+
+use Symfony\Components\Console\Output\OutputInterface;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * The Dialog class provides helpers to interact with the user.
+ *
+ * @package    symfony
+ * @subpackage console
+ * @author     Fabien Potencier <[email protected]>
+ */
+class DialogHelper extends Helper
+{
+  /**
+   * 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
+  }
+
+  /**
+   * Returns the helper's canonical name
+   */
+  public function getName()
+  {
+    return 'dialog';
+  }
+}

Copied: branches/2.0/src/Symfony/Components/Console/Helper/FormatterHelper.php 
(from rev 26613, 
branches/2.0/src/Symfony/Components/Console/Output/Formatter.php)
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Helper/FormatterHelper.php      
                        (rev 0)
+++ branches/2.0/src/Symfony/Components/Console/Helper/FormatterHelper.php      
2010-01-14 11:22:29 UTC (rev 26614)
@@ -0,0 +1,89 @@
+<?php
+
+namespace Symfony\Components\Console\Helper;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * The Formatter class provides helpers to format messages.
+ *
+ * @package    symfony
+ * @subpackage cli
+ * @author     Fabien Potencier <[email protected]>
+ */
+class FormatterHelper extends Helper
+{
+  /**
+   * 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
+   */
+  public function formatSection($section, $message, $style = 'info')
+  {
+    return sprintf("<%s>[%s]</%s> %s", $style, $section, $style, $message);
+  }
+
+  /**
+   * Formats a message as a block of text.
+   *
+   * @param string|array $messages The message to write in the block
+   * @param string       $style    The style to apply to the whole block
+   * @param Boolean      $large    Whether to return a large block
+   *
+   * @return string The formatter message
+   */
+  public function formatBlock($messages, $style, $large = false)
+  {
+    if (!is_array($messages))
+    {
+      $messages = array($messages);
+    }
+
+    $len = 0;
+    $lines = array();
+    foreach ($messages as $message)
+    {
+      $lines[] = sprintf($large ? '  %s  ' : ' %s ', $message);
+      $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 - $this->strlen($line));
+    }
+    if ($large)
+    {
+      $messages[] = str_repeat(' ', $len);
+    }
+
+    foreach ($messages as &$message)
+    {
+      $message = sprintf('<%s>%s</%s>', $style, $message, $style);
+    }
+
+    return implode("\n", $messages);
+  }
+
+  protected function strlen($string)
+  {
+    return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
+  }
+
+  /**
+   * Returns the helper's canonical name
+   */
+  public function getName()
+  {
+    return 'formatter';
+  }
+}

Added: branches/2.0/src/Symfony/Components/Console/Helper/Helper.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Helper/Helper.php               
                (rev 0)
+++ branches/2.0/src/Symfony/Components/Console/Helper/Helper.php       
2010-01-14 11:22:29 UTC (rev 26614)
@@ -0,0 +1,45 @@
+<?php
+
+namespace Symfony\Components\Console\Helper;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Helper is the base class for all helper classes.
+ *
+ * @package    symfony
+ * @subpackage console
+ * @author     Fabien Potencier <[email protected]>
+ */
+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;
+  }
+}

Added: branches/2.0/src/Symfony/Components/Console/Helper/HelperInterface.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Helper/HelperInterface.php      
                        (rev 0)
+++ branches/2.0/src/Symfony/Components/Console/Helper/HelperInterface.php      
2010-01-14 11:22:29 UTC (rev 26614)
@@ -0,0 +1,43 @@
+<?php
+
+namespace Symfony\Components\Console\Helper;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * HelperInterface is the interface all helpers must implement.
+ *
+ * @package    symfony
+ * @subpackage console
+ * @author     Fabien Potencier <[email protected]>
+ */
+interface HelperInterface
+{
+  /**
+   * 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();
+
+  /**
+   * Returns the canonical name of this helper.
+   *
+   * @return string The canonical name
+   */
+  function getName();
+}

Added: branches/2.0/src/Symfony/Components/Console/Helper/HelperSet.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Helper/HelperSet.php            
                (rev 0)
+++ branches/2.0/src/Symfony/Components/Console/Helper/HelperSet.php    
2010-01-14 11:22:29 UTC (rev 26614)
@@ -0,0 +1,104 @@
+<?php
+
+namespace Symfony\Components\Console\Helper;
+
+use Symfony\Components\Console\Command\Command;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * HelperSet represents a set of helpers to be used with a command.
+ *
+ * @package    symfony
+ * @subpackage console
+ * @author     Fabien Potencier <[email protected]>
+ */
+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;
+  }
+}

Deleted: branches/2.0/src/Symfony/Components/Console/Output/Formatter.php
===================================================================
--- branches/2.0/src/Symfony/Components/Console/Output/Formatter.php    
2010-01-14 11:22:25 UTC (rev 26613)
+++ branches/2.0/src/Symfony/Components/Console/Output/Formatter.php    
2010-01-14 11:22:29 UTC (rev 26614)
@@ -1,81 +0,0 @@
-<?php
-
-namespace Symfony\Components\Console\Output;
-
-/*
- * This file is part of the symfony framework.
- *
- * (c) Fabien Potencier <[email protected]>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
-
-/**
- * The Formatter class provides helpers to format messages.
- *
- * @package    symfony
- * @subpackage cli
- * @author     Fabien Potencier <[email protected]>
- */
-class 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')
-  {
-    return sprintf("<%s>[%s]</%s> %s", $style, $section, $style, $message);
-  }
-
-  /**
-   * Formats a message as a block of text.
-   *
-   * @param string|array $messages The message to write in the block
-   * @param string       $style    The style to apply to the whole block
-   * @param Boolean      $large    Whether to return a large block
-   *
-   * @return string The formatter message
-   */
-  static public function formatBlock($messages, $style, $large = false)
-  {
-    if (!is_array($messages))
-    {
-      $messages = array($messages);
-    }
-
-    $len = 0;
-    $lines = array();
-    foreach ($messages as $message)
-    {
-      $lines[] = sprintf($large ? '  %s  ' : ' %s ', $message);
-      $len = max(static::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));
-    }
-    if ($large)
-    {
-      $messages[] = str_repeat(' ', $len);
-    }
-
-    foreach ($messages as &$message)
-    {
-      $message = sprintf('<%s>%s</%s>', $style, $message, $style);
-    }
-
-    return implode("\n", $messages);
-  }
-
-  static protected function strlen($string)
-  {
-    return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
-  }
-}

Added: 
branches/2.0/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php
===================================================================
--- 
branches/2.0/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php
                           (rev 0)
+++ 
branches/2.0/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php
   2010-01-14 11:22:29 UTC (rev 26614)
@@ -0,0 +1,28 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * 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'), 
'<info>[cli]</info> Some text to display', '::formatSection() formats a message 
in a section');
+
+// ::formatBlock()
+$t->diag('::formatBlock()');
+$t->is($formatter->formatBlock('Some text to display', 'error'), '<error> Some 
text to display </error>', '::formatBlock() formats a message in a block');
+$t->is($formatter->formatBlock(array('Some text to display', 'foo bar'), 
'error'), "<error> Some text to display </error>\n<error> foo bar              
</error>", '::formatBlock() formats a message in a block');
+
+$t->is($formatter->formatBlock('Some text to display', 'error', true), 
"<error>                        </error>\n<error>  Some text to display  
</error>\n<error>                        </error>", '::formatBlock() formats a 
message in a block');

Deleted: 
branches/2.0/tests/unit/Symfony/Components/Console/Output/FormatterTest.php
===================================================================
--- branches/2.0/tests/unit/Symfony/Components/Console/Output/FormatterTest.php 
2010-01-14 11:22:25 UTC (rev 26613)
+++ branches/2.0/tests/unit/Symfony/Components/Console/Output/FormatterTest.php 
2010-01-14 11:22:29 UTC (rev 26614)
@@ -1,26 +0,0 @@
-<?php
-
-/*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <[email protected]>
- *
- * 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'), 
'<info>[cli]</info> Some text to display', '::formatSection() formats a message 
in a section');
-
-// ::formatBlock()
-$t->diag('::formatBlock()');
-$t->is(Formatter::formatBlock('Some text to display', 'error'), '<error> Some 
text to display </error>', '::formatBlock() formats a message in a block');
-$t->is(Formatter::formatBlock(array('Some text to display', 'foo bar'), 
'error'), "<error> Some text to display </error>\n<error> foo bar              
</error>", '::formatBlock() formats a message in a block');
-
-$t->is(Formatter::formatBlock('Some text to display', 'error', true), "<error> 
                       </error>\n<error>  Some text to display  
</error>\n<error>                        </error>", '::formatBlock() formats a 
message in a block');

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.


Reply via email to