smarty_internal_runtime_capture.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Runtime Extension Capture
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. */
  9. class Smarty_Internal_Runtime_Capture
  10. {
  11. /**
  12. * Flag that this instance will not be cached
  13. *
  14. * @var bool
  15. */
  16. public $isPrivateExtension = true;
  17. /**
  18. * Stack of capture parameter
  19. *
  20. * @var array
  21. */
  22. private $captureStack = array();
  23. /**
  24. * Current open capture sections
  25. *
  26. * @var int
  27. */
  28. private $captureCount = 0;
  29. /**
  30. * Count stack
  31. *
  32. * @var int[]
  33. */
  34. private $countStack = array();
  35. /**
  36. * Named buffer
  37. *
  38. * @var string[]
  39. */
  40. private $namedBuffer = array();
  41. /**
  42. * Flag if callbacks are registered
  43. *
  44. * @var bool
  45. */
  46. private $isRegistered = false;
  47. /**
  48. * Open capture section
  49. *
  50. * @param \Smarty_Internal_Template $_template
  51. * @param string $buffer capture name
  52. * @param string $assign variable name
  53. * @param string $append variable name
  54. */
  55. public function open(Smarty_Internal_Template $_template, $buffer, $assign, $append)
  56. {
  57. if (!$this->isRegistered) {
  58. $this->register($_template);
  59. }
  60. $this->captureStack[] = array($buffer, $assign, $append);
  61. $this->captureCount ++;
  62. ob_start();
  63. }
  64. /**
  65. * Register callbacks in template class
  66. *
  67. * @param \Smarty_Internal_Template $_template
  68. */
  69. private function register(Smarty_Internal_Template $_template)
  70. {
  71. $_template->startRenderCallbacks[] = array($this, 'startRender');
  72. $_template->endRenderCallbacks[] = array($this, 'endRender');
  73. $this->startRender($_template);
  74. $this->isRegistered = true;
  75. }
  76. /**
  77. * Start render callback
  78. *
  79. * @param \Smarty_Internal_Template $_template
  80. */
  81. public function startRender(Smarty_Internal_Template $_template)
  82. {
  83. $this->countStack[] = $this->captureCount;
  84. $this->captureCount = 0;
  85. }
  86. /**
  87. * Close capture section
  88. *
  89. * @param \Smarty_Internal_Template $_template
  90. *
  91. * @throws \SmartyException
  92. */
  93. public function close(Smarty_Internal_Template $_template)
  94. {
  95. if ($this->captureCount) {
  96. list($buffer, $assign, $append) = array_pop($this->captureStack);
  97. $this->captureCount --;
  98. if (isset($assign)) {
  99. $_template->assign($assign, ob_get_contents());
  100. }
  101. if (isset($append)) {
  102. $_template->append($append, ob_get_contents());
  103. }
  104. $this->namedBuffer[ $buffer ] = ob_get_clean();
  105. } else {
  106. $this->error($_template);
  107. }
  108. }
  109. /**
  110. * Error exception on not matching {capture}{/capture}
  111. *
  112. * @param \Smarty_Internal_Template $_template
  113. *
  114. * @throws \SmartyException
  115. */
  116. public function error(Smarty_Internal_Template $_template)
  117. {
  118. throw new SmartyException("Not matching {capture}{/capture} in \"{$_template->template_resource}\"");
  119. }
  120. /**
  121. * Return content of named capture buffer
  122. *
  123. * @param \Smarty_Internal_Template $_template
  124. * @param $name
  125. *
  126. * @return null
  127. */
  128. public function getBuffer(Smarty_Internal_Template $_template, $name)
  129. {
  130. return isset($this->namedBuffer[ $name ]) ? $this->namedBuffer[ $name ] : null;
  131. }
  132. /**
  133. * End render callback
  134. *
  135. * @param \Smarty_Internal_Template $_template
  136. *
  137. * @throws \SmartyException
  138. */
  139. public function endRender(Smarty_Internal_Template $_template)
  140. {
  141. if ($this->captureCount) {
  142. $this->error($_template);
  143. } else {
  144. $this->captureCount = array_pop($this->countStack);
  145. }
  146. }
  147. }