| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |  * Smarty Internal Plugin Config File Compiler | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |  * This is the config file compiler class. It calls the lexer and parser to | 
					
						
							|  |  |  |  * perform the compiling. | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage Config | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @author Uwe Tews | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |  * Main config file compiler class | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage Config | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-08-08 17:28:23 +00:00
										 |  |  | class Smarty_Internal_Config_File_Compiler { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Lexer object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $lex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Parser object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $parser; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Smarty object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var Smarty object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Smarty object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var Smarty_Internal_Config object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Compiled config data sections and variables | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $config_data = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Initialize compiler | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty $smarty base instance | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-08-08 17:28:23 +00:00
										 |  |  |     public function __construct($smarty) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $this->smarty = $smarty; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         $this->config_data['sections'] = array(); | 
					
						
							|  |  |  |         $this->config_data['vars'] = array(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * Method to compile a Smarty template. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  Smarty_Internal_Config $config config object | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |      * @return bool true if compiling succeeded, false if it failed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function compileSource(Smarty_Internal_Config $config) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         /* here is where the compiling takes place. Smarty | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |           tags in the templates are replaces with PHP code, | 
					
						
							|  |  |  |           then written to compiled files. */ | 
					
						
							|  |  |  |         $this->config = $config; | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |         // get config file source
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $_content = $config->source->content . "\n"; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // on empty template just return
 | 
					
						
							|  |  |  |         if ($_content == '') { | 
					
						
							|  |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // init the lexer/parser to compile the config file
 | 
					
						
							| 
									
										
										
										
											2009-08-08 17:28:23 +00:00
										 |  |  |         $lex = new Smarty_Internal_Configfilelexer($_content, $this->smarty); | 
					
						
							| 
									
										
										
										
											2010-04-28 20:30:27 +00:00
										 |  |  |         $parser = new Smarty_Internal_Configfileparser($lex, $this); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         if ($this->smarty->_parserdebug) $parser->PrintTrace(); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // get tokens from lexer and parse them
 | 
					
						
							|  |  |  |         while ($lex->yylex()) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             if ($this->smarty->_parserdebug) echo "<br>Parsing  {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n"; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             $parser->doParse($lex->token, $lex->value); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // finish parsing process
 | 
					
						
							|  |  |  |         $parser->doParse(0, 0); | 
					
						
							| 
									
										
										
										
											2010-04-28 20:30:27 +00:00
										 |  |  |         $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>'; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |      * display compiler error messages without dying | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |      * If parameter $args is empty it is a parser detected syntax error. | 
					
						
							|  |  |  |      * In this case the parser is called to obtain information about exspected tokens. | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |      * If parameter $args contains a string this is used as error message | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param string $args individual error message or null | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     public function trigger_config_file_error($args = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->lex = Smarty_Internal_Configfilelexer::instance(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $this->parser = Smarty_Internal_Configfileparser::instance(); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // get template source line which has error
 | 
					
						
							|  |  |  |         $line = $this->lex->line; | 
					
						
							|  |  |  |         if (isset($args)) { | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  |             // $line--;
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         $match = preg_split("/\n/", $this->lex->data); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $error_text = "Syntax error in config file '{$this->config->source->filepath}' on line {$line} '{$match[$line-1]}' "; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         if (isset($args)) { | 
					
						
							|  |  |  |             // individual error message
 | 
					
						
							|  |  |  |             $error_text .= $args; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // exspected token from parser
 | 
					
						
							|  |  |  |             foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) { | 
					
						
							|  |  |  |                 $exp_token = $this->parser->yyTokenName[$token]; | 
					
						
							|  |  |  |                 if (isset($this->lex->smarty_token_names[$exp_token])) { | 
					
						
							|  |  |  |                     // token type from lexer
 | 
					
						
							|  |  |  |                     $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"'; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     // otherwise internal token name
 | 
					
						
							|  |  |  |                     $expect[] = $this->parser->yyTokenName[$token]; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             // output parser error message
 | 
					
						
							|  |  |  |             $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-08-13 10:39:51 +00:00
										 |  |  |         throw new SmartyCompilerException($error_text); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-29 15:41:01 +00:00
										 |  |  | ?>
 |