4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Exception\InvalidArgumentException;
18 * HelperSet represents a set of helpers to be used with a command.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class HelperSet implements \IteratorAggregate
27 private $helpers = array();
33 * @param Helper[] $helpers An array of helper
35 public function __construct(array $helpers = array())
37 foreach ($helpers as $alias => $helper) {
38 $this->set($helper, is_int($alias) ? null : $alias);
45 * @param HelperInterface $helper The helper instance
46 * @param string $alias An alias
48 public function set(HelperInterface $helper, $alias = null)
50 $this->helpers[$helper->getName()] = $helper;
51 if (null !== $alias) {
52 $this->helpers[$alias] = $helper;
55 $helper->setHelperSet($this);
59 * Returns true if the helper if defined.
61 * @param string $name The helper name
63 * @return bool true if the helper is defined, false otherwise
65 public function has($name)
67 return isset($this->helpers[$name]);
71 * Gets a helper value.
73 * @param string $name The helper name
75 * @return HelperInterface The helper instance
77 * @throws InvalidArgumentException if the helper is not defined
79 public function get($name)
81 if (!$this->has($name)) {
82 throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
85 if ('dialog' === $name && $this->helpers[$name] instanceof DialogHelper) {
86 @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);
87 } elseif ('progress' === $name && $this->helpers[$name] instanceof ProgressHelper) {
88 @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);
89 } elseif ('table' === $name && $this->helpers[$name] instanceof TableHelper) {
90 @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);
93 return $this->helpers[$name];
97 * Sets the command associated with this helper set.
99 * @param Command $command A Command instance
101 public function setCommand(Command $command = null)
103 $this->command = $command;
107 * Gets the command associated with this helper set.
109 * @return Command A Command instance
111 public function getCommand()
113 return $this->command;
119 public function getIterator()
121 return new \ArrayIterator($this->helpers);