No Description

HelperSet.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Command\Command;
  12. /**
  13. * HelperSet represents a set of helpers to be used with a command.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class HelperSet implements \IteratorAggregate
  18. {
  19. private $helpers = array();
  20. private $command;
  21. /**
  22. * Constructor.
  23. *
  24. * @param Helper[] $helpers An array of helper.
  25. */
  26. public function __construct(array $helpers = array())
  27. {
  28. foreach ($helpers as $alias => $helper) {
  29. $this->set($helper, is_int($alias) ? null : $alias);
  30. }
  31. }
  32. /**
  33. * Sets a helper.
  34. *
  35. * @param HelperInterface $helper The helper instance
  36. * @param string $alias An alias
  37. */
  38. public function set(HelperInterface $helper, $alias = null)
  39. {
  40. $this->helpers[$helper->getName()] = $helper;
  41. if (null !== $alias) {
  42. $this->helpers[$alias] = $helper;
  43. }
  44. $helper->setHelperSet($this);
  45. }
  46. /**
  47. * Returns true if the helper if defined.
  48. *
  49. * @param string $name The helper name
  50. *
  51. * @return bool true if the helper is defined, false otherwise
  52. */
  53. public function has($name)
  54. {
  55. return isset($this->helpers[$name]);
  56. }
  57. /**
  58. * Gets a helper value.
  59. *
  60. * @param string $name The helper name
  61. *
  62. * @return HelperInterface The helper instance
  63. *
  64. * @throws \InvalidArgumentException if the helper is not defined
  65. */
  66. public function get($name)
  67. {
  68. if (!$this->has($name)) {
  69. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  70. }
  71. return $this->helpers[$name];
  72. }
  73. /**
  74. * Sets the command associated with this helper set.
  75. *
  76. * @param Command $command A Command instance
  77. */
  78. public function setCommand(Command $command = null)
  79. {
  80. $this->command = $command;
  81. }
  82. /**
  83. * Gets the command associated with this helper set.
  84. *
  85. * @return Command A Command instance
  86. */
  87. public function getCommand()
  88. {
  89. return $this->command;
  90. }
  91. public function getIterator()
  92. {
  93. return new \ArrayIterator($this->helpers);
  94. }
  95. }