PageRenderTime 69ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/php5-3/core/tools/smarty/sysplugins/SmartyInternalCompilePrivatePrintExpression.class.php

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