[Console] refactored helpers
This commit is contained in:
parent
e7dfdab05f
commit
7a0586efb7
@ -14,6 +14,9 @@ use Symfony\Components\Console\Output\ConsoleOutput;
|
|||||||
use Symfony\Components\Console\Command\Command;
|
use Symfony\Components\Console\Command\Command;
|
||||||
use Symfony\Components\Console\Command\HelpCommand;
|
use Symfony\Components\Console\Command\HelpCommand;
|
||||||
use Symfony\Components\Console\Command\ListCommand;
|
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.
|
* This file is part of the symfony framework.
|
||||||
@ -53,6 +56,7 @@ class Application
|
|||||||
protected $catchExceptions;
|
protected $catchExceptions;
|
||||||
protected $autoExit;
|
protected $autoExit;
|
||||||
protected $definition;
|
protected $definition;
|
||||||
|
protected $helperSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -68,6 +72,10 @@ class Application
|
|||||||
$this->autoExit = true;
|
$this->autoExit = true;
|
||||||
$this->commands = array();
|
$this->commands = array();
|
||||||
$this->aliases = array();
|
$this->aliases = array();
|
||||||
|
$this->helperSet = new HelperSet(array(
|
||||||
|
new FormatterHelper(),
|
||||||
|
new DialogHelper(),
|
||||||
|
));
|
||||||
|
|
||||||
$this->addCommand(new HelpCommand());
|
$this->addCommand(new HelpCommand());
|
||||||
$this->addCommand(new ListCommand());
|
$this->addCommand(new ListCommand());
|
||||||
@ -200,6 +208,26 @@ class Application
|
|||||||
return is_numeric($statusCode) ? $statusCode : 0;
|
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.
|
* Gets the InputDefinition related to this Application.
|
||||||
*
|
*
|
||||||
|
@ -8,9 +8,6 @@ use Symfony\Components\Console\Input\InputArgument;
|
|||||||
use Symfony\Components\Console\Input\InputInterface;
|
use Symfony\Components\Console\Input\InputInterface;
|
||||||
use Symfony\Components\Console\Output\OutputInterface;
|
use Symfony\Components\Console\Output\OutputInterface;
|
||||||
use Symfony\Components\Console\Application;
|
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.
|
* This file is part of the symfony framework.
|
||||||
@ -45,26 +42,15 @@ class Command
|
|||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name The name of the command
|
||||||
* @param HelperSet $helperSet A helper set instance
|
|
||||||
*/
|
*/
|
||||||
public function __construct($name = null, HelperSet $helperSet = null)
|
public function __construct($name = null)
|
||||||
{
|
{
|
||||||
$this->definition = new InputDefinition();
|
$this->definition = new InputDefinition();
|
||||||
$this->ignoreValidationErrors = false;
|
$this->ignoreValidationErrors = false;
|
||||||
$this->applicationDefinitionMerged = false;
|
$this->applicationDefinitionMerged = false;
|
||||||
$this->aliases = array();
|
$this->aliases = array();
|
||||||
|
|
||||||
if (null === $helperSet)
|
|
||||||
{
|
|
||||||
$helperSet = new HelperSet(array(
|
|
||||||
new FormatterHelper(),
|
|
||||||
new InteractHelper(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setHelperSet($helperSet);
|
|
||||||
|
|
||||||
if (null !== $name)
|
if (null !== $name)
|
||||||
{
|
{
|
||||||
$this->setName($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
|
* @param string $name The helper name
|
||||||
*/
|
|
||||||
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
|
|
||||||
*
|
*
|
||||||
* @return mixed The helper value
|
* @return mixed The helper value
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException if the helper is not defined
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
* @package symfony
|
||||||
* @subpackage cli
|
* @subpackage console
|
||||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*/
|
*/
|
||||||
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.
|
* Asks a question to the user.
|
||||||
*
|
*
|
||||||
@ -116,4 +108,12 @@ class InteractHelper extends Helper
|
|||||||
throw $error;
|
throw $error;
|
||||||
// @codeCoverageIgnoreEnd
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the helper's canonical name
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'dialog';
|
||||||
|
}
|
||||||
}
|
}
|
@ -20,14 +20,6 @@ namespace Symfony\Components\Console\Helper;
|
|||||||
*/
|
*/
|
||||||
class FormatterHelper extends Helper
|
class FormatterHelper extends Helper
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Returns the helper's canonical name
|
|
||||||
*/
|
|
||||||
public function getName()
|
|
||||||
{
|
|
||||||
return 'formatter';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats a message within a section.
|
* Formats a message within a section.
|
||||||
*
|
*
|
||||||
@ -86,5 +78,12 @@ class FormatterHelper extends Helper
|
|||||||
{
|
{
|
||||||
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
|
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the helper's canonical name
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'formatter';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -3,19 +3,19 @@
|
|||||||
namespace Symfony\Components\Console\Helper;
|
namespace Symfony\Components\Console\Helper;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the symfony package.
|
* This file is part of the symfony framework.
|
||||||
*
|
*
|
||||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* This source file is subject to the MIT license that is bundled
|
||||||
* file that was distributed with this source code.
|
* with this source code in the file LICENSE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper is the base class for all helper classes.
|
* Helper is the base class for all helper classes.
|
||||||
*
|
*
|
||||||
* @package symfony
|
* @package symfony
|
||||||
* @subpackage templating
|
* @subpackage console
|
||||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*/
|
*/
|
||||||
abstract class Helper implements HelperInterface
|
abstract class Helper implements HelperInterface
|
||||||
|
@ -3,30 +3,23 @@
|
|||||||
namespace Symfony\Components\Console\Helper;
|
namespace Symfony\Components\Console\Helper;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the symfony package.
|
* This file is part of the symfony framework.
|
||||||
*
|
*
|
||||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* This source file is subject to the MIT license that is bundled
|
||||||
* file that was distributed with this source code.
|
* with this source code in the file LICENSE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HelperInterface is the interface all helpers must implement.
|
* HelperInterface is the interface all helpers must implement.
|
||||||
*
|
*
|
||||||
* @package symfony
|
* @package symfony
|
||||||
* @subpackage templating
|
* @subpackage console
|
||||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*/
|
*/
|
||||||
interface HelperInterface
|
interface HelperInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Returns the canonical name of this helper.
|
|
||||||
*
|
|
||||||
* @return string The canonical name
|
|
||||||
*/
|
|
||||||
function getName();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the helper set associated with this helper.
|
* Sets the helper set associated with this helper.
|
||||||
*
|
*
|
||||||
@ -40,4 +33,11 @@ interface HelperInterface
|
|||||||
* @return HelperSet A HelperSet instance
|
* @return HelperSet A HelperSet instance
|
||||||
*/
|
*/
|
||||||
function getHelperSet();
|
function getHelperSet();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the canonical name of this helper.
|
||||||
|
*
|
||||||
|
* @return string The canonical name
|
||||||
|
*/
|
||||||
|
function getName();
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,19 @@ namespace Symfony\Components\Console\Helper;
|
|||||||
use Symfony\Components\Console\Command\Command;
|
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 <fabien.potencier@symfony-project.com>
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* This source file is subject to the MIT license that is bundled
|
||||||
* file that was distributed with this source code.
|
* with this source code in the file LICENSE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HelperSet represents a set of helpers to be used with a command.
|
* HelperSet represents a set of helpers to be used with a command.
|
||||||
*
|
*
|
||||||
* @package symfony
|
* @package symfony
|
||||||
* @subpackage templating
|
* @subpackage console
|
||||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
*/
|
*/
|
||||||
class HelperSet
|
class HelperSet
|
||||||
@ -102,4 +102,3 @@ class HelperSet
|
|||||||
return $this->command;
|
return $this->command;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user