HelperSet.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. if ('dialog' === $name && $this->helpers[$name] instanceof DialogHelper) {
  72. @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);
  73. } elseif ('progress' === $name && $this->helpers[$name] instanceof ProgressHelper) {
  74. @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);
  75. } elseif ('table' === $name && $this->helpers[$name] instanceof TableHelper) {
  76. @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);
  77. }
  78. return $this->helpers[$name];
  79. }
  80. /**
  81. * Sets the command associated with this helper set.
  82. *
  83. * @param Command $command A Command instance
  84. */
  85. public function setCommand(Command $command = null)
  86. {
  87. $this->command = $command;
  88. }
  89. /**
  90. * Gets the command associated with this helper set.
  91. *
  92. * @return Command A Command instance
  93. */
  94. public function getCommand()
  95. {
  96. return $this->command;
  97. }
  98. public function getIterator()
  99. {
  100. return new \ArrayIterator($this->helpers);
  101. }
  102. }