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 return $this->helpers[$name];
89 * Sets the command associated with this helper set.
91 * @param Command $command A Command instance
93 public function setCommand(Command $command = null)
95 $this->command = $command;
99 * Gets the command associated with this helper set.
101 * @return Command A Command instance
103 public function getCommand()
105 return $this->command;
111 public function getIterator()
113 return new \ArrayIterator($this->helpers);