error_reporting E_ALL friendly Smarty_compiler

From: Date: Mon, 23 Apr 2001 18:25:19 +0000
Subject: error_reporting E_ALL friendly Smarty_compiler
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-567@lists.php.net to get a copy of this message
The included patch makes Smarty_compiler, and the compiled templates, work without a warning with error_reporting set to E_ALL. (Tested with the default index.tpl.) The original class relied on E_ALL &~ E_NOTICE: First, there were couple of list($foo, $bar) = $array lines where $bar wasn't always set. I could have just prepended the statements with @, but I don't like that approach. An example: - list($var_ref, $modifiers) = explode('|', $section_prop_expr, 2); + $section_prop_expr = explode('|', $section_prop_expr, 2); + $var_ref = $section_prop_expr[0]; + if (isset($section_prop_expr[1])) { + $modifiers = $section_prop_expr[1]; + } else { + $modifiers = '' ; + } Second, _compile_files() emitted warnings if $text_blocks[$curr_tb] wasn't set. I added a simple isset() before the preg_match_all() call. Third, the compiled templates relied on E_ALL &~ E_NOTICE in the config-setting code. I added two isset()s, and an explicit declaration of $_smarty_config. There's probably more that needs patching, but at least the default index.tpl now compiles, and index.tpl.php runs, without a notice. I know this patch is somewhat lengthy, but it's easy to review. If you want me to break it into a set of isolated patches, let me know. Oh, BTW, is there actually any interest in making Smarty error_reporting E_ALL - friendly? cynic@mail.cz ------------- And the eyes of them both were opened and they saw that their files were world readable and writable, so they chmoded 600 their files. - Book of Installation chapt 3 sec 7

--- smarty_compiler.class.php.orig Wed Mar 28 17:44:40 2001 +++ smarty_compiler.class.php Mon Apr 23 01:12:24 2001 @@ -164,8 +164,8 @@ /* loop through text blocks */ for ($curr_tb = 0; $curr_tb <= count($text_blocks); $curr_tb++) { - /* match anything within <? ?> */ - if (preg_match_all('!(<\?[^?]*?\?>|<script\s+language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $text_blocks[$curr_tb], $sp_match)) { + /* match anything within <? ? > */ + if ( isset($text_blocks[$curr_tb]) && preg_match_all('!(<\?[^?]*?\?>|<script\s+language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $text_blocks[$curr_tb], $sp_match)) { /* found at least one match, loop through each one */ for ($curr_sp = 0; $curr_sp < count($sp_match[0]); $curr_sp++) { if (preg_match('!^(<\?(php\s|\s|=\s)|<script\s*language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $sp_match[0][$curr_sp])) { @@ -265,8 +265,10 @@ ) (?:\s+(.*))? /xs', $template_tag, $match); - list(, $tag_command, $tag_args) = $match; - + $tag_command = $match[1]; + if (isset($match[2])) { + $tag_args = $match[2]; + } /* If the tag name matches a variable or section property definition, we simply process it. */ if (preg_match('!^\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a variable @@ -403,10 +405,13 @@ $output = "<?php if (!class_exists('Config_File'))\n" . " include_once 'Config_File.class.php';\n" . - "if (!is_object(\$GLOBALS['_smarty_conf_obj']) || get_class(\$GLOBALS['_smarty_conf_obj']) != 'config_file')\n" . + "if (!isset(\$GLOBALS['_smarty_conf_obj']) || (!is_object(\$GLOBALS['_smarty_conf_obj']) || get_class(\$GLOBALS['_smarty_conf_obj']) != 'config_file'))\n" . " \$GLOBALS['_smarty_conf_obj'] = new Config_File('".$this->config_dir."');\n" . "if (isset(\$_smarty_config_parent) && $update_parent)\n" . " \$_smarty_config_parent = array_merge((array)\$_smarty_config_parent, \$GLOBALS['_smarty_conf_obj']->get(".$attrs['file']."));\n" . + "if (!isset(\$_smarty_config)) {\n" . + "\t\$_smarty_config = array();" . + "}" . "\$_smarty_config = array_merge((array)\$_smarty_config, \$GLOBALS['_smarty_conf_obj']->get(".$attrs['file']."));\n"; if (!empty($attrs['section'])) { @@ -450,7 +455,7 @@ return "<?php " . "if (!function_exists('$include_func_name')) {\n". " function $include_func_name(\$_smarty_file_name, \$_smarty_def_vars," . - " \$_smarty_include_vars, &\$_smarty_config_parent)\n" . + " \$_smarty_include_vars, &\$_smarty_config_parent)\n" . " {\n" . " extract(\$_smarty_def_vars);\n" . " extract(\$_smarty_include_vars);\n" . @@ -508,6 +513,7 @@ if (isset($attrs['show'])) { $show_check_code = "{$section_props}['show'] && "; } else { + $show_check_code = '' ; $output .= "{$section_props}['show'] = {$section_props}['loop'] > 0;\n"; } @@ -643,8 +649,10 @@ if (($first_token = array_shift($tokens)) == 'not') { $negate_expr = true; $expr_type = array_shift($tokens); - } else + } else { $expr_type = $first_token; + $negate_expr = false; + } switch ($expr_type) { case 'even': @@ -826,8 +834,13 @@ \*======================================================================*/ function _parse_var($var_expr) { - list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2); - + $var_expr = explode('|', substr($var_expr, 1), 2); + $var_ref = $var_expr[0]; + if (isset($var_expr[1])) { + $modifiers = $var_expr[1]; + } else { + $modifiers = '' ; + } $sections = explode('/', $var_ref); $props = explode('.', array_pop($sections)); $var_name = array_shift($props); @@ -835,9 +848,13 @@ $output = "\$$var_name"; foreach ($sections as $section_ref) { - list($section, $section_prop) = explode('.', $section_ref); - if (!isset($section_prop)) + $section_ref = explode('.', $section_ref); + $section = $section_ref[0]; + if (isset($section_ref[1])) { + $section_prop = $section_ref[1]; + } else { $section_prop = 'index'; + } $output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]"; } foreach ($props as $prop) { @@ -855,9 +872,13 @@ \*======================================================================*/ function _parse_conf_var($conf_var_expr) { - list($var_ref, $modifiers) = explode('|', $conf_var_expr, 2); - - $var_name = substr($var_ref, 1, -1); + $conf_var_expr = explode('|', $conf_var_expr, 2); + if(isset($conf_var_expr[1])) { + $modifiers = $conf_var_expr[1]; + } else { + $modifiers = ''; + } + $var_name = substr($conf_var_expr[0], 1, -1); $output = "\$_smarty_config['$var_name']"; @@ -872,7 +893,13 @@ \*======================================================================*/ function _parse_section_prop($section_prop_expr) { - list($var_ref, $modifiers) = explode('|', $section_prop_expr, 2); + $section_prop_expr = explode('|', $section_prop_expr, 2); + $var_ref = $section_prop_expr[0]; + if (isset($section_prop_expr[1])) { + $modifiers = $section_prop_expr[1]; + } else { + $modifiers = '' ; + } preg_match('!%(\w+)\.(\w+)%!', $var_ref, $match); $section_name = $match[1];
« previous php.smarty.general (#567) next »