2010-01-11 11:24:08 +00:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2010-01-11 11:24:08 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-01-11 11:24:08 +00:00
*
2011-01-15 13:29:43 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
2010-01-11 11:24:08 +00:00
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\Console\Helper ;
use Symfony\Component\Console\Command\Command ;
2010-01-11 11:24:08 +00:00
/**
* HelperSet represents a set of helpers to be used with a command .
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2010-01-11 11:24:08 +00:00
*/
2013-07-09 10:28:35 +01:00
class HelperSet implements \IteratorAggregate
2010-01-11 11:24:08 +00:00
{
2013-11-10 17:06:47 +00:00
private $helpers = array ();
2011-03-11 11:53:42 +00:00
private $command ;
2010-01-11 11:24:08 +00:00
2010-05-06 12:25:53 +01:00
/**
2011-12-28 09:33:49 +00:00
* Constructor .
*
2010-05-06 12:25:53 +01:00
* @ param Helper [] $helpers An array of helper .
*/
public function __construct ( array $helpers = array ())
2010-01-11 11:24:08 +00:00
{
2010-05-07 15:09:11 +01:00
foreach ( $helpers as $alias => $helper ) {
2010-05-06 12:25:53 +01:00
$this -> set ( $helper , is_int ( $alias ) ? null : $alias );
}
2010-01-11 11:24:08 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Sets a helper .
*
2011-05-30 00:25:35 +01:00
* @ param HelperInterface $helper The helper instance
* @ param string $alias An alias
2010-05-06 12:25:53 +01:00
*/
public function set ( HelperInterface $helper , $alias = null )
2010-01-11 11:24:08 +00:00
{
2010-05-06 12:25:53 +01:00
$this -> helpers [ $helper -> getName ()] = $helper ;
2010-05-07 15:09:11 +01:00
if ( null !== $alias ) {
2010-05-06 12:25:53 +01:00
$this -> helpers [ $alias ] = $helper ;
}
2010-01-11 11:24:08 +00:00
2010-05-06 12:25:53 +01:00
$helper -> setHelperSet ( $this );
}
2010-01-11 11:24:08 +00:00
2010-05-06 12:25:53 +01:00
/**
* Returns true if the helper if defined .
*
2012-05-15 21:19:31 +01:00
* @ param string $name The helper name
2010-05-06 12:25:53 +01:00
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the helper is defined , false otherwise
2010-05-06 12:25:53 +01:00
*/
public function has ( $name )
2010-01-11 11:24:08 +00:00
{
2010-05-06 12:25:53 +01:00
return isset ( $this -> helpers [ $name ]);
2010-01-11 11:24:08 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* 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 )
{
2010-05-07 15:09:11 +01:00
if ( ! $this -> has ( $name )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( sprintf ( 'The helper "%s" is not defined.' , $name ));
}
2014-12-01 19:40:50 +00:00
if ( 'dialog' === $name && $this -> helpers [ $name ] instanceof DialogHelper ) {
trigger_error ( '"Symfony\Component\Console\Helper\DialogHelper" is deprecated since version 2.5 and will be removed in 3.0. Use "Symfony\Component\Console\Helper\QuestionHelper" instead.' , E_USER_DEPRECATED );
} elseif ( 'progress' === $name && $this -> helpers [ $name ] instanceof ProgressHelper ) {
trigger_error ( '"Symfony\Component\Console\Helper\ProgressHelper" is deprecated since version 2.5 and will be removed in 3.0. Use "Symfony\Component\Console\Helper\ProgressBar" instead.' , E_USER_DEPRECATED );
} elseif ( 'table' === $name && $this -> helpers [ $name ] instanceof TableHelper ) {
trigger_error ( '"Symfony\Component\Console\Helper\TableHelper" is deprecated since version 2.5 and will be removed in 3.0. Use "Symfony\Component\Console\Helper\Table" instead.' , E_USER_DEPRECATED );
}
2010-05-06 12:25:53 +01:00
return $this -> helpers [ $name ];
}
2010-01-11 11:24:08 +00:00
2010-05-06 12:25:53 +01:00
/**
* Sets the command associated with this helper set .
*
* @ param Command $command A Command instance
*/
public function setCommand ( Command $command = null )
{
$this -> command = $command ;
}
2010-01-11 11:24:08 +00:00
2010-05-06 12:25:53 +01:00
/**
* Gets the command associated with this helper set .
*
* @ return Command A Command instance
*/
public function getCommand ()
{
return $this -> command ;
}
2013-07-09 10:28:35 +01:00
public function getIterator ()
{
return new \ArrayIterator ( $this -> helpers );
}
2010-01-11 11:24:08 +00:00
}