HelperSet.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. use Symfony\Component\Console\Exception\InvalidArgumentException;
  13. /**
  14. * HelperSet represents a set of helpers to be used with a command.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class HelperSet implements \IteratorAggregate
  19. {
  20. private $helpers = array();
  21. private $command;
  22. /**
  23. * Constructor.
  24. *
  25. * @param Helper[] $helpers An array of helper.
  26. */
  27. public function __construct(array $helpers = array())
  28. {
  29. foreach ($helpers as $alias => $helper) {
  30. $this->set($helper, is_int($alias) ? null : $alias);
  31. }
  32. }
  33. /**
  34. * Sets a helper.
  35. *
  36. * @param HelperInterface $helper The helper instance
  37. * @param string $alias An alias
  38. */
  39. public function set(HelperInterface $helper, $alias = null)
  40. {
  41. $this->helpers[$helper->getName()] = $helper;
  42. if (null !== $alias) {
  43. $this->helpers[$alias] = $helper;
  44. }
  45. $helper->setHelperSet($this);
  46. }
  47. /**
  48. * Returns true if the helper if defined.
  49. *
  50. * @param string $name The helper name
  51. *
  52. * @return bool true if the helper is defined, false otherwise
  53. */
  54. public function has($name)
  55. {
  56. return isset($this->helpers[$name]);
  57. }
  58. /**
  59. * Gets a helper value.
  60. *
  61. * @param string $name The helper name
  62. *
  63. * @return HelperInterface The helper instance
  64. *
  65. * @throws InvalidArgumentException if the helper is not defined
  66. */
  67. public function get($name)
  68. {
  69. if (!$this->has($name)) {
  70. throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  71. }
  72. if ('dialog' === $name && $this->helpers[$name] instanceof DialogHelper) {
  73. @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);
  74. } elseif ('progress' === $name && $this->helpers[$name] instanceof ProgressHelper) {
  75. @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);
  76. } elseif ('table' === $name && $this->helpers[$name] instanceof TableHelper) {
  77. @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);
  78. }
  79. return $this->helpers[$name];
  80. }
  81. /**
  82. * Sets the command associated with this helper set.
  83. *
  84. * @param Command $command A Command instance
  85. */
  86. public function setCommand(Command $command = null)
  87. {
  88. $this->command = $command;
  89. }
  90. /**
  91. * Gets the command associated with this helper set.
  92. *
  93. * @return Command A Command instance
  94. */
  95. public function getCommand()
  96. {
  97. return $this->command;
  98. }
  99. public function getIterator()
  100. {
  101. return new \ArrayIterator($this->helpers);
  102. }
  103. }