PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/recess/recess/framework/helpers/Buffer.class.php

http://github.com/recess/recess
PHP | 134 lines | 56 code | 10 blank | 68 comment | 3 complexity | 1f557ddfcf80d691e2d75a0e689623f6 MD5 | raw file
Possible License(s): MIT, GPL-2.0
  1. <?php
  2. Library::import('recess.framework.helpers.blocks.Block');
  3. Library::import('recess.framework.helpers.blocks.HtmlBlock');
  4. Library::import('recess.framework.helpers.blocks.ListBlock');
  5. Library::import('recess.framework.AbstractHelper');
  6. /**
  7. * Buffer is a helper class that acts as a factory for
  8. * HtmlBlocks. Buffer and blocks are often used in conjunction
  9. * with layouts as an easy mechanism for transferring chunks of
  10. * HTML from a child template to a parent Layout.
  11. *
  12. * Buffer can be used to fill unempty HtmlBlocks,
  13. * overwrite HtmlBlocks, or append/prepend to them. Here are some
  14. * example usages:
  15. *
  16. * Buffer::to($block);
  17. * echo 'hello world';
  18. * Buffer::end();
  19. * // $block is now an HtmlBlock with contents 'hello world'
  20. *
  21. * Buffer::append($block);
  22. * echo '!<br />';
  23. * Buffer::end();
  24. * // $block is now an HtmlBlock with contents 'hello world!<br />'
  25. *
  26. * Buffer::to($block);
  27. * print_r($block);
  28. * Block::end();
  29. * // $block is still 'hello world!<br />'
  30. *
  31. * Buffer::to($block, Buffer::OVERWRITE);
  32. * echo 'overwritten';
  33. * Buffer::end();
  34. * // $block is now 'overwritten'
  35. *
  36. * echo $block;
  37. * // overwritten
  38. *
  39. * @author Kris Jordan
  40. */
  41. abstract class Buffer extends AbstractHelper {
  42. const NORMAL = 0;
  43. const OVERWRITE = 1;
  44. const APPEND = 2;
  45. const PREPEND = 3;
  46. /** STATIC MEMBERS **/
  47. private static $bufferBlocks = array();
  48. private static $bufferModes = array();
  49. /**
  50. * Begin output buffering to the block passed by reference. If the
  51. * reference is set to Null a new HtmlBlock will be assigned to the
  52. * reference.
  53. *
  54. * @param HtmlBlock or Null - The block the buffer will fill.
  55. * @param int Optional - mode used to fill block.
  56. */
  57. public static function to(&$block, $mode = self::NORMAL) {
  58. self::modalStart($block, $mode);
  59. }
  60. /**
  61. * Buffer will append to the provided HtmlBlock. If null this will
  62. * create a new block, not fail.
  63. *
  64. * @param HtmlBlock The block to append to.
  65. */
  66. public static function appendTo(&$block) {
  67. self::modalStart($block, self::APPEND);
  68. }
  69. /**
  70. * Buffer will append to the provided HtmlBlock. If null this will
  71. * create a new block, not fail.
  72. *
  73. * @param HtmlBlock The block to append to.
  74. */
  75. public static function prependTo(&$block) {
  76. self::modalStart($block, self::PREPEND);
  77. }
  78. /**
  79. * Internal helper method for starting a new buffer.
  80. *
  81. * @param HtmlBlock The block to append to.
  82. */
  83. private static function modalStart(&$block, $mode) {
  84. if($block === null) {
  85. $block = new HtmlBlock();
  86. }
  87. array_push(self::$bufferBlocks, $block);
  88. array_push(self::$bufferModes, $mode);
  89. ob_start();
  90. }
  91. /**
  92. * End the output buffer, clear contents, and assign contents
  93. * to the block passed by reference to start the buffer. Also
  94. * returns the block.
  95. *
  96. * @return The final block.
  97. */
  98. public static function end() {
  99. if(empty(self::$bufferBlocks)) {
  100. throw new RecessFrameworkException('Buffer ended without corresponding Buffer::to($block).', 2);
  101. }
  102. $buffer = ob_get_clean();
  103. $mode = array_pop(self::$bufferModes);
  104. $block = array_pop(self::$bufferBlocks);
  105. switch($mode) {
  106. case self::NORMAL:
  107. if((string)$block === '') {
  108. $block->set($buffer);
  109. }
  110. break;
  111. case self::OVERWRITE:
  112. $block->set($buffer);
  113. break;
  114. case self::APPEND:
  115. $block->append($buffer);
  116. break;
  117. case self::PREPEND:
  118. $block->prepend($buffer);
  119. break;
  120. }
  121. return $block;
  122. }
  123. }
  124. ?>