- optimization replace internal Smarty::$ds property by DIRECTORY_SEPARATOR

This commit is contained in:
Uwe Tews
2017-10-26 06:14:22 +02:00
parent 4575a31e62
commit 570bc41b12
4 changed files with 32 additions and 28 deletions

View File

@@ -7,7 +7,6 @@
* @author Uwe Tews * @author Uwe Tews
* @author Rodney Rehm * @author Rodney Rehm
*/ */
/** /**
* This class does contain all necessary methods for the HTML cache on file system * This class does contain all necessary methods for the HTML cache on file system
* Implements the file system as resource for the HTML cache Version ussing nocache inserts. * Implements the file system as resource for the HTML cache Version ussing nocache inserts.
@@ -29,12 +28,13 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
{ {
$source = &$_template->source; $source = &$_template->source;
$smarty = &$_template->smarty; $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); $_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir(); $cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) { if (isset($_template->cache_id)) {
$cached->filepath .= preg_replace(array('![^\w|]+!', $cached->filepath .= preg_replace(array('![^\w|]+!',
'![|]+!'), array('_', '![|]+!'),
array('_',
$_compile_dir_sep), $_compile_dir_sep),
$_template->cache_id) . $_compile_dir_sep; $_template->cache_id) . $_compile_dir_sep;
} }
@@ -43,9 +43,10 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
} }
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) { if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] . $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] .
$smarty->ds . $_filepath[ 3 ] .
$_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds; DIRECTORY_SEPARATOR .
$_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR;
} }
$cached->filepath .= $_filepath; $cached->filepath .= $_filepath;
$basename = $source->handler->getBasename($source); $basename = $source->handler->getBasename($source);
@@ -86,7 +87,8 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
* *
* @return boolean true or false if the cached content does not exist * @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) $update = false)
{ {
$_smarty_tpl->cached->valid = false; $_smarty_tpl->cached->valid = false;
@@ -108,14 +110,15 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
*/ */
public function writeCachedContent(Smarty_Internal_Template $_template, $content) 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 $_template->smarty) === true
) { ) {
if (function_exists('opcache_invalidate') && if (function_exists('opcache_invalidate') &&
(!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1 (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1
) { ) {
opcache_invalidate($_template->cached->filepath, true); 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); apc_compile_file($_template->cached->filepath);
} }
$cached = $_template->cached; $cached = $_template->cached;

View File

@@ -7,7 +7,6 @@
* @subpackage Compiler * @subpackage Compiler
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Plugin Compile Include Class * Smarty Internal Plugin Compile Include Class
* *

View File

@@ -1,5 +1,4 @@
<?php <?php
/** /**
* Smarty Method CompileAllTemplates * Smarty Method CompileAllTemplates
* *
@@ -31,7 +30,10 @@ class Smarty_Internal_Method_CompileAllTemplates
* *
* @return integer number of template files recompiled * @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) $max_errors = null)
{ {
return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors); return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
@@ -49,7 +51,11 @@ class Smarty_Internal_Method_CompileAllTemplates
* *
* @return int number of template files compiled * @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) $isConfig = false)
{ {
// switch off time limit // switch off time limit
@@ -69,11 +75,11 @@ class Smarty_Internal_Method_CompileAllTemplates
if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
continue; continue;
} }
if (!substr_compare($_file, $extension, - strlen($extension)) == 0) { if (!substr_compare($_file, $extension, -strlen($extension)) == 0) {
continue; continue;
} }
if ($_fileinfo->getPath() !== substr($_dir, 0, - 1)) { if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . $smarty->ds . $_file; $_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file;
} }
echo "\n<br>", $_dir, '---', $_file; echo "\n<br>", $_dir, '---', $_file;
flush(); flush();
@@ -92,7 +98,7 @@ class Smarty_Internal_Method_CompileAllTemplates
$isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl); $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
if ($_tpl->mustCompile()) { if ($_tpl->mustCompile()) {
$_tpl->compileTemplateSource(); $_tpl->compileTemplateSource();
$_count ++; $_count++;
echo ' compiled in ', microtime(true) - $_start_time, ' seconds'; echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
flush(); flush();
} else { } else {
@@ -102,7 +108,7 @@ class Smarty_Internal_Method_CompileAllTemplates
} }
catch (Exception $e) { catch (Exception $e) {
echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n"; echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
$_error_count ++; $_error_count++;
} }
// free memory // free memory
unset($_tpl); unset($_tpl);

View File

@@ -1,5 +1,4 @@
<?php <?php
/** /**
* Smarty cache resource file clear method * Smarty cache resource file clear method
* *
@@ -7,7 +6,6 @@
* @subpackage PluginsInternal * @subpackage PluginsInternal
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Runtime Cache Resource File Class * Smarty Internal Runtime Cache Resource File Class
* *
@@ -43,7 +41,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
$_cache_id_parts_count = count($_cache_id_parts); $_cache_id_parts_count = count($_cache_id_parts);
if ($smarty->use_sub_dirs) { if ($smarty->use_sub_dirs) {
foreach ($_cache_id_parts as $id_part) { foreach ($_cache_id_parts as $id_part) {
$_dir .= $id_part . $smarty->ds; $_dir .= $id_part . DIRECTORY_SEPARATOR;
} }
} }
} }
@@ -52,10 +50,8 @@ class Smarty_Internal_Runtime_CacheResourceFile
$smarty->caching = true; $smarty->caching = true;
$tpl = new $smarty->template_class($resource_name, $smarty); $tpl = new $smarty->template_class($resource_name, $smarty);
$smarty->caching = $_save_stat; $smarty->caching = $_save_stat;
// remove from template cache // remove from template cache
$tpl->source; // have the template registered before unset() $tpl->source; // have the template registered before unset()
if ($tpl->source->exists) { if ($tpl->source->exists) {
$_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
} else { } else {
@@ -71,7 +67,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
if (substr(basename($_file->getPathname()), 0, 1) == '.') { if (substr(basename($_file->getPathname()), 0, 1) == '.') {
continue; continue;
} }
$_filepath = (string) $_file; $_filepath = (string)$_file;
// directory ? // directory ?
if ($_file->isDir()) { if ($_file->isDir()) {
if (!$_cache->isDot()) { if (!$_cache->isDot()) {
@@ -80,7 +76,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
} }
} else { } else {
// delete only php files // delete only php files
if (substr($_filepath, - 4) !== '.php') { if (substr($_filepath, -4) !== '.php') {
continue; continue;
} }
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length))); $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
@@ -105,7 +101,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
if ($_parts_count < $_cache_id_parts_count) { if ($_parts_count < $_cache_id_parts_count) {
continue; continue;
} }
for ($i = 0; $i < $_cache_id_parts_count; $i ++) { for ($i = 0; $i < $_cache_id_parts_count; $i++) {
if ($_parts[ $i ] != $_cache_id_parts[ $i ]) { if ($_parts[ $i ] != $_cache_id_parts[ $i ]) {
continue 2; continue 2;
} }
@@ -130,7 +126,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1) && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) { ) {
opcache_invalidate($_filepath, true); opcache_invalidate($_filepath, true);
} elseif (function_exists('apc_delete_file')) { } else if (function_exists('apc_delete_file')) {
apc_delete_file($_filepath); apc_delete_file($_filepath);
} }
} }