]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_compile_break.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_compile_break.php
1 <?php
2 /**
3  * Smarty Internal Plugin Compile Break
4  * Compiles the {break} tag
5  *
6  * @package    Smarty
7  * @subpackage Compiler
8  * @author     Uwe Tews
9  */
10
11 /**
12  * Smarty Internal Plugin Compile Break Class
13  *
14  * @package    Smarty
15  * @subpackage Compiler
16  */
17 class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
18 {
19     /**
20      * Attribute definition: Overwrites base class.
21      *
22      * @var array
23      * @see Smarty_Internal_CompileBase
24      */
25     public $optional_attributes = array('levels');
26
27     /**
28      * Attribute definition: Overwrites base class.
29      *
30      * @var array
31      * @see Smarty_Internal_CompileBase
32      */
33     public $shorttag_order = array('levels');
34
35     /**
36      * Compiles code for the {break} tag
37      *
38      * @param  array                                $args      array with attributes from parser
39      * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
40      * @param  array                                $parameter array with compilation parameter
41      *
42      * @return string compiled code
43      */
44     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
45     {
46         list($levels, $foreachLevels) = $this->checkLevels($args, $compiler);
47         $output = "<?php\n";
48         if ($foreachLevels) {
49             /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
50             $foreachCompiler = $compiler->getTagCompiler('foreach');
51             $output .= $foreachCompiler->compileRestore($foreachLevels);
52         }
53         $output .= "break {$levels};?>";
54         return $output;
55     }
56
57     /**
58      * check attributes and return array of break and foreach levels
59      *
60      * @param  array                                $args     array with attributes from parser
61      * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
62      * @param  string                               $tag      tag name
63      *
64      * @return array
65      * @throws \SmartyCompilerException
66      */
67     public function checkLevels($args, Smarty_Internal_TemplateCompilerBase $compiler, $tag = 'break')
68     {
69         static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
70         // check and get attributes
71         $_attr = $this->getAttributes($compiler, $args);
72
73         if ($_attr[ 'nocache' ] === true) {
74             $compiler->trigger_template_error('nocache option not allowed', null, true);
75         }
76
77         if (isset($_attr[ 'levels' ])) {
78             if (!is_numeric($_attr[ 'levels' ])) {
79                 $compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
80             }
81             $levels = $_attr[ 'levels' ];
82         } else {
83             $levels = 1;
84         }
85         $level_count = $levels;
86         $stack_count = count($compiler->_tag_stack) - 1;
87         $foreachLevels = 0;
88         $lastTag = '';
89         while ($level_count >= 0 && $stack_count >= 0) {
90             if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
91                 $lastTag = $compiler->_tag_stack[ $stack_count ][ 0 ];
92                 if ($level_count === 0) {
93                     break;
94                 }
95                 $level_count --;
96                 if ($compiler->_tag_stack[ $stack_count ][ 0 ] === 'foreach') {
97                     $foreachLevels ++;
98                 }
99             }
100             $stack_count --;
101         }
102         if ($level_count != 0) {
103             $compiler->trigger_template_error("cannot {$tag} {$levels} level(s)", null, true);
104         }
105         if ($lastTag === 'foreach' && $tag === 'break') {
106             $foreachLevels --;
107         }
108         return array($levels, $foreachLevels);
109     }
110 }