Small optimization

This commit is contained in:
Uwe Tews
2015-01-04 05:51:03 +01:00
parent 068da64c99
commit d528234a97

View File

@@ -37,7 +37,9 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
}
preg_match('#^((?P<absolute>[\/]|[a-zA-Z]:[\/])|(\[(?P<index>[^\]]+)\])|((?P<rel1>\.[\/])?(?P<rel2>(\.\.[\/])*))|(?P<skip>[\/]))?(?P<file>.+)$#', $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);
}
}