HelperSet.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Symfony\Components\Templating\Helper;
  3. use Symfony\Components\Templating\Engine;
  4. /*
  5. * This file is part of the symfony package.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. /**
  13. * HelperSet represents a set of helpers to be used with a templating engine.
  14. *
  15. * @package symfony
  16. * @subpackage templating
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class HelperSet
  20. {
  21. protected $helpers;
  22. protected $engine;
  23. public function __construct(array $helpers = array())
  24. {
  25. $this->helpers = array();
  26. foreach ($helpers as $alias => $helper)
  27. {
  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. {
  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 Boolean 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. {
  70. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  71. }
  72. return $this->helpers[$name];
  73. }
  74. /**
  75. * Sets the template engine associated with this helper set.
  76. *
  77. * @param Engine $engine A Engine instance
  78. */
  79. public function setEngine(Engine $engine = null)
  80. {
  81. $this->engine = $engine;
  82. }
  83. /**
  84. * Gets the template engine associated with this helper set.
  85. *
  86. * @return Engine A Engine instance
  87. */
  88. public function getEngine()
  89. {
  90. return $this->engine;
  91. }
  92. }