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();
31 * @param Helper[] $helpers An array of helper
33 public function __construct(array $helpers = array())
35 foreach ($helpers as $alias => $helper) {
36 $this->set($helper, \is_int($alias) ? null : $alias);
43 * @param HelperInterface $helper The helper instance
44 * @param string $alias An alias
46 public function set(HelperInterface $helper, $alias = null)
48 $this->helpers[$helper->getName()] = $helper;
49 if (null !== $alias) {
50 $this->helpers[$alias] = $helper;
53 $helper->setHelperSet($this);
57 * Returns true if the helper if defined.
59 * @param string $name The helper name
61 * @return bool true if the helper is defined, false otherwise
63 public function has($name)
65 return isset($this->helpers[$name]);
69 * Gets a helper value.
71 * @param string $name The helper name
73 * @return HelperInterface The helper instance
75 * @throws InvalidArgumentException if the helper is not defined
77 public function get($name)
79 if (!$this->has($name)) {
80 throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
83 return $this->helpers[$name];
86 public function setCommand(Command $command = null)
88 $this->command = $command;
92 * Gets the command associated with this helper set.
94 * @return Command A Command instance
96 public function getCommand()
98 return $this->command;
104 public function getIterator()
106 return new \ArrayIterator($this->helpers);