ViewBlock.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\View;
  17. use Cake\Core\Exception\Exception;
  18. /**
  19. * ViewBlock implements the concept of Blocks or Slots in the View layer.
  20. * Slots or blocks are combined with extending views and layouts to afford slots
  21. * of content that are present in a layout or parent view, but are defined by the child
  22. * view or elements used in the view.
  23. */
  24. class ViewBlock
  25. {
  26. /**
  27. * Override content
  28. *
  29. * @var string
  30. */
  31. public const OVERRIDE = 'override';
  32. /**
  33. * Append content
  34. *
  35. * @var string
  36. */
  37. public const APPEND = 'append';
  38. /**
  39. * Prepend content
  40. *
  41. * @var string
  42. */
  43. public const PREPEND = 'prepend';
  44. /**
  45. * Block content. An array of blocks indexed by name.
  46. *
  47. * @var array
  48. */
  49. protected $_blocks = [];
  50. /**
  51. * The active blocks being captured.
  52. *
  53. * @var string[]
  54. */
  55. protected $_active = [];
  56. /**
  57. * Should the currently captured content be discarded on ViewBlock::end()
  58. *
  59. * @see \Cake\View\ViewBlock::end()
  60. * @var bool
  61. */
  62. protected $_discardActiveBufferOnEnd = false;
  63. /**
  64. * Start capturing output for a 'block'
  65. *
  66. * Blocks allow you to create slots or blocks of dynamic content in the layout.
  67. * view files can implement some or all of a layout's slots.
  68. *
  69. * You can end capturing blocks using View::end(). Blocks can be output
  70. * using View::get();
  71. *
  72. * @param string $name The name of the block to capture for.
  73. * @param string $mode If ViewBlock::OVERRIDE existing content will be overridden by new content.
  74. * If ViewBlock::APPEND content will be appended to existing content.
  75. * If ViewBlock::PREPEND it will be prepended.
  76. * @throws \Cake\Core\Exception\Exception When starting a block twice
  77. * @return void
  78. */
  79. public function start(string $name, string $mode = ViewBlock::OVERRIDE): void
  80. {
  81. if (array_key_exists($name, $this->_active)) {
  82. throw new Exception(sprintf("A view block with the name '%s' is already/still open.", $name));
  83. }
  84. $this->_active[$name] = $mode;
  85. ob_start();
  86. }
  87. /**
  88. * End a capturing block. The compliment to ViewBlock::start()
  89. *
  90. * @return void
  91. * @see \Cake\View\ViewBlock::start()
  92. */
  93. public function end(): void
  94. {
  95. if ($this->_discardActiveBufferOnEnd) {
  96. $this->_discardActiveBufferOnEnd = false;
  97. ob_end_clean();
  98. return;
  99. }
  100. if ($this->_active) {
  101. $mode = end($this->_active);
  102. $active = key($this->_active);
  103. $content = ob_get_clean();
  104. if ($mode === ViewBlock::OVERRIDE) {
  105. $this->_blocks[$active] = $content;
  106. } else {
  107. $this->concat($active, $content, $mode);
  108. }
  109. array_pop($this->_active);
  110. }
  111. }
  112. /**
  113. * Concat content to an existing or new block.
  114. * Concating to a new block will create the block.
  115. *
  116. * Calling concat() without a value will create a new capturing
  117. * block that needs to be finished with View::end(). The content
  118. * of the new capturing context will be added to the existing block context.
  119. *
  120. * @param string $name Name of the block
  121. * @param mixed $value The content for the block. Value will be type cast
  122. * to string.
  123. * @param string $mode If ViewBlock::APPEND content will be appended to existing content.
  124. * If ViewBlock::PREPEND it will be prepended.
  125. * @return void
  126. */
  127. public function concat(string $name, $value = null, $mode = ViewBlock::APPEND): void
  128. {
  129. if ($value === null) {
  130. $this->start($name, $mode);
  131. return;
  132. }
  133. if (!isset($this->_blocks[$name])) {
  134. $this->_blocks[$name] = '';
  135. }
  136. if ($mode === ViewBlock::PREPEND) {
  137. $this->_blocks[$name] = $value . $this->_blocks[$name];
  138. } else {
  139. $this->_blocks[$name] .= $value;
  140. }
  141. }
  142. /**
  143. * Set the content for a block. This will overwrite any
  144. * existing content.
  145. *
  146. * @param string $name Name of the block
  147. * @param mixed $value The content for the block. Value will be type cast
  148. * to string.
  149. * @return void
  150. */
  151. public function set(string $name, $value): void
  152. {
  153. $this->_blocks[$name] = (string)$value;
  154. }
  155. /**
  156. * Get the content for a block.
  157. *
  158. * @param string $name Name of the block
  159. * @param string $default Default string
  160. * @return string The block content or $default if the block does not exist.
  161. */
  162. public function get(string $name, string $default = ''): string
  163. {
  164. if (!isset($this->_blocks[$name])) {
  165. return $default;
  166. }
  167. return $this->_blocks[$name];
  168. }
  169. /**
  170. * Check if a block exists
  171. *
  172. * @param string $name Name of the block
  173. * @return bool
  174. */
  175. public function exists(string $name): bool
  176. {
  177. return isset($this->_blocks[$name]);
  178. }
  179. /**
  180. * Get the names of all the existing blocks.
  181. *
  182. * @return string[] An array containing the blocks.
  183. */
  184. public function keys(): array
  185. {
  186. return array_keys($this->_blocks);
  187. }
  188. /**
  189. * Get the name of the currently open block.
  190. *
  191. * @return string|null Either null or the name of the last open block.
  192. */
  193. public function active(): ?string
  194. {
  195. end($this->_active);
  196. return key($this->_active);
  197. }
  198. /**
  199. * Get the unclosed/active blocks. Key is name, value is mode.
  200. *
  201. * @return string[] An array of unclosed blocks.
  202. */
  203. public function unclosed(): array
  204. {
  205. return $this->_active;
  206. }
  207. }