/PhPVersion/application/third_party/Smarty/libs/sysplugins/smarty_internal_compile_extends.php

https://gitlab.com/garabedian.kevin/web2TB · PHP · 134 lines · 63 code · 8 blank · 63 comment · 13 complexity · 2f36ae66f03072cdca7da3d98b589ddb MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile extend
  4. * Compiles the {extends} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile extend Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Extends extends Smarty_Internal_Compile_Shared_Inheritance
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('file');
  25. /**
  26. * Array of names of optional attribute required by tag
  27. * use array('_any') if there is no restriction of attributes names
  28. *
  29. * @var array
  30. */
  31. public $optional_attributes = array('extends_resource');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $shorttag_order = array('file');
  39. /**
  40. * Compiles code for the {extends} tag extends: resource
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  44. *
  45. * @return string compiled code
  46. * @throws \SmartyCompilerException
  47. * @throws \SmartyException
  48. */
  49. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  50. {
  51. // check and get attributes
  52. $_attr = $this->getAttributes($compiler, $args);
  53. if ($_attr[ 'nocache' ] === true) {
  54. $compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->line - 1);
  55. }
  56. if (strpos($_attr[ 'file' ], '$_tmp') !== false) {
  57. $compiler->trigger_template_error('illegal value for file attribute', $compiler->parser->lex->line - 1);
  58. }
  59. // add code to initialize inheritance
  60. $this->registerInit($compiler, true);
  61. $file = trim($_attr[ 'file' ], '\'"');
  62. if (strlen($file) > 8 && substr($file, 0, 8) == 'extends:') {
  63. // generate code for each template
  64. $files = array_reverse(explode('|', substr($file, 8)));
  65. $i = 0;
  66. foreach ($files as $file) {
  67. if ($file[ 0 ] == '"') {
  68. $file = trim($file, '".');
  69. } else {
  70. $file = "'{$file}'";
  71. }
  72. $i ++;
  73. if ($i == count($files) && isset($_attr[ 'extends_resource' ])) {
  74. $this->compileEndChild($compiler);
  75. }
  76. $this->compileInclude($compiler, $file);
  77. }
  78. if (!isset($_attr[ 'extends_resource' ])) {
  79. $this->compileEndChild($compiler);
  80. }
  81. } else {
  82. $this->compileEndChild($compiler);
  83. $this->compileInclude($compiler, $_attr[ 'file' ]);
  84. }
  85. $compiler->has_code = false;
  86. return '';
  87. }
  88. /**
  89. * Add code for inheritance endChild() method to end of template
  90. *
  91. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  92. */
  93. private function compileEndChild(Smarty_Internal_TemplateCompilerBase $compiler)
  94. {
  95. $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
  96. "<?php \$_smarty_tpl->inheritance->endChild();\n?>\n");
  97. }
  98. /**
  99. * Add code for including subtemplate to end of template
  100. *
  101. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  102. * @param string $file subtemplate name
  103. */
  104. private function compileInclude(Smarty_Internal_TemplateCompilerBase $compiler, $file)
  105. {
  106. $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
  107. $compiler->compileTag('include',
  108. array($file,
  109. array('scope' => 'parent'))));
  110. }
  111. /**
  112. * Create source code for {extends} from source components array
  113. *
  114. * @param []\Smarty_Internal_Template_Source $components
  115. *
  116. * @return string
  117. */
  118. public static function extendsSourceArrayCode($components)
  119. {
  120. $resources = array();
  121. foreach ($components as $source) {
  122. $resources[] = $source->resource;
  123. }
  124. return '{extends file=\'extends:' . join('|', $resources) . '\' extends_resource=true}';
  125. }
  126. }