Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Helper; Chris@0: Chris@0: use Symfony\Component\Console\Command\Command; Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * HelperSet represents a set of helpers to be used with a command. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class HelperSet implements \IteratorAggregate Chris@0: { Chris@0: /** Chris@0: * @var Helper[] Chris@0: */ Chris@17: private $helpers = []; Chris@0: private $command; Chris@0: Chris@0: /** Chris@0: * @param Helper[] $helpers An array of helper Chris@0: */ Chris@17: public function __construct(array $helpers = []) Chris@0: { Chris@0: foreach ($helpers as $alias => $helper) { Chris@17: $this->set($helper, \is_int($alias) ? null : $alias); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a helper. Chris@0: * Chris@0: * @param HelperInterface $helper The helper instance Chris@0: * @param string $alias An alias Chris@0: */ Chris@0: public function set(HelperInterface $helper, $alias = null) Chris@0: { Chris@0: $this->helpers[$helper->getName()] = $helper; Chris@0: if (null !== $alias) { Chris@0: $this->helpers[$alias] = $helper; Chris@0: } Chris@0: Chris@0: $helper->setHelperSet($this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the helper if defined. Chris@0: * Chris@0: * @param string $name The helper name Chris@0: * Chris@0: * @return bool true if the helper is defined, false otherwise Chris@0: */ Chris@0: public function has($name) Chris@0: { Chris@0: return isset($this->helpers[$name]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a helper value. Chris@0: * Chris@0: * @param string $name The helper name Chris@0: * Chris@0: * @return HelperInterface The helper instance Chris@0: * Chris@0: * @throws InvalidArgumentException if the helper is not defined Chris@0: */ Chris@0: public function get($name) Chris@0: { Chris@0: if (!$this->has($name)) { Chris@0: throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); Chris@0: } Chris@0: Chris@0: return $this->helpers[$name]; Chris@0: } Chris@0: Chris@0: public function setCommand(Command $command = null) Chris@0: { Chris@0: $this->command = $command; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the command associated with this helper set. Chris@0: * Chris@0: * @return Command A Command instance Chris@0: */ Chris@0: public function getCommand() Chris@0: { Chris@0: return $this->command; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return Helper[] Chris@0: */ Chris@0: public function getIterator() Chris@0: { Chris@0: return new \ArrayIterator($this->helpers); Chris@0: } Chris@0: }