/test/distributions/smarty/3.2dev/libs/sysplugins/smarty_internal_compile_private_registered_block.php

https://github.com/rodneyrehm/php-template-engines · PHP · 126 lines · 77 code · 6 blank · 43 comment · 32 complexity · 0d58ea55f33e426c4bb8516dee1e03f8 MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Registered Block
  4. *
  5. * Compiles code for the execution of a registered block function
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Registered Block Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Private_Registered_Block extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $optional_attributes = array('_any');
  25. /**
  26. * Compiles code for the execution of a block function
  27. *
  28. * @param array $args array with attributes from parser
  29. * @param object $compiler compiler object
  30. * @param array $parameter array with compilation parameter
  31. * @param string $tag name of block function
  32. * @return string compiled code
  33. */
  34. public function compile($args, $compiler, $parameter, $tag) {
  35. if (!isset($tag[5]) || substr($tag, -5) != 'close') {
  36. // opening tag of block plugin
  37. // check and get attributes
  38. $_attr = $this->getAttributes($compiler, $args);
  39. if ($_attr['nocache']) {
  40. $compiler->tag_nocache = true;
  41. }
  42. unset($_attr['nocache']);
  43. if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$tag])) {
  44. $tag_info = $compiler->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$tag];
  45. } else {
  46. $tag_info = $compiler->default_handler_plugins[Smarty::PLUGIN_BLOCK][$tag];
  47. }
  48. $function = $tag_info[0];
  49. // convert attributes into parameter string
  50. $par_string = $this->getPluginParameterString($function, $_attr, $compiler, true, $tag_info[2]);
  51. $this->openTag($compiler, $tag, array($par_string, $compiler->nocache));
  52. // maybe nocache because of nocache variables or nocache plugin
  53. $compiler->nocache = !$tag_info[1] | $compiler->nocache | $compiler->tag_nocache;
  54. // compile code
  55. if (is_array($par_string)) {
  56. // old style with params array
  57. if (!is_array($function)) {
  58. $output = "<?php \$_smarty_tpl->smarty->_tag_stack[] = array('{$tag}', {$par_string['par']}); \$_block_repeat=true; echo {$function}({$par_string['par']}, null, {$par_string['obj']}, \$_block_repeat);while (\$_block_repeat) { ob_start();?>";
  59. } else if (is_object($function[0])) {
  60. $output = "<?php \$_smarty_tpl->smarty->_tag_stack[] = array('{$tag}', {$par_string['par']}); \$_block_repeat=true; echo \$_smarty_tpl->smarty->registered_plugins['block']['{$tag}'][0][0]->{$function[1]}({$par_string['par']}, null, {$par_string['obj']}, \$_block_repeat);while (\$_block_repeat) { ob_start();?>";
  61. } else {
  62. $output = "<?php \$_smarty_tpl->smarty->_tag_stack[] = array('{$tag}', {$par_string['par']}); \$_block_repeat=true; echo {$function[0]}::{$function[1]}({$par_string['par']}, null, {$par_string['obj']}, \$_block_repeat);while (\$_block_repeat) { ob_start();?>";
  63. }
  64. } else {
  65. // new style with real parameter
  66. $par_string = str_replace('__content__', 'null', $par_string);
  67. if (!is_array($function)) {
  68. $output = "<?php \$_block_repeat=true; \$_smarty_tpl->smarty->_tag_stack[] = array('{$tag}', {$par_string}); echo {$function}({$par_string});while (\$_block_repeat) { ob_start();?>";
  69. } else if (is_object($function[0])) {
  70. $output = "<?php \$_block_repeat=true; \$_smarty_tpl->smarty->_tag_stack[] = array('{$tag}', {$par_string}); echo \$_smarty_tpl->smarty->registered_plugins['block']['{$tag}'][0][0]->{$function[1]}({$par_string});while (\$_block_repeat) { ob_start();?>";
  71. } else {
  72. $output = "<?php \$_block_repeat=true; \$_smarty_tpl->smarty->_tag_stack[] = array('{$tag}', {$par_string}); echo {$function[0]}::{$function[1]}({$par_string});while (\$_block_repeat) { ob_start();?>";
  73. }
  74. }
  75. } else {
  76. // must endblock be nocache?
  77. if ($compiler->nocache) {
  78. $compiler->tag_nocache = true;
  79. }
  80. $base_tag = substr($tag, 0, -5);
  81. // closing tag of block plugin, restore nocache
  82. list($par_string, $compiler->nocache) = $this->closeTag($compiler, $base_tag);
  83. // This tag does create output
  84. $compiler->has_output = true;
  85. if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
  86. $function = $compiler->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag][0];
  87. } else {
  88. $function = $compiler->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag][0];
  89. }
  90. // compile code
  91. if (!isset($parameter['modifier_list'])) {
  92. $mod_pre = $mod_post = '';
  93. } else {
  94. $mod_pre = ' ob_start(); ';
  95. $mod_post = 'echo ' . $compiler->compileTag('private_modifier', array(), array('modifierlist' => $parameter['modifier_list'], 'value' => 'ob_get_clean()')) . ';';
  96. }
  97. if (is_array($par_string)) {
  98. // old style with params array
  99. if (!is_array($function)) {
  100. $output = "<?php \$_block_content = ob_get_clean(); \$_block_repeat=false;" . $mod_pre . " echo {$function}({$par_string['par']}, \$_block_content, {$par_string['obj']}, \$_block_repeat);" . $mod_post . " } array_pop(\$_smarty_tpl->smarty->_tag_stack);?>";
  101. } else if (is_object($function[0])) {
  102. $output = "<?php \$_block_content = ob_get_clean(); \$_block_repeat=false;" . $mod_pre . " echo \$_smarty_tpl->smarty->registered_plugins['block']['{$base_tag}'][0][0]->{$function[1]}({$par_string['par']}, \$_block_content, {$par_string['obj']}, \$_block_repeat); " . $mod_post . "} array_pop(\$_smarty_tpl->smarty->_tag_stack);?>";
  103. } else {
  104. $output = "<?php \$_block_content = ob_get_clean(); \$_block_repeat=false;" . $mod_pre . " echo {$function[0]}::{$function[1]}({$par_string['par']}, \$_block_content, {$par_string['obj']}, \$_block_repeat); " . $mod_post . "} array_pop(\$_smarty_tpl->smarty->_tag_stack);?>";
  105. }
  106. } else {
  107. // new style witn real parameter
  108. $par_string = str_replace('__content__', '$_block_content', $par_string);
  109. if (!is_array($function)) {
  110. $output = "<?php \$_block_content = ob_get_clean(); \$_block_repeat=false;" . $mod_pre . " echo {$function}({$par_string});" . $mod_post . " } array_pop(\$_smarty_tpl->smarty->_tag_stack);?>";
  111. } else if (is_object($function[0])) {
  112. $output = "<?php \$_block_content = ob_get_clean(); \$_block_repeat=false;" . $mod_pre . " echo \$_smarty_tpl->smarty->registered_plugins['block']['{$base_tag}'][0][0]->{$function[1]}({$par_string}); " . $mod_post . "} array_pop(\$_smarty_tpl->smarty->_tag_stack);?>";
  113. } else {
  114. $output = "<?php \$_block_content = ob_get_clean(); \$_block_repeat=false;" . $mod_pre . " echo {$function[0]}::{$function[1]}({$par_string}); " . $mod_post . "} array_pop(\$_smarty_tpl->smarty->_tag_stack);?>";
  115. }
  116. }
  117. }
  118. return $output . "\n";
  119. }
  120. }