Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
/**
3
 * Smarty Internal Plugin Smarty Template Compiler Base
4
 * This file contains the basic classes and methods for compiling Smarty templates with lexer/parser
5
 *
6
 * @package    Smarty
7
 * @subpackage Compiler
8
 * @author     Uwe Tews
9
 */
10
 
11
/**
12
 * Class SmartyTemplateCompiler
13
 *
14
 * @package    Smarty
15
 * @subpackage Compiler
16
 */
17
class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCompilerBase
18
{
19
    /**
20
     * Lexer class name
21
     *
22
     * @var string
23
     */
24
    public $lexer_class;
25
 
26
    /**
27
     * Parser class name
28
     *
29
     * @var string
30
     */
31
    public $parser_class;
32
 
33
    /**
34
     * array of vars which can be compiled in local scope
35
     *
36
     * @var array
37
     */
38
    public $local_var = array();
39
 
40
    /**
41
     * array of callbacks called when the normal compile process of template is finished
42
     *
43
     * @var array
44
     */
45
    public $postCompileCallbacks = array();
46
 
47
    /**
48
     * prefix code
49
     *
50
     * @var string
51
     */
52
    public $prefixCompiledCode = '';
53
 
54
    /**
55
     * postfix code
56
     *
57
     * @var string
58
     */
59
    public $postfixCompiledCode = '';
60
 
61
    /**
62
     * Initialize compiler
63
     *
64
     * @param string $lexer_class  class name
65
     * @param string $parser_class class name
66
     * @param Smarty $smarty       global instance
67
     */
68
    public function __construct($lexer_class, $parser_class, Smarty $smarty)
69
    {
70
        parent::__construct($smarty);
71
        // get required plugins
72
        $this->lexer_class = $lexer_class;
73
        $this->parser_class = $parser_class;
74
    }
75
 
76
    /**
77
     * method to compile a Smarty template
78
     *
79
     * @param mixed $_content template source
80
     * @param bool  $isTemplateSource
81
     *
82
     * @return bool true if compiling succeeded, false if it failed
83
     * @throws \SmartyCompilerException
84
     */
85
    protected function doCompile($_content, $isTemplateSource = false)
86
    {
87
        /* here is where the compiling takes place. Smarty
88
          tags in the templates are replaces with PHP code,
89
          then written to compiled files. */
90
        // init the lexer/parser to compile the template
91
        $this->parser =
92
            new $this->parser_class(
93
                new $this->lexer_class(
94
                    str_replace(
95
                        array(
96
                            "\r\n",
97
                            "\r"
98
                        ),
99
                        "\n",
100
                        $_content
101
                    ),
102
                    $this
103
                ),
104
                $this
105
            );
106
        if ($isTemplateSource && $this->template->caching) {
107
            $this->parser->insertPhpCode("<?php\n\$_smarty_tpl->compiled->nocache_hash = '{$this->nocache_hash}';\n?>\n");
108
        }
109
        if (function_exists('mb_internal_encoding')
110
            && function_exists('ini_get')
111
            && ((int)ini_get('mbstring.func_overload')) & 2
112
        ) {
113
            $mbEncoding = mb_internal_encoding();
114
            mb_internal_encoding('ASCII');
115
        } else {
116
            $mbEncoding = null;
117
        }
118
        if ($this->smarty->_parserdebug) {
119
            $this->parser->PrintTrace();
120
            $this->parser->lex->PrintTrace();
121
        }
122
        // get tokens from lexer and parse them
123
        while ($this->parser->lex->yylex()) {
124
            if ($this->smarty->_parserdebug) {
125
                echo "<pre>Line {$this->parser->lex->line} Parsing  {$this->parser->yyTokenName[$this->parser->lex->token]} Token " .
126
                     htmlentities($this->parser->lex->value) . "</pre>";
127
            }
128
            $this->parser->doParse($this->parser->lex->token, $this->parser->lex->value);
129
        }
130
        // finish parsing process
131
        $this->parser->doParse(0, 0);
132
        if ($mbEncoding) {
133
            mb_internal_encoding($mbEncoding);
134
        }
135
        // check for unclosed tags
136
        if (count($this->_tag_stack) > 0) {
137
            // get stacked info
138
            list($openTag, $_data) = array_pop($this->_tag_stack);
139
            $this->trigger_template_error(
140
                "unclosed {$this->smarty->left_delimiter}" . $openTag .
141
                "{$this->smarty->right_delimiter} tag"
142
            );
143
        }
144
        // call post compile callbacks
145
        foreach ($this->postCompileCallbacks as $cb) {
146
            $parameter = $cb;
147
            $parameter[ 0 ] = $this;
148
            call_user_func_array($cb[ 0 ], $parameter);
149
        }
150
        // return compiled code
151
        return $this->prefixCompiledCode . $this->parser->retvalue . $this->postfixCompiledCode;
152
    }
153
 
154
    /**
155
     * Register a post compile callback
156
     * - when the callback is called after template compiling the compiler object will be inserted as first parameter
157
     *
158
     * @param callback $callback
159
     * @param array    $parameter optional parameter array
160
     * @param string   $key       optional key for callback
161
     * @param bool     $replace   if true replace existing keyed callback
162
     */
163
    public function registerPostCompileCallback($callback, $parameter = array(), $key = null, $replace = false)
164
    {
165
        array_unshift($parameter, $callback);
166
        if (isset($key)) {
167
            if ($replace || !isset($this->postCompileCallbacks[ $key ])) {
168
                $this->postCompileCallbacks[ $key ] = $parameter;
169
            }
170
        } else {
171
            $this->postCompileCallbacks[] = $parameter;
172
        }
173
    }
174
 
175
    /**
176
     * Remove a post compile callback
177
     *
178
     * @param string $key callback key
179
     */
180
    public function unregisterPostCompileCallback($key)
181
    {
182
        unset($this->postCompileCallbacks[ $key ]);
183
    }
184
}