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 26, 2017
1 parent 4575a31 commit 570bc41
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
23 changes: 13 additions & 10 deletions libs/sysplugins/smarty_internal_cacheresource_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @author Uwe Tews
* @author Rodney Rehm
*/

/**
* 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.
Expand All @@ -29,23 +28,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 +87,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 +110,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
1 change: 0 additions & 1 deletion libs/sysplugins/smarty_internal_compile_include.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @subpackage Compiler
* @author Uwe Tews
*/

/**
* Smarty Internal Plugin Compile Include Class
*
Expand Down
22 changes: 14 additions & 8 deletions libs/sysplugins/smarty_internal_method_compilealltemplates.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Smarty Method CompileAllTemplates
*
Expand Down Expand Up @@ -31,7 +30,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 +51,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 +75,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 +98,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 +108,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
14 changes: 5 additions & 9 deletions libs/sysplugins/smarty_internal_runtime_cacheresourcefile.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php

/**
* Smarty cache resource file clear method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/

/**
* Smarty Internal Runtime Cache Resource File Class
*
Expand Down Expand Up @@ -43,7 +41,7 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
$_cache_id_parts_count = count($_cache_id_parts);
if ($smarty->use_sub_dirs) {
foreach ($_cache_id_parts as $id_part) {
$_dir .= $id_part . $smarty->ds;
$_dir .= $id_part . DIRECTORY_SEPARATOR;
}
}
}
Expand All @@ -52,10 +50,8 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
$smarty->caching = true;
$tpl = new $smarty->template_class($resource_name, $smarty);
$smarty->caching = $_save_stat;

// remove from template cache
$tpl->source; // have the template registered before unset()

if ($tpl->source->exists) {
$_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
} else {
Expand All @@ -71,7 +67,7 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
if (substr(basename($_file->getPathname()), 0, 1) == '.') {
continue;
}
$_filepath = (string) $_file;
$_filepath = (string)$_file;
// directory ?
if ($_file->isDir()) {
if (!$_cache->isDot()) {
Expand All @@ -80,7 +76,7 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
}
} else {
// delete only php files
if (substr($_filepath, - 4) !== '.php') {
if (substr($_filepath, -4) !== '.php') {
continue;
}
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
Expand All @@ -105,7 +101,7 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
if ($_parts_count < $_cache_id_parts_count) {
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 ]) {
continue 2;
}
Expand All @@ -130,7 +126,7 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($_filepath, true);
} elseif (function_exists('apc_delete_file')) {
} else if (function_exists('apc_delete_file')) {
apc_delete_file($_filepath);
}
}
Expand Down

0 comments on commit 570bc41

Please sign in to comment.