some minor speed optimization

This commit is contained in:
Uwe Tews
2015-01-03 21:27:33 +01:00
parent 57620443e9
commit 8b71e591c0
2 changed files with 30 additions and 30 deletions

View File

@@ -36,6 +36,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
$_directories = $source->smarty->getTemplateDir(); $_directories = $source->smarty->getTemplateDir();
} }
preg_match('#^((?P<absolute>[\/]|[a-zA-Z]:[\/])|(\[(?P<index>[^\]]+)\])|((?P<rel1>\.[\/])?(?P<rel2>(\.\.[\/])*))|(?P<skip>[\/]))?(?P<file>.+)$#', $file, $fileMatch); 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'];
// go relative to a given template? // go relative to a given template?
if ($_template && $_template->parent instanceof Smarty_Internal_Template && (!empty($fileMatch['rel1']) || !empty($fileMatch['rel2']))) { 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) { 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']; $file = $path . '/' . $fileMatch['file'];
} }
// files relative to a template only get one shot // files relative to a template only get one shot
return $this->fileExists($source, $file) ? $file : false; return is_file($file) ? $file : false;
} }
$_filepath = null; $_filepath = null;
@@ -83,7 +85,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
} }
if ($_directory) { if ($_directory) {
$_filepath = $_directory . $fileMatch['file']; $_filepath = $_directory . $fileMatch['file'];
if ($this->fileExists($source, $_filepath)) { if (is_file($_filepath)) {
return $_filepath; return $_filepath;
} }
} }
@@ -94,15 +96,17 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
foreach ($_directories as $_directory) { foreach ($_directories as $_directory) {
if (empty($fileMatch['rel2'])) { if (empty($fileMatch['rel2'])) {
$_filepath = $_directory . $fileMatch['file']; $_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 { } 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; return $_filepath;
} }
if ($source->smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_directory)) { 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 ($_filepath !== false) {
if ($this->fileExists($source, $_filepath)) { if (is_file($_filepath)) {
return $_filepath; return $_filepath;
} }
} }
@@ -122,7 +126,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
} }
} else { } else {
// try absolute filepath // try absolute filepath
if ($this->fileExists($source, $file)) { if (is_file($file)) {
return $file; return $file;
} }
} }
@@ -136,13 +140,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
} }
$file = $path . '/' . $fileMatch['file']; $file = $path . '/' . $fileMatch['file'];
} }
if ($this->fileExists($source, $file)) { return is_file($file) ? $file : false;
return $file;
}
// give up
$source->timestamp = false;
return $source->exists = false;
} }
/** /**
@@ -155,17 +153,15 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
*/ */
protected function fileExists(Smarty_Template_Source $source, $file) protected function fileExists(Smarty_Template_Source $source, $file)
{ {
if (is_file($file)) { $source->timestamp = is_file($file) ? @filemtime($file) : false;
$source->timestamp = filemtime($file); return $source->exists = !!$source->timestamp;
return $source->exists = true;
}
return false;
} }
/** /**
* populate Source Object with meta data from 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 * @param Smarty_Internal_Template $_template template object
*/ */
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) 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->timestamp = @filemtime($source->filepath);
$source->exists = !!$source->timestamp; $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) public function getBasename(Smarty_Template_Source $source)
{ {
$_file = $source->name; return $source->basename;
if (($_pos = strpos($_file, ']')) !== false) {
$_file = substr($_file, $_pos + 1);
}
return basename($_file);
} }
} }

View File

@@ -76,6 +76,12 @@ class Smarty_Template_Source
* @var string * @var string
*/ */
public $filepath = null; public $filepath = null;
/**
* Source File Base name
*
* @var string
*/
public $basename = null;
/** /**
* The Components an extended template is made of * The Components an extended template is made of