1 <?php
3 /**
4 * Smarty Internal Plugin Config File Compiler
5 *
6 * This is the config file compiler class. It calls the lexer and parser to
7 * perform the compiling.
8 *
9 * @package Smarty
10 * @subpackage Config
11 * @author Uwe Tews
12 */
13 /**
14 * Main config file compiler class
15 */
16 class Smarty_Internal_Config_File_Compiler {
17 public $compile_error= false;
18 /**
19 * Initialize compiler
20 */
21 public function __construct($smarty)
22 {
23 $this->smarty = $smarty;
24 // get required plugins
25 $this->smarty->loadPlugin('Smarty_Internal_Configfilelexer');
26 $this->smarty->loadPlugin('Smarty_Internal_Configfileparser');
27 $this->config_data['sections'] = array();
28 $this->config_data['vars'] = array();
29 }
31 /**
32 * Methode to compile a Smarty template
33 *
34 * @param $template template object to compile
35 * @return bool true if compiling succeeded, false if it failed
36 */
37 public function compileSource($config)
38 {
39 /* here is where the compiling takes place. Smarty
40 tags in the templates are replaces with PHP code,
41 then written to compiled files. */
42 $this->config = $config;
43 // get config file source
44 $_content = $config->getConfigSource()."\n";
45 // on empty template just return
46 if ($_content == '') {
47 return true;
48 }
49 // init the lexer/parser to compile the config file
50 $lex = new Smarty_Internal_Configfilelexer($_content, $this->smarty);
51 $parser = new Smarty_Internal_Configfileparser($lex, $this);
52 // $parser->PrintTrace();
53 // get tokens from lexer and parse them
54 while ($lex->yylex()) {
55 // echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
56 $parser->doParse($lex->token, $lex->value);
57 }
58 // finish parsing process
59 $parser->doParse(0, 0);
61 $config->compiled_config = serialize($this->config_data);
62 if (!$this->compile_error) {
63 return true;
64 } else {
65 // compilation error
66 return false;
67 }
68 }
69 /**
70 * display compiler error messages without dying
71 *
72 * If parameter $args is empty it is a parser detected syntax error.
73 * In this case the parser is called to obtain information about exspected tokens.
74 *
75 * If parameter $args contains a string this is used as error message
76 *
77 * @todo output exact position of parse error in source line
78 * @param $args string individual error message or null
79 */
80 public function trigger_config_file_error($args = null)
81 {
82 $this->lex = Smarty_Internal_Configfilelexer::instance();
83 $this->parser = Smarty_Internal_Configfileparser::instance();
84 // get template source line which has error
85 $line = $this->lex->line;
86 if (isset($args)) {
87 // $line--;
88 }
89 $match = preg_split("/\n/", $this->lex->data);
90 $error_text = "Syntax error in config file '{$this->config->getConfigFilepath()}' on line {$line} '{$match[$line-1]}' ";
91 if (isset($args)) {
92 // individual error message
93 $error_text .= $args;
94 } else {
95 // exspected token from parser
96 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
97 $exp_token = $this->parser->yyTokenName[$token];
98 if (isset($this->lex->smarty_token_names[$exp_token])) {
99 // token type from lexer
100 $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
101 } else {
102 // otherwise internal token name
103 $expect[] = $this->parser->yyTokenName[$token];
104 }
105 }
106 // output parser error message
107 $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
108 }
109 throw new Exception($error_text);
110 // set error flag
111 $this->compile_error = true;
112 }
114 }
116 ?>