From d528234a977053db88eeb33bc70af9c410152f95 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Sun, 4 Jan 2015 05:51:03 +0100 Subject: [PATCH] Small optimization --- .../smarty_internal_resource_file.php | 68 +++++++++---------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index 66f1aca9..a97ed46b 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -37,7 +37,9 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } preg_match('#^((?P[\/]|[a-zA-Z]:[\/])|(\[(?P[^\]]+)\])|((?P\.[\/])?(?P(\.\.[\/])*))|(?P[\/]))?(?P.+)$#', $file, $fileMatch); // save basename - $source->basename = $fileMatch['file']; + if (!empty($fileMatch['absolute'])) { + return is_file($file) ? $file : false; + } // 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) { @@ -92,42 +94,35 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } // relative file name? - if (empty($fileMatch['absolute'])) { - foreach ($_directories as $_directory) { - if (empty($fileMatch['rel2'])) { - $_filepath = $_directory . $fileMatch['file']; + 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 { - 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 (is_file($_filepath)) { - return $_filepath; - } - if ($source->smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_directory)) { - // try PHP include_path - if (function_exists('stream_resolve_include_path')) { - $_filepath = stream_resolve_include_path($_filepath); - } else { - $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath); - } - - if ($_filepath !== false) { - if (is_file($_filepath)) { - return $_filepath; - } - } + $_filepath = $_directory . $file; } } - } else { - // try absolute filepath - if (is_file($file)) { - return $file; + if (is_file($_filepath)) { + return $_filepath; + } + if ($source->smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_directory)) { + // try PHP include_path + if (function_exists('stream_resolve_include_path')) { + $_filepath = stream_resolve_include_path($_filepath); + } else { + $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath); + } + + if ($_filepath !== false) { + if (is_file($_filepath)) { + return $_filepath; + } + } } } // Could be relative to cwd @@ -159,9 +154,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource /** * 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) @@ -223,6 +217,6 @@ class Smarty_Internal_Resource_File extends Smarty_Resource */ public function getBasename(Smarty_Template_Source $source) { - return $source->basename; + return basename($source->filepath); } }