- optimize use_include_path processing

This commit is contained in:
uwetews
2015-08-23 11:13:20 +02:00
parent 2c197a2ea6
commit 606b8e8669
2 changed files with 36 additions and 44 deletions

View File

@@ -11,6 +11,7 @@
- move caching to Smarty::_cache - move caching to Smarty::_cache
- remove properties with redundant content - remove properties with redundant content
- optimize Smarty::templateExists() - optimize Smarty::templateExists()
- optimize use_include_path processing
06.08.2015 06.08.2015
- avoid possible circular object references caused by parser/lexer objects - avoid possible circular object references caused by parser/lexer objects

View File

@@ -39,40 +39,33 @@ class Smarty_Internal_Get_Include_Path
/** /**
* stream cache * stream cache
* *
* @var array * @var string[]
*/
static $stream = array();
/**
* stream cache
*
* @var array
*/ */
static $isFile = array(); static $isFile = array();
/** /**
* stream cache * stream cache
* *
* @var array * @var string[]
*/ */
static $isPath = array(); static $isPath = array();
/** /**
* stream cache * stream cache
* *
* @var array * @var int[]
*/ */
static $number = array(); static $number = array();
/** /**
* status cache * status cache
* *
* @var null * @var bool
*/ */
static $_has_stream_include = null; static $_has_stream_include = null;
/** /**
* Numger for array index * Number for array index
* *
* @var int * @var int
*/ */
@@ -81,15 +74,12 @@ class Smarty_Internal_Get_Include_Path
/** /**
* Check if include path was updated * Check if include path was updated
* *
* @return bool * @param \Smarty $smarty
* *
* @return bool
*/ */
public static function isNewIncludePath(Smarty $smarty) public static function isNewIncludePath(Smarty $smarty)
{ {
if (!isset(self::$_has_stream_include)) {
self::$_has_stream_include = ($smarty->use_include_path === 2) &&
function_exists('stream_resolve_include_path');
}
$_i_path = get_include_path(); $_i_path = get_include_path();
if (self::$_include_path != $_i_path) { if (self::$_include_path != $_i_path) {
self::$_include_dirs = array(); self::$_include_dirs = array();
@@ -111,6 +101,8 @@ class Smarty_Internal_Get_Include_Path
/** /**
* return array with include path directories * return array with include path directories
* *
* @param \Smarty $smarty
*
* @return array * @return array
*/ */
public static function getIncludePathDirs(Smarty $smarty) public static function getIncludePathDirs(Smarty $smarty)
@@ -131,7 +123,9 @@ class Smarty_Internal_Get_Include_Path
*/ */
public static function getIncludePath($dirs, $file, Smarty $smarty) public static function getIncludePath($dirs, $file, Smarty $smarty)
{ {
self::isNewIncludePath($smarty); if (!(isset(self::$_has_stream_include) ? self::$_has_stream_include : self::$_has_stream_include = function_exists('stream_resolve_include_path'))) {
self::isNewIncludePath($smarty);
}
// try PHP include_path // try PHP include_path
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
$dir_n = isset(self::$number[$dir]) ? self::$number[$dir] : self::$number[$dir] = self::$counter ++; $dir_n = isset(self::$number[$dir]) ? self::$number[$dir] : self::$number[$dir] = self::$counter ++;
@@ -146,43 +140,40 @@ class Smarty_Internal_Get_Include_Path
if (false === self::$_user_dirs[$dir_n]) { if (false === self::$_user_dirs[$dir_n]) {
continue; continue;
} else { } else {
$_u_dir = self::$_user_dirs[$dir_n]; $dir = self::$_user_dirs[$dir_n];
} }
} else { } else {
if ($dir[0] == '/' || $dir[1] == ':') { if ($dir[0] == '/' || $dir[1] == ':') {
$_u_dir = str_ireplace(getcwd(), '.', $dir); $dir = str_ireplace(getcwd(), '.', $dir);
if ($_u_dir[0] == '/' || $_u_dir[1] == ':') { if ($dir[0] == '/' || $dir[1] == ':') {
self::$_user_dirs[$dir_n] = false; self::$_user_dirs[$dir_n] = false;
continue; continue;
} }
self::$_user_dirs[$dir_n] = $_u_dir;
} else {
$_u_dir = self::$_user_dirs[$dir_n] = $dir;
} }
$dir = substr($dir, 2);
self::$_user_dirs[$dir_n] = $dir;
} }
if (self::$_has_stream_include) { if (self::$_has_stream_include) {
// available since PHP 5.3.2 $path = stream_resolve_include_path($dir . (isset($file) ? $file : ''));
$_d_path = $_u_dir . (isset($file) ? $file : ''); if ($path) {
self::$stream[$_d_path] = isset(self::$stream[$_d_path]) ? self::$stream[$_d_path] : ($path = stream_resolve_include_path($_d_path)) ? is_file($path) : false; return self::$isFile[$dir_n][$file] = $path;
if (self::$stream[$_d_path]) {
return self::$isFile[$dir_n][$file] = self::$stream[$_d_path];
} }
} } else {
foreach (self::$_include_dirs as $key => $_i_path) { foreach (self::$_include_dirs as $key => $_i_path) {
$path = self::$isPath[$key][$dir_n] = isset(self::$isPath[$key][$dir_n]) ? self::$isPath[$key][$dir_n] : is_dir($_i_path . $path = isset(self::$isPath[$key][$dir_n]) ? self::$isPath[$key][$dir_n] : self::$isPath[$key][$dir_n] = is_dir($_dir_path = $_i_path .
$_u_dir) ? $_i_path . $dir) ? $_dir_path : false;
substr($_u_dir, 2) : false; if ($path === false) {
if ($path === false) { continue;
continue; }
} if (isset($file)) {
if (isset($file)) { $_file = self::$isFile[$dir_n][$file] = (is_file($path . $file)) ? $path . $file : false;
$_file = self::$isFile[$dir_n][$file] = ($path && is_file($path . $file)) ? $path . $file : false; if ($_file) {
if ($_file) { return $_file;
return $_file; }
} else {
// no file was given return directory path
return $path;
} }
} else {
// no file was given return directory path
return $path;
} }
} }
} }