HelperSet.php 2.4 KB

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