Drupal investigation

HelperSet.php 3.5KB

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