From 8b71e591c09dba8e691343672664c57117a6942c Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Sat, 3 Jan 2015 21:27:33 +0100 Subject: [PATCH] some minor speed optimization --- .../smarty_internal_resource_file.php | 54 +++++++++---------- libs/sysplugins/smarty_template_source.php | 6 +++ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index 743e19f0..66f1aca9 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -36,6 +36,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource $_directories = $source->smarty->getTemplateDir(); } preg_match('#^((?P[\/]|[a-zA-Z]:[\/])|(\[(?P[^\]]+)\])|((?P\.[\/])?(?P(\.\.[\/])*))|(?P[\/]))?(?P.+)$#', $file, $fileMatch); + // save basename + $source->basename = $fileMatch['file']; // go relative to a given template? if ($_template && $_template->parent instanceof Smarty_Internal_Template && (!empty($fileMatch['rel1']) || !empty($fileMatch['rel2']))) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { @@ -59,7 +61,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource $file = $path . '/' . $fileMatch['file']; } // files relative to a template only get one shot - return $this->fileExists($source, $file) ? $file : false; + return is_file($file) ? $file : false; } $_filepath = null; @@ -83,7 +85,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } if ($_directory) { $_filepath = $_directory . $fileMatch['file']; - if ($this->fileExists($source, $_filepath)) { + if (is_file($_filepath)) { return $_filepath; } } @@ -94,15 +96,17 @@ class Smarty_Internal_Resource_File extends Smarty_Resource foreach ($_directories as $_directory) { if (empty($fileMatch['rel2'])) { $_filepath = $_directory . $fileMatch['file']; - } else if (false === strpos($_directory, '..')) { - for ($i = 1; $i <= substr_count($fileMatch['rel2'], '../') + 1; $i ++) { - $_directory = substr($_directory, 0, strrpos($_directory, '/')); - } - $_filepath = $_directory . '/' . $fileMatch['file']; } else { - $_filepath = $_directory . $file; + if (false === strpos($_directory, '..')) { + for ($i = 1; $i <= substr_count($fileMatch['rel2'], '../') + 1; $i ++) { + $_directory = substr($_directory, 0, strrpos($_directory, '/')); + } + $_filepath = $_directory . '/' . $fileMatch['file']; + } else { + $_filepath = $_directory . $file; + } } - if ($this->fileExists($source, $_filepath)) { + if (is_file($_filepath)) { return $_filepath; } if ($source->smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_directory)) { @@ -114,7 +118,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } if ($_filepath !== false) { - if ($this->fileExists($source, $_filepath)) { + if (is_file($_filepath)) { return $_filepath; } } @@ -122,7 +126,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } } else { // try absolute filepath - if ($this->fileExists($source, $file)) { + if (is_file($file)) { return $file; } } @@ -136,13 +140,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } $file = $path . '/' . $fileMatch['file']; } - if ($this->fileExists($source, $file)) { - return $file; - } - - // give up - $source->timestamp = false; - return $source->exists = false; + return is_file($file) ? $file : false; } /** @@ -155,17 +153,15 @@ class Smarty_Internal_Resource_File extends Smarty_Resource */ protected function fileExists(Smarty_Template_Source $source, $file) { - if (is_file($file)) { - $source->timestamp = filemtime($file); - return $source->exists = true; - } - return false; + $source->timestamp = is_file($file) ? @filemtime($file) : false; + return $source->exists = !!$source->timestamp; } /** * populate Source Object with meta data from Resource + * - * @param Smarty_Template_Source $source source object +*@param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object */ public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) @@ -182,6 +178,9 @@ class Smarty_Internal_Resource_File extends Smarty_Resource $source->timestamp = @filemtime($source->filepath); $source->exists = !!$source->timestamp; } + } else { + $source->timestamp = false; + $source->exists = false; } } @@ -224,11 +223,6 @@ class Smarty_Internal_Resource_File extends Smarty_Resource */ public function getBasename(Smarty_Template_Source $source) { - $_file = $source->name; - if (($_pos = strpos($_file, ']')) !== false) { - $_file = substr($_file, $_pos + 1); - } - - return basename($_file); + return $source->basename; } } diff --git a/libs/sysplugins/smarty_template_source.php b/libs/sysplugins/smarty_template_source.php index d65eabec..edaa8e34 100644 --- a/libs/sysplugins/smarty_template_source.php +++ b/libs/sysplugins/smarty_template_source.php @@ -76,6 +76,12 @@ class Smarty_Template_Source * @var string */ public $filepath = null; + /** + * Source File Base name + * + * @var string + */ + public $basename = null; /** * The Components an extended template is made of