PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/code/web/private_php/ams/smarty/libs/sysplugins/smarty_internal_compile_private_print_expression.php

https://bitbucket.org/ryzom/ryzomcore
PHP | 151 lines | 91 code | 6 blank | 54 comment | 26 complexity | db8c70c3c64d1d4e4dce5ed1ec048c3f MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0, GPL-3.0, LGPL-2.1
  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 object $compiler compiler object
  37. * @param array $parameter array with compilation parameter
  38. *
  39. * @throws SmartyException
  40. * @return string compiled code
  41. */
  42. public function compile($args, $compiler, $parameter)
  43. {
  44. // check and get attributes
  45. $_attr = $this->getAttributes($compiler, $args);
  46. // nocache option
  47. if ($_attr['nocache'] === true) {
  48. $compiler->tag_nocache = true;
  49. }
  50. if (isset($_attr['assign'])) {
  51. // assign output to variable
  52. $output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
  53. } else {
  54. // display value
  55. $output = $parameter['value'];
  56. // tag modifier
  57. if (!empty($parameter['modifierlist'])) {
  58. $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $parameter['modifierlist'], 'value' => $output));
  59. }
  60. if (!$_attr['nofilter']) {
  61. // default modifier
  62. if (!empty($compiler->smarty->default_modifiers)) {
  63. if (empty($compiler->default_modifier_list)) {
  64. $modifierlist = array();
  65. foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
  66. preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/', $single_default_modifier, $mod_array);
  67. for ($i = 0, $count = count($mod_array[0]); $i < $count; $i ++) {
  68. if ($mod_array[0][$i] != ':') {
  69. $modifierlist[$key][] = $mod_array[0][$i];
  70. }
  71. }
  72. }
  73. $compiler->default_modifier_list = $modifierlist;
  74. }
  75. $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $compiler->default_modifier_list, 'value' => $output));
  76. }
  77. // autoescape html
  78. if ($compiler->template->smarty->escape_html) {
  79. $output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
  80. }
  81. // loop over registered filters
  82. if (!empty($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE])) {
  83. foreach ($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE] as $key => $function) {
  84. if (!is_array($function)) {
  85. $output = "{$function}({$output},\$_smarty_tpl)";
  86. } elseif (is_object($function[0])) {
  87. $output = "\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE]['{$key}'][0]->{$function[1]}({$output},\$_smarty_tpl)";
  88. } else {
  89. $output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
  90. }
  91. }
  92. }
  93. // auto loaded filters
  94. if (isset($compiler->smarty->autoload_filters[Smarty::FILTER_VARIABLE])) {
  95. foreach ((array) $compiler->template->smarty->autoload_filters[Smarty::FILTER_VARIABLE] as $name) {
  96. $result = $this->compile_output_filter($compiler, $name, $output);
  97. if ($result !== false) {
  98. $output = $result;
  99. } else {
  100. // not found, throw exception
  101. throw new SmartyException("Unable to load filter '{$name}'");
  102. }
  103. }
  104. }
  105. if (isset($compiler->template->variable_filters)) {
  106. foreach ($compiler->template->variable_filters as $filter) {
  107. if (count($filter) == 1 && ($result = $this->compile_output_filter($compiler, $filter[0], $output)) !== false) {
  108. $output = $result;
  109. } else {
  110. $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => array($filter), 'value' => $output));
  111. }
  112. }
  113. }
  114. }
  115. $compiler->has_output = true;
  116. $output = "<?php echo {$output};?>";
  117. }
  118. return $output;
  119. }
  120. /**
  121. * @param object $compiler compiler object
  122. * @param string $name name of variable filter
  123. * @param string $output embedded output
  124. *
  125. * @return string
  126. */
  127. private function compile_output_filter($compiler, $name, $output)
  128. {
  129. $plugin_name = "smarty_variablefilter_{$name}";
  130. $path = $compiler->smarty->loadPlugin($plugin_name, false);
  131. if ($path) {
  132. if ($compiler->template->caching) {
  133. $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
  134. $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
  135. } else {
  136. $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
  137. $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
  138. }
  139. } else {
  140. // not found
  141. return false;
  142. }
  143. return "{$plugin_name}({$output},\$_smarty_tpl)";
  144. }
  145. }