Skip to content

Commit

Permalink
- optimization replace internal Smarty::$ds property by DIRECTORY_SEP…
Browse files Browse the repository at this point in the history
…ARATOR
  • Loading branch information
uwetews committed Oct 22, 2017
1 parent 5ec2e1f commit f7a5316
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 277 deletions.
4 changes: 4 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
===== 3.1.32 - dev ===
22.10.2017 3.1.32-dev-28
- bugfix Smarty version was not filled in header comment of compiled and cached files
- optimization replace internal Smarty::$ds property by DIRECTORY_SEPARATOR

21.10.2017
- bugfix custom delimiters could fail since modification of version 3.1.32-dev-23
https://github.com/smarty-php/smarty/issues/394
Expand Down
28 changes: 11 additions & 17 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.32-dev-27';
const SMARTY_VERSION = '3.1.32-dev-28';
/**
* define variable scopes
*/
Expand Down Expand Up @@ -544,12 +544,6 @@ class Smarty extends Smarty_Internal_TemplateBase
* @var Smarty_Internal_Debug
*/
public $_debug = null;
/**
* Directory separator
*
* @var string
*/
public $ds = DIRECTORY_SEPARATOR;
/**
* template directory
*
Expand Down Expand Up @@ -942,7 +936,7 @@ public function getPluginsDir()
$this->plugins_dir = (array)$this->plugins_dir;
}
foreach ($this->plugins_dir as $k => $v) {
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . DIRECTORY_SEPARATOR, true);
}
$this->_cache[ 'plugin_files' ] = array();
$this->_pluginsDirNormalized = true;
Expand Down Expand Up @@ -1133,9 +1127,9 @@ public function _getTemplateId($template_name,
*/
public function _realpath($path, $realpath = null)
{
$nds = $this->ds == '/' ? '\\' : '/';
// normalize $this->ds
$path = str_replace($nds, $this->ds, $path);
$nds = DIRECTORY_SEPARATOR === '/' ? '\\' : '/';
// normalize DIRECTORY_SEPARATOR
$path = str_replace($nds, DIRECTORY_SEPARATOR, $path);
preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(.*))$%u',
$path,
$parts);
Expand All @@ -1144,13 +1138,13 @@ public function _realpath($path, $realpath = null)
$parts[ 'root' ] = substr(getcwd(), 0, 2) . $parts[ 'root' ];
} else {
if ($realpath !== null && !$parts[ 'root' ]) {
$path = getcwd() . $this->ds . $path;
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
}
}
// remove noop 'DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR' and 'DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR' patterns
$path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#u', $this->ds, $path);
$path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#u', DIRECTORY_SEPARATOR, $path);
// resolve '..DIRECTORY_SEPARATOR' pattern, smallest first
if (strpos($path, '..' . $this->ds) != false &&
if (strpos($path, '..' . DIRECTORY_SEPARATOR) != false &&
preg_match_all('#(([.]?[\\\\/])*([.][.])[\\\\/]([.]?[\\\\/])*)+#u', $path, $match)
) {
$counts = array();
Expand All @@ -1161,7 +1155,7 @@ public function _realpath($path, $realpath = null)
foreach ($counts as $count) {
$path = preg_replace('#(([\\\\/]([.]?[\\\\/])*[^\\\\/.]+){' . $count .
'}[\\\\/]([.]?[\\\\/])*([.][.][\\\\/]([.]?[\\\\/])*){' . $count . '})(?=[^.])#u',
$this->ds,
DIRECTORY_SEPARATOR,
$path);
}
}
Expand Down Expand Up @@ -1407,7 +1401,7 @@ public function __set($name, $value)
*/
private function _normalizeDir($dirName, $dir)
{
$this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . $this->ds, true);
$this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . DIRECTORY_SEPARATOR, true);
if (!isset(Smarty::$_muted_directories[ $this->{$dirName} ])) {
Smarty::$_muted_directories[ $this->{$dirName} ] = null;
}
Expand All @@ -1433,7 +1427,7 @@ private function _normalizeTemplateConfig($isConfig)
}
foreach ($dir as $k => $v) {
if (!isset($processed[ $k ])) {
$dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
$dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . DIRECTORY_SEPARATOR, true);
$processed[ $k ] = true;
}
}
Expand Down
22 changes: 13 additions & 9 deletions libs/sysplugins/smarty_internal_cacheresource_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Templat
{
$source = &$_template->source;
$smarty = &$_template->smarty;
$_compile_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^';
$_compile_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
$_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) {
$cached->filepath .= preg_replace(array('![^\w|]+!',
'![|]+!'), array('_',
$_compile_dir_sep),
'![|]+!'),
array('_',
$_compile_dir_sep),
$_template->cache_id) . $_compile_dir_sep;
}
if (isset($_template->compile_id)) {
$cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
}
// if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] .
$smarty->ds .
$_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] .
$_filepath[ 3 ] .
DIRECTORY_SEPARATOR .
$_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR;
}
$cached->filepath .= $_filepath;
$basename = $source->handler->getBasename($source);
Expand Down Expand Up @@ -86,7 +88,8 @@ public function populateTimestamp(Smarty_Template_Cached $cached)
*
* @return boolean true or false if the cached content does not exist
*/
public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null,
public function process(Smarty_Internal_Template $_smarty_tpl,
Smarty_Template_Cached $cached = null,
$update = false)
{
$_smarty_tpl->cached->valid = false;
Expand All @@ -108,14 +111,15 @@ public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_C
*/
public function writeCachedContent(Smarty_Internal_Template $_template, $content)
{
if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath,
$content,
$_template->smarty) === true
) {
if (function_exists('opcache_invalidate') &&
(!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1
) {
opcache_invalidate($_template->cached->filepath, true);
} elseif (function_exists('apc_compile_file')) {
} else if (function_exists('apc_compile_file')) {
apc_compile_file($_template->cached->filepath);
}
$cached = $_template->cached;
Expand Down
8 changes: 2 additions & 6 deletions libs/sysplugins/smarty_internal_compile_include_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
}
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);

/** @var Smarty_Internal_Template $_smarty_tpl
* used in evaluated code
*/
Expand All @@ -74,8 +73,8 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$_dir = $compiler->smarty->trusted_dir;
}
if (!empty($_dir)) {
foreach ((array) $_dir as $_script_dir) {
$_path = $compiler->smarty->_realpath($_script_dir . $compiler->smarty->ds . $_file, true);
foreach ((array)$_dir as $_script_dir) {
$_path = $compiler->smarty->_realpath($_script_dir . DIRECTORY_SEPARATOR . $_file, true);
if (file_exists($_path)) {
$_filepath = $_path;
break;
Expand All @@ -86,11 +85,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
if ($_filepath == false) {
$compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", null, true);
}

if (isset($compiler->smarty->security_policy)) {
$compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
}

if (isset($_attr[ 'assign' ])) {
// output will be stored in a smarty variable instead of being displayed
$_assign = $_attr[ 'assign' ];
Expand All @@ -101,7 +98,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$_once = '';
}
}

if (isset($_assign)) {
return "<?php ob_start();\ninclude{$_once} ('{$_filepath}');\n\$_smarty_tpl->assign({$_assign},ob_get_clean());\n?>";
} else {
Expand Down
14 changes: 6 additions & 8 deletions libs/sysplugins/smarty_internal_compile_insert.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Smarty Internal Plugin Compile Insert
* Compiles the {insert} tag
Expand Down Expand Up @@ -56,7 +55,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$_attr = $this->getAttributes($compiler, $args);
//Does tag create output
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;

$nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
if (!$nocacheParam) {
// do not compile as nocache code
Expand All @@ -66,7 +64,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$_smarty_tpl = $compiler->template;
$_name = null;
$_script = null;

$_output = '<?php ';
// save possible attributes
eval('$_name = @' . $_attr[ 'name' ] . ';');
Expand Down Expand Up @@ -96,8 +93,8 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null;
}
if (!empty($_dir)) {
foreach ((array) $_dir as $_script_dir) {
$_script_dir = rtrim($_script_dir, '/\\') . $compiler->smarty->ds;
foreach ((array)$_dir as $_script_dir) {
$_script_dir = rtrim($_script_dir, '/\\') . DIRECTORY_SEPARATOR;
if (file_exists($_script_dir . $_script)) {
$_filepath = $_script_dir . $_script;
break;
Expand All @@ -113,7 +110,8 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
require_once $_filepath;
if (!is_callable($_function)) {
$compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'",
null, true);
null,
true);
}
} else {
$_filepath = 'null';
Expand All @@ -122,7 +120,8 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
if (!is_callable($_function)) {
// try plugin
if (!$_function = $compiler->getPlugin($_name, 'insert')) {
$compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", null,
$compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'",
null,
true);
}
}
Expand All @@ -149,7 +148,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
}
}

return $_output;
}
}
21 changes: 14 additions & 7 deletions libs/sysplugins/smarty_internal_method_compilealltemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class Smarty_Internal_Method_CompileAllTemplates
*
* @return integer number of template files recompiled
*/
public function compileAllTemplates(Smarty $smarty, $extension = '.tpl', $force_compile = false, $time_limit = 0,
public function compileAllTemplates(Smarty $smarty,
$extension = '.tpl',
$force_compile = false,
$time_limit = 0,
$max_errors = null)
{
return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
Expand All @@ -49,7 +52,11 @@ public function compileAllTemplates(Smarty $smarty, $extension = '.tpl', $force_
*
* @return int number of template files compiled
*/
protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_limit, $max_errors,
protected function compileAll(Smarty $smarty,
$extension,
$force_compile,
$time_limit,
$max_errors,
$isConfig = false)
{
// switch off time limit
Expand All @@ -69,11 +76,11 @@ protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_
if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
continue;
}
if (!substr_compare($_file, $extension, - strlen($extension)) == 0) {
if (!substr_compare($_file, $extension, -strlen($extension)) == 0) {
continue;
}
if ($_fileinfo->getPath() !== substr($_dir, 0, - 1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . $smarty->ds . $_file;
if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file;
}
echo "\n<br>", $_dir, '---', $_file;
flush();
Expand All @@ -92,7 +99,7 @@ protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_
$isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
if ($_tpl->mustCompile()) {
$_tpl->compileTemplateSource();
$_count ++;
$_count++;
echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
flush();
} else {
Expand All @@ -102,7 +109,7 @@ protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_
}
catch (Exception $e) {
echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
$_error_count ++;
$_error_count++;
}
// free memory
unset($_tpl);
Expand Down
Loading

0 comments on commit f7a5316

Please sign in to comment.