/library/Smarty/sysplugins/smarty_internal_compile_private_print_expression.php

https://gitlab.com/Shenglian/SmartyProject · PHP · 162 lines · 90 code · 6 blank · 66 comment · 21 complexity · 68ed73cefaba083c16f3ad1a5ef10184 MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Print Expression
  4. * Compiles any tag which will output an expression or variable
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Print Expression Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $optional_attributes = array('assign');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $option_flags = array('nocache', 'nofilter');
  32. /**
  33. * Compiles code for generating output from any expression
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  37. * @param array $parameter array with compilation parameter
  38. *
  39. * @return string
  40. * @throws \SmartyException
  41. */
  42. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  43. {
  44. // check and get attributes
  45. $_attr = $this->getAttributes($compiler, $args);
  46. $output = $parameter[ 'value' ];
  47. // tag modifier
  48. if (!empty($parameter[ 'modifierlist' ])) {
  49. $output = $compiler->compileTag('private_modifier', array(),
  50. array('modifierlist' => $parameter[ 'modifierlist' ],
  51. 'value' => $output));
  52. }
  53. if (isset($_attr[ 'assign' ])) {
  54. // assign output to variable
  55. return "<?php \$_smarty_tpl->assign({$_attr['assign']},{$output});?>";
  56. } else {
  57. // display value
  58. if (!$_attr[ 'nofilter' ]) {
  59. // default modifier
  60. if (!empty($compiler->smarty->default_modifiers)) {
  61. if (empty($compiler->default_modifier_list)) {
  62. $modifierlist = array();
  63. foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
  64. preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/',
  65. $single_default_modifier, $mod_array);
  66. for ($i = 0, $count = count($mod_array[ 0 ]); $i < $count; $i ++) {
  67. if ($mod_array[ 0 ][ $i ] != ':') {
  68. $modifierlist[ $key ][] = $mod_array[ 0 ][ $i ];
  69. }
  70. }
  71. }
  72. $compiler->default_modifier_list = $modifierlist;
  73. }
  74. $output = $compiler->compileTag('private_modifier', array(),
  75. array('modifierlist' => $compiler->default_modifier_list,
  76. 'value' => $output));
  77. }
  78. // autoescape html
  79. if ($compiler->template->smarty->escape_html) {
  80. $output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
  81. }
  82. // loop over registered filters
  83. if (!empty($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ])) {
  84. foreach ($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ] as $key =>
  85. $function) {
  86. if (!is_array($function)) {
  87. $output = "{$function}({$output},\$_smarty_tpl)";
  88. } elseif (is_object($function[ 0 ])) {
  89. $output =
  90. "\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE]['{$key}'][0]->{$function[1]}({$output},\$_smarty_tpl)";
  91. } else {
  92. $output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
  93. }
  94. }
  95. }
  96. // auto loaded filters
  97. if (isset($compiler->smarty->autoload_filters[ Smarty::FILTER_VARIABLE ])) {
  98. foreach ((array) $compiler->template->smarty->autoload_filters[ Smarty::FILTER_VARIABLE ] as $name)
  99. {
  100. $result = $this->compile_output_filter($compiler, $name, $output);
  101. if ($result !== false) {
  102. $output = $result;
  103. } else {
  104. // not found, throw exception
  105. throw new SmartyException("Unable to load filter '{$name}'");
  106. }
  107. }
  108. }
  109. foreach ($compiler->variable_filters as $filter) {
  110. if (count($filter) == 1 &&
  111. ($result = $this->compile_output_filter($compiler, $filter[ 0 ], $output)) !== false
  112. ) {
  113. $output = $result;
  114. } else {
  115. $output = $compiler->compileTag('private_modifier', array(),
  116. array('modifierlist' => array($filter), 'value' => $output));
  117. }
  118. }
  119. }
  120. $compiler->has_output = true;
  121. $output = "<?php echo {$output};?>";
  122. }
  123. return $output;
  124. }
  125. /**
  126. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  127. * @param string $name name of variable filter
  128. * @param string $output embedded output
  129. *
  130. * @return string
  131. */
  132. private function compile_output_filter(Smarty_Internal_TemplateCompilerBase $compiler, $name, $output)
  133. {
  134. $plugin_name = "smarty_variablefilter_{$name}";
  135. $path = $compiler->smarty->loadPlugin($plugin_name);
  136. if ($path) {
  137. /**
  138. if ($compiler->template->caching) {
  139. $compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'file' ] =
  140. $path;
  141. $compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'function' ] =
  142. $plugin_name;
  143. } else {
  144. $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'file' ] =
  145. $path;
  146. $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'function' ] =
  147. $plugin_name;
  148. }
  149. * */
  150. return "{$plugin_name}({$output},\$_smarty_tpl)";
  151. } else {
  152. // not found
  153. return false;
  154. }
  155. }
  156. }