source: trunk/include/smarty/libs/sysplugins/smarty_internal_config_file_compiler.php @ 23485

Last change on this file since 23485 was 23485, checked in by rvelices, 11 years ago

Smarty EOL style LF svn property

  • Property svn:eol-style set to LF
File size: 4.3 KB
Line 
1<?php
2/**
3 * Smarty Internal Plugin Config File Compiler
4 *
5 * This is the config file compiler class. It calls the lexer and parser to
6 * perform the compiling.
7 *
8 * @package Smarty
9 * @subpackage Config
10 * @author Uwe Tews
11 */
12
13/**
14 * Main config file compiler class
15 *
16 * @package Smarty
17 * @subpackage Config
18 */
19class Smarty_Internal_Config_File_Compiler {
20
21    /**
22     * Lexer object
23     *
24     * @var object
25     */
26    public $lex;
27
28    /**
29     * Parser object
30     *
31     * @var object
32     */
33    public $parser;
34
35    /**
36     * Smarty object
37     *
38     * @var Smarty object
39     */
40    public $smarty;
41
42    /**
43     * Smarty object
44     *
45     * @var Smarty_Internal_Config object
46     */
47    public $config;
48
49    /**
50     * Compiled config data sections and variables
51     *
52     * @var array
53     */
54    public $config_data = array();
55
56    /**
57     * Initialize compiler
58     *
59     * @param Smarty $smarty base instance
60     */
61    public function __construct($smarty)
62    {
63        $this->smarty = $smarty;
64        $this->config_data['sections'] = array();
65        $this->config_data['vars'] = array();
66    }
67
68    /**
69     * Method to compile a Smarty template.
70     *
71     * @param  Smarty_Internal_Config $config config object
72     * @return bool true if compiling succeeded, false if it failed
73     */
74    public function compileSource(Smarty_Internal_Config $config)
75    {
76        /* here is where the compiling takes place. Smarty
77          tags in the templates are replaces with PHP code,
78          then written to compiled files. */
79        $this->config = $config;
80        // get config file source
81        $_content = $config->source->content . "\n";
82        // on empty template just return
83        if ($_content == '') {
84            return true;
85        }
86        // init the lexer/parser to compile the config file
87        $lex = new Smarty_Internal_Configfilelexer($_content, $this->smarty);
88        $parser = new Smarty_Internal_Configfileparser($lex, $this);
89        if ($this->smarty->_parserdebug) $parser->PrintTrace();
90        // get tokens from lexer and parse them
91        while ($lex->yylex()) {
92            if ($this->smarty->_parserdebug) echo "<br>Parsing  {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
93            $parser->doParse($lex->token, $lex->value);
94        }
95        // finish parsing process
96        $parser->doParse(0, 0);
97        $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
98    }
99
100    /**
101     * display compiler error messages without dying
102     *
103     * If parameter $args is empty it is a parser detected syntax error.
104     * In this case the parser is called to obtain information about exspected tokens.
105     *
106     * If parameter $args contains a string this is used as error message
107     *
108     * @param string $args individual error message or null
109     */
110    public function trigger_config_file_error($args = null)
111    {
112        $this->lex = Smarty_Internal_Configfilelexer::instance();
113        $this->parser = Smarty_Internal_Configfileparser::instance();
114        // get template source line which has error
115        $line = $this->lex->line;
116        if (isset($args)) {
117            // $line--;
118        }
119        $match = preg_split("/\n/", $this->lex->data);
120        $error_text = "Syntax error in config file '{$this->config->source->filepath}' on line {$line} '{$match[$line-1]}' ";
121        if (isset($args)) {
122            // individual error message
123            $error_text .= $args;
124        } else {
125            // exspected token from parser
126            foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
127                $exp_token = $this->parser->yyTokenName[$token];
128                if (isset($this->lex->smarty_token_names[$exp_token])) {
129                    // token type from lexer
130                    $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
131                } else {
132                    // otherwise internal token name
133                    $expect[] = $this->parser->yyTokenName[$token];
134                }
135            }
136            // output parser error message
137            $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
138        }
139        throw new SmartyCompilerException($error_text);
140    }
141
142}
143
144?>
Note: See TracBrowser for help on using the repository browser.