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