shuther Smarty Rookie
Joined: 04 Jan 2004 Posts: 13
|
Posted: Fri Apr 16, 2004 3:20 pm Post subject: more compiler functions |
|
|
I think it would be good to have more compiler functions, or to remove/move the functions we have into compilers.
I did it for the ocnfig_load plug ins.
File: compiler.config_load.php
Code: | <?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {config_load} compiler plugin
*
* Type: compiler<br>
* Name: config_load<br>
* Purpose: load config file vars
* @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
* (Smarty online manual)
* @param array Format:
* <pre>
* array('file' => required config file name,
* 'section' => optional config file section to load
* 'scope' => local/parent/global
* 'global' => overrides scope, setting to parent if true)
* </pre>
* @param Smarty
*/
function smarty_compiler_config_load($tag_arg, &$smarty)
{
//var_dump($smarty);
//echo '<br /><br />';
//This function can be called directly through config_load
if (is_a($smarty, 'Smarty_Compiler')) {
$justLoad = false;
$params = $smarty->_parse_attrs($tag_arg);
$_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
$_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
$_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
$_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
} else {
$justLoad = true; //We just want to load the values into the memory
$_file = isset($tag_arg['file']) ? $tag_arg['file'] : null;
$_section = isset($tag_arg['section']) ? $tag_arg['section'] : null;
$_scope = isset($tag_arg['scope']) ? $tag_arg['scope'] : 'global';
$_global = isset($tag_arg['global']) ? $tag_arg['global'] : false;
}
if (!isset($_file) || strlen($_file) == 0) {
$smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
}
if (isset($_scope)) {
if ($_scope != 'local' &&
$_scope != 'parent' &&
$_scope != 'global') {
$smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
}
} else {
if ($_global) {
$_scope = 'parent';
} else {
$_scope = 'local';
}
}
$_params = array('resource_name' => $_file, 'resource_base_path' => $smarty->config_dir);
$smarty->_parse_resource_name($_params);
$_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
if (isset($_section)) {
$_file_path_full = $_file_path . '|' . $_section;
} else {
$_file_path_full = $_file_path;
}
$_compile_file = $smarty->_get_compile_path($_file_path_full);
if (!$justLoad && ($smarty->force_compile || $smarty->debugging || $smarty->compile_check)) {
$smarty->_add_plugin('function', 'config_load');
return sprintf('smarty_function_config_load(array("file" => "%s", "section" => "%s", "scope" => "%s", "global" => %s, "compile_file" => "%s", "file_path" => "%s"), $this);', addslashes($_file), addslashes($_section), $_scope, $_global? 'true': 'false', addslashes($_compile_file), addslashes($_file_path) );
}
if (!file_exists($_compile_file) || !$smarty->_is_compiled($_file_path, $_compile_file)) {
// compile config file
if(!is_object($smarty->_conf_obj)) {
require_once SMARTY_DIR . $smarty->config_class . '.class.php';
$smarty->_conf_obj = &new $smarty->config_class();
$smarty->_conf_obj->overwrite = $smarty->config_overwrite;
$smarty->_conf_obj->booleanize = $smarty->config_booleanize;
$smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
$smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
}
$_params = array('resource_name' => $_file, 'resource_base_path' => $smarty->config_dir);
if (!$smarty->_fetch_resource_info($_params)) {
return;
}
$smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
$_config_vars = array_merge($smarty->_conf_obj->get($_file), $smarty->_conf_obj->get($_file, $_section));
if (function_exists('var_export')) {
$_output = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; ?>';
} else {
$_output = '<?php $_config_vars = unserialize(\'' . strtr(serialize($_config_vars),array('\''=>'\\\'', '\\'=>'\\\\')) . '\'); ?>';
}
$_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.write_compiled_resource.php');
smarty_core_write_compiled_resource($_params, $smarty);
} else {
include($_compile_file);
}
if ($smarty->caching) {
$smarty->_cache_info['config'][$_file] = true;
}
$smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
$smarty->_config[0]['files'][$_file] = true;
switch($_scope) {
case 'parent':
$smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
$smarty->_config[1]['files'][$_file] = true;
break;
case 'global':
for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
$smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
$smarty->_config[$i]['files'][$_file] = true;
}
break;
case 'local':
break;
default:
$smarty->trigger_error(sprintf('Scope "%s" attribute in config_load tag is not correct', $_scope), E_USER_ERROR, __FILE__, __LINE__);
}
}
/* vim: set expandtab: */
?>
|
And a new function.config_load.php
Code: | <?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {config_load} function plugin
*
* Type: function<br>
* Name: config_load<br>
* Purpose: load config file vars
* @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
* (Smarty online manual)
* @param array Format:
* <pre>
* array('file' => required config file name,
* 'section' => optional config file section to load
* 'scope' => local/parent/global
* 'global' => overrides scope, setting to parent if true)
* </pre>
* @param Smarty
*/
function smarty_function_config_load($params, &$smarty)
{
if ($smarty->debugging) {
$_params = array();
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$_debug_start_time = smarty_core_get_microtime($_params, $smarty);
}
$_file = $params['file'];
$_section = $params['section'];
$_scope = $params['scope'];
$_global = $params['global'];
$_file_path = $params['file_path'];
$_compile_file = $params['compile_file'];
/*
$_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
$_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
$_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
$_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
if (!isset($_file) || strlen($_file) == 0) {
$smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
}
if (isset($_scope)) {
if ($_scope != 'local' &&
$_scope != 'parent' &&
$_scope != 'global') {
$smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
}
} else {
if ($_global) {
$_scope = 'parent';
} else {
$_scope = 'local';
}
}
$_params = array('resource_name' => $_file, 'resource_base_path' => $smarty->config_dir);
$smarty->_parse_resource_name($_params);
$_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
if (isset($_section)) {
$_file_path_full = $_file_path . '|' . $_section;
} else {
$_file_path_full = $_file_path;
}
$_compile_file = $smarty->_get_compile_path($_file_path_full);
*/
if($smarty->force_compile
|| !file_exists($_compile_file)
|| ($smarty->compile_check
&& !$smarty->_is_compiled($_file_path, $_compile_file))) {
// compile config file
if(!is_object($smarty->_conf_obj)) {
require_once SMARTY_DIR . $smarty->config_class . '.class.php';
$smarty->_conf_obj = &new $smarty->config_class();
$smarty->_conf_obj->overwrite = $smarty->config_overwrite;
$smarty->_conf_obj->booleanize = $smarty->config_booleanize;
$smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
$smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
}
$_params = array('resource_name' => $_file, 'resource_base_path' => $smarty->config_dir);
if (!$smarty->_fetch_resource_info($_params)) {
return;
}
$smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
$_config_vars = array_merge($smarty->_conf_obj->get($_file),
$smarty->_conf_obj->get($_file, $_section));
if(function_exists('var_export')) {
$_output = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; ?>';
} else {
$_output = '<?php $_config_vars = unserialize(\'' . strtr(serialize($_config_vars),array('\''=>'\\\'', '\\'=>'\\\\')) . '\'); ?>';
}
$_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.write_compiled_resource.php');
smarty_core_write_compiled_resource($_params, $smarty);
} else {
include($_compile_file);
}
if ($smarty->caching) {
$smarty->_cache_info['config'][$_file] = true;
}
$smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
$smarty->_config[0]['files'][$_file] = true;
switch($_scope) {
case 'parent':
$smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
$smarty->_config[1]['files'][$_file] = true;
break;
case 'global':
for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
$smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
$smarty->_config[$i]['files'][$_file] = true;
}
break;
}
if ($smarty->debugging) {
$_params = array();
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$smarty->_smarty_debug_info[] = array('type' => 'config',
'filename' => $_file.' ['.$_section.'] '.$_scope,
'depth' => $smarty->_inclusion_depth,
'exec_time' => smarty_core_get_microtime($_params, $smarty) - $_debug_start_time);
}
}
/* vim: set expandtab: */
?>
|
What are the advantages?
If we don't require compile_check, ... they are loaded just once.
We have to change also the Smarty_Compiler.class.php file to speed the process a little bit:
We have to change a method
Code: |
/**
* parse configuration variable expression into PHP code
*
* @param string $conf_var_expr
*/
function _parse_conf_var($conf_var_expr)
{
$parts = explode('|', $conf_var_expr, 2);
$var_ref = $parts[0];
$modifiers = isset($parts[1]) ? $parts[1] : '';
$var_name = substr($var_ref, 1, -1);
$result = $this->get_config_vars($var_name);
if ($smarty->force_compile || $smarty->debugging || is_null($result)) {
$output = "\$this->_config[0]['vars']['$var_name']";
$this->_parse_modifiers($output, $modifiers);
return $output;
}
$output = "'" . addslashes($result) ."'";
$this->_parse_modifiers($output, $modifiers);
return $output;
} |
To finish the subject, we have to change in the same file, in the method: _compile_smarty_ref
Code: |
case 'config':
array_shift($indexes);
$_val = $this->_parse_var_props(substr($indexes[0],1));
// $compiled_ref = "\$this->_config[0]['vars']";
$compiled_ref = $this->_parse_conf_var($_val);
$_max_index = 1;
break;
|
|
|