summaryrefslogtreecommitdiff
path: root/lib/smarty3/sysplugins/smarty_internal_compile_private_foreachsection.php
blob: d3aab24bb08d3617a54bb8e68f3296cc05f264c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
 * Smarty Internal Plugin Compile ForeachSection
 * Shared methods for {foreach} {section} tags
 *
 * @package    Smarty
 * @subpackage Compiler
 * @author     Uwe Tews
 */

/**
 * Smarty Internal Plugin Compile ForeachSection Class
 *
 * @package    Smarty
 * @subpackage Compiler
 */
class Smarty_Internal_Compile_Private_ForeachSection extends Smarty_Internal_CompileBase
{
    /**
     * Name of this tag
     *
     * @var string
     */
    public $tagName = '';

    /**
     * Valid properties of $smarty.xxx variable
     *
     * @var array
     */
    public $nameProperties = array();

    /**
     * {section} tag has no item properties
     *
     * @var array
     */
    public $itemProperties = null;

    /**
     * {section} tag has always name attribute
     *
     * @var bool
     */
    public $isNamed = true;

    /**
     * @var array
     */
    public $matchResults = array();

    /**
     * Preg search pattern
     *
     * @var string
     */
    private $propertyPreg = '';

    /**
     * Offsets in preg match result
     *
     * @var array
     */
    private $resultOffsets = array();

    /**
     * Start offset
     *
     * @var int
     */
    private $startOffset = 0;

    /**
     * Scan sources for used tag attributes
     *
     * @param array                                 $attributes
     * @param \Smarty_Internal_TemplateCompilerBase $compiler
     *
     * @throws \SmartyException
     */
    public function scanForProperties($attributes, Smarty_Internal_TemplateCompilerBase $compiler)
    {
        $this->propertyPreg = '~(';
        $this->startOffset = 1;
        $this->resultOffsets = array();
        $this->matchResults = array('named' => array(), 'item' => array());
        if (isset($attributes[ 'name' ])) {
            $this->buildPropertyPreg(true, $attributes);
        }
        if (isset($this->itemProperties)) {
            if ($this->isNamed) {
                $this->propertyPreg .= '|';
            }
            $this->buildPropertyPreg(false, $attributes);
        }
        $this->propertyPreg .= ')\W~i';
        // Template source
        $this->matchTemplateSource($compiler);
        // Parent template source
        $this->matchParentTemplateSource($compiler);
        // {block} source
        $this->matchBlockSource($compiler);
    }

    /**
     * Build property preg string
     *
     * @param bool  $named
     * @param array $attributes
     */
    public function buildPropertyPreg($named, $attributes)
    {
        if ($named) {
            $this->resultOffsets[ 'named' ] = $this->startOffset = $this->startOffset + 3;
            $this->propertyPreg .= "(([\$]smarty[.]{$this->tagName}[.]" .
                                   ($this->tagName === 'section' ? "|[\[]\s*" : '') .
                                   "){$attributes['name']}[.](";
            $properties = $this->nameProperties;
        } else {
            $this->resultOffsets[ 'item' ] = $this->startOffset = $this->startOffset + 2;
            $this->propertyPreg .= "([\$]{$attributes['item']}[@](";
            $properties = $this->itemProperties;
        }
        $propName = reset($properties);
        while ($propName) {
            $this->propertyPreg .= "{$propName}";
            $propName = next($properties);
            if ($propName) {
                $this->propertyPreg .= '|';
            }
        }
        $this->propertyPreg .= '))';
    }

    /**
     * Find matches in source string
     *
     * @param string $source
     */
    public function matchProperty($source)
    {
        preg_match_all($this->propertyPreg, $source, $match);
        foreach ($this->resultOffsets as $key => $offset) {
            foreach ($match[ $offset ] as $m) {
                if (!empty($m)) {
                    $this->matchResults[ $key ][ strtolower($m) ] = true;
                }
            }
        }
    }

    /**
     * Find matches in template source
     *
     * @param \Smarty_Internal_TemplateCompilerBase $compiler
     */
    public function matchTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
    {
        $this->matchProperty($compiler->parser->lex->data);
    }

    /**
     * Find matches in all parent template source
     *
     * @param \Smarty_Internal_TemplateCompilerBase $compiler
     *
     * @throws \SmartyException
     */
    public function matchParentTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
    {
        // search parent compiler template source
        $nextCompiler = $compiler;
        while ($nextCompiler !== $nextCompiler->parent_compiler) {
            $nextCompiler = $nextCompiler->parent_compiler;
            if ($compiler !== $nextCompiler) {
                // get template source
                $_content = $nextCompiler->template->source->getContent();
                if ($_content !== '') {
                    // run pre filter if required
                    if ((isset($nextCompiler->smarty->autoload_filters[ 'pre' ]) ||
                         isset($nextCompiler->smarty->registered_filters[ 'pre' ]))
                    ) {
                        $_content = $nextCompiler->smarty->ext->_filterHandler->runFilter(
                            'pre',
                            $_content,
                            $nextCompiler->template
                        );
                    }
                    $this->matchProperty($_content);
                }
            }
        }
    }

    /**
     * Find matches in {block} tag source
     *
     * @param \Smarty_Internal_TemplateCompilerBase $compiler
     */
    public function matchBlockSource(Smarty_Internal_TemplateCompilerBase $compiler)
    {
    }

    /**
     * Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
     *
     * @param array                                 $args      array with attributes from parser
     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
     * @param array                                 $parameter array with compilation parameter
     *
     * @return string compiled code
     * @throws \SmartyCompilerException
     */
    public function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
    {
        $tag = strtolower(trim($parameter[ 0 ], '"\''));
        $name = isset($parameter[ 1 ]) ? $compiler->getId($parameter[ 1 ]) : false;
        if (!$name) {
            $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
        }
        $property = isset($parameter[ 2 ]) ? strtolower($compiler->getId($parameter[ 2 ])) : false;
        if (!$property || !in_array($property, $this->nameProperties)) {
            $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} property attribute", null, true);
        }
        $tagVar = "'__smarty_{$tag}_{$name}'";
        return "(isset(\$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}'] : null)";
    }
}