SlotsHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Templating\Helper;
  11. /**
  12. * SlotsHelper manages template slots.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class SlotsHelper extends Helper
  17. {
  18. protected $slots = array();
  19. protected $openSlots = array();
  20. /**
  21. * Starts a new slot.
  22. *
  23. * This method starts an output buffer that will be
  24. * closed when the stop() method is called.
  25. *
  26. * @param string $name The slot name
  27. *
  28. * @throws \InvalidArgumentException if a slot with the same name is already started
  29. */
  30. public function start($name)
  31. {
  32. if (in_array($name, $this->openSlots)) {
  33. throw new \InvalidArgumentException(sprintf('A slot named "%s" is already started.', $name));
  34. }
  35. $this->openSlots[] = $name;
  36. $this->slots[$name] = '';
  37. ob_start();
  38. ob_implicit_flush(0);
  39. }
  40. /**
  41. * Stops a slot.
  42. *
  43. * @throws \LogicException if no slot has been started
  44. */
  45. public function stop()
  46. {
  47. if (!$this->openSlots) {
  48. throw new \LogicException('No slot started.');
  49. }
  50. $name = array_pop($this->openSlots);
  51. $this->slots[$name] = ob_get_clean();
  52. }
  53. /**
  54. * Returns true if the slot exists.
  55. *
  56. * @param string $name The slot name
  57. *
  58. * @return bool
  59. */
  60. public function has($name)
  61. {
  62. return isset($this->slots[$name]);
  63. }
  64. /**
  65. * Gets the slot value.
  66. *
  67. * @param string $name The slot name
  68. * @param bool|string $default The default slot content
  69. *
  70. * @return string The slot content
  71. */
  72. public function get($name, $default = false)
  73. {
  74. return isset($this->slots[$name]) ? $this->slots[$name] : $default;
  75. }
  76. /**
  77. * Sets a slot value.
  78. *
  79. * @param string $name The slot name
  80. * @param string $content The slot content
  81. */
  82. public function set($name, $content)
  83. {
  84. $this->slots[$name] = $content;
  85. }
  86. /**
  87. * Outputs a slot.
  88. *
  89. * @param string $name The slot name
  90. * @param bool|string $default The default slot content
  91. *
  92. * @return bool true if the slot is defined or if a default content has been provided, false otherwise
  93. */
  94. public function output($name, $default = false)
  95. {
  96. if (!isset($this->slots[$name])) {
  97. if (false !== $default) {
  98. echo $default;
  99. return true;
  100. }
  101. return false;
  102. }
  103. echo $this->slots[$name];
  104. return true;
  105. }
  106. /**
  107. * Returns the canonical name of this helper.
  108. *
  109. * @return string The canonical name
  110. */
  111. public function getName()
  112. {
  113. return 'slots';
  114. }
  115. }