From b1fdc085e7401edf8c33ac2f2a7d7183e12cc764 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Mon, 6 Jul 2015 03:25:03 +0200 Subject: [PATCH] - optimization get rid of __get and __set in source object --- change_log.txt | 1 + demo/plugins/resource.extendsall.php | 10 +-- libs/Smarty.class.php | 2 +- .../smarty_internal_compile_foreach.php | 2 +- .../smarty_internal_config_file_compiler.php | 6 +- ...ernal_extension_defaulttemplatehandler.php | 5 +- .../smarty_internal_resource_extends.php | 12 ++-- .../smarty_internal_resource_file.php | 2 +- .../smarty_internal_resource_php.php | 2 +- .../smarty_internal_resource_registered.php | 5 +- libs/sysplugins/smarty_internal_template.php | 2 +- libs/sysplugins/smarty_resource.php | 1 - libs/sysplugins/smarty_template_compiled.php | 1 + libs/sysplugins/smarty_template_config.php | 2 +- libs/sysplugins/smarty_template_source.php | 65 +++++++------------ 15 files changed, 46 insertions(+), 72 deletions(-) diff --git a/change_log.txt b/change_log.txt index 2ac9a65e..a699e676 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,6 +1,7 @@  ===== 3.1.28-dev===== (xx.xx.2015) 06.07.2015 - optimize {block} compilation + - optimization get rid of __get and __set in source object 01.07.2015 - optimize compile check handling diff --git a/demo/plugins/resource.extendsall.php b/demo/plugins/resource.extendsall.php index 500b3c86..f9f3263c 100644 --- a/demo/plugins/resource.extendsall.php +++ b/demo/plugins/resource.extendsall.php @@ -23,7 +23,7 @@ class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends $uid = ''; $sources = array(); $exists = true; - foreach ($_template->smarty->getTemplateDir() as $key => $directory) { + foreach ($source->smarty->getTemplateDir() as $key => $directory) { try { $s = Smarty_Resource::source(null, $source->smarty, '[' . $key . ']' . $source->name); if (!$s->exists) { @@ -38,9 +38,7 @@ class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends if (!$sources) { $source->exists = false; - $source->template = $_template; - - return; + return; } $sources = array_reverse($sources, true); @@ -52,9 +50,7 @@ class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends $source->uid = sha1($uid); $source->exists = $exists; if ($_template && $_template->smarty->compile_check) { - $source->timestamp = $s->timestamp; + $source->timestamp = $s->getTimeStamp(); } - // need the template at getContent() - $source->template = $_template; } } diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index d5293133..6eff1249 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/21'; + const SMARTY_VERSION = '3.1.28-dev/22'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index 903db68e..ad34d11b 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -151,7 +151,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase $nextCompiler = $compiler; while ($nextCompiler !== $nextCompiler->parent_compiler) { $nextCompiler = $nextCompiler->parent_compiler; - preg_match_all($preg, $nextCompiler->template->source->content, $match, PREG_SET_ORDER); + preg_match_all($preg, $nextCompiler->template->source->getContent(), $match, PREG_SET_ORDER); foreach ($match as $m) { if (isset($m[3]) && !empty($m[3])) { $itemAttr[strtolower($m[3])] = true; diff --git a/libs/sysplugins/smarty_internal_config_file_compiler.php b/libs/sysplugins/smarty_internal_config_file_compiler.php index 397cac55..fbde96e1 100644 --- a/libs/sysplugins/smarty_internal_config_file_compiler.php +++ b/libs/sysplugins/smarty_internal_config_file_compiler.php @@ -101,15 +101,11 @@ class Smarty_Internal_Config_File_Compiler { $this->template = $template; $this->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->name, $this->template->source->getTimeStamp(), $this->template->source->type); - // on empty config just return - if ($template->source->content == '') { - return true; - } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this->template); } // init the lexer/parser to compile the config file - $lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->content) . "\n", $this); + $lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n", $this); $parser = new $this->parser_class($lex, $this); if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { diff --git a/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php b/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php index ed602920..1f58f04f 100644 --- a/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php +++ b/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php @@ -21,10 +21,9 @@ class Smarty_Internal_Extension_DefaultTemplateHandler * get default content from template of config resource handler * * @param Smarty_Internal_Template $_template - * @param Smarty_Internal_Template_Source $source - * @param Smarty_Resource $resObj + * @param Smarty_Template_Source $source */ - static function _getDefault(Smarty_Internal_Template $_template, &$source, &$resObj) + static function _getDefault(Smarty_Internal_Template $_template, $source) { if ($source->isConfig) { $default_handler = $_template->smarty->default_config_handler_func; diff --git a/libs/sysplugins/smarty_internal_resource_extends.php b/libs/sysplugins/smarty_internal_resource_extends.php index 4b3181a1..a7a1194f 100644 --- a/libs/sysplugins/smarty_internal_resource_extends.php +++ b/libs/sysplugins/smarty_internal_resource_extends.php @@ -39,13 +39,13 @@ class Smarty_Internal_Resource_Extends extends Smarty_Resource $components = explode('|', $source->name); $exists = true; foreach ($components as $component) { - $s = Smarty_Resource::source(null, $source->smarty, $component); + $s = Smarty_Template_Source::load(null, $source->smarty, $component); if ($s->type == 'php') { throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type"); } $sources[$s->uid] = $s; $uid .= $source->smarty->_realpath($s->filepath); - if ($_template && $_template->smarty->compile_check) { + if ($_template) { $exists = $exists && $s->exists; } } @@ -53,11 +53,9 @@ class Smarty_Internal_Resource_Extends extends Smarty_Resource $source->filepath = $s->filepath; $source->uid = sha1($uid); $source->exists = $exists; - if ($_template && $_template->smarty->compile_check) { + if ($_template) { $source->timestamp = $s->timestamp; } - // need the template at getContent() - $source->template = $_template; } /** @@ -71,7 +69,7 @@ class Smarty_Internal_Resource_Extends extends Smarty_Resource foreach ($source->components as $s) { $source->exists = $source->exists && $s->exists; } - $source->timestamp = $s->timestamp; + $source->timestamp = $source->exists ? $s->getTimeStamp() : false; } /** @@ -93,7 +91,7 @@ class Smarty_Internal_Resource_Extends extends Smarty_Resource $_content = ''; foreach ($_components as $_component) { // read content - $_content .= $_component->content; + $_content .= $_component->getContent(); } return $_content; } diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index e259a79d..94c70a66 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -161,7 +161,7 @@ class Smarty_Internal_Resource_File extends Smarty_Resource */ public function getContent(Smarty_Template_Source $source) { - if ($source->timestamp) { + if ($source->exists) { return file_get_contents($source->filepath); } if ($source instanceof Smarty_Template_Config) { diff --git a/libs/sysplugins/smarty_internal_resource_php.php b/libs/sysplugins/smarty_internal_resource_php.php index c836d1b2..09229c4f 100644 --- a/libs/sysplugins/smarty_internal_resource_php.php +++ b/libs/sysplugins/smarty_internal_resource_php.php @@ -43,7 +43,7 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File */ public function getContent(Smarty_Template_Source $source) { - if ($source->timestamp) { + if ($source->exists) { return ''; } throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); diff --git a/libs/sysplugins/smarty_internal_resource_registered.php b/libs/sysplugins/smarty_internal_resource_registered.php index f7175dd9..69b396b6 100644 --- a/libs/sysplugins/smarty_internal_resource_registered.php +++ b/libs/sysplugins/smarty_internal_resource_registered.php @@ -76,12 +76,13 @@ class Smarty_Internal_Resource_Registered extends Smarty_Resource public function getContent(Smarty_Template_Source $source) { // return template string - $t = call_user_func_array($source->smarty->registered_resources[$source->type][0][0], array($source->name, &$source->content, $source->smarty)); + $content = null; + $t = call_user_func_array($source->smarty->registered_resources[$source->type][0][0], array($source->name, &$content, $source->smarty)); if (is_bool($t) && !$t) { throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); } - return $source->content; + return $content; } /** diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 359d5907..1ee3fc0e 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -613,7 +613,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase } elseif ($_file_to_check[2] == 'string') { continue; } else { - $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]); + $source = Smarty_Template_Source::load(null, $this->smarty, $_file_to_check[0]); $mtime = $source->getTimeStamp(); } if (!$mtime || $mtime > $_file_to_check[1]) { diff --git a/libs/sysplugins/smarty_resource.php b/libs/sysplugins/smarty_resource.php index 5d5e368d..bdb1bd85 100644 --- a/libs/sysplugins/smarty_resource.php +++ b/libs/sysplugins/smarty_resource.php @@ -84,7 +84,6 @@ abstract class Smarty_Resource /** * Load template's source into current template object - * {@internal The loaded source is assigned to $_template->source->content directly.}} * * @param Smarty_Template_Source $source source object * diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index 29dd6121..0103fed8 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -225,6 +225,7 @@ class Smarty_Template_Compiled } // call compiler try { + $_template->loadCompiler(); $code = $_template->compiler->compileTemplate($_template); } catch (Exception $e) { diff --git a/libs/sysplugins/smarty_template_config.php b/libs/sysplugins/smarty_template_config.php index f48d4711..a8851692 100644 --- a/libs/sysplugins/smarty_template_config.php +++ b/libs/sysplugins/smarty_template_config.php @@ -110,7 +110,7 @@ class Smarty_Template_Config extends Smarty_Template_Source $source = new Smarty_Template_Config($resource, $smarty, $template_resource, $type, $name); $resource->populate($source, $_template); if ((!isset($source->exists) || !$source->exists) && isset($_template->smarty->default_config_handler_func)) { - Smarty_Internal_Extension_DefaultTemplateHandler::_getDefault($_template, $source, $resource); + Smarty_Internal_Extension_DefaultTemplateHandler::_getDefault($_template, $source); } $source->unique_resource = $resource->buildUniqueResourceName($smarty, $name, true); return $source; diff --git a/libs/sysplugins/smarty_template_source.php b/libs/sysplugins/smarty_template_source.php index f6ae8842..184fecff 100644 --- a/libs/sysplugins/smarty_template_source.php +++ b/libs/sysplugins/smarty_template_source.php @@ -76,6 +76,7 @@ class Smarty_Template_Source * @var string */ public $filepath = null; + /** * Source Timestamp * @@ -89,6 +90,7 @@ class Smarty_Template_Source * @var boolean */ public $exists = false; + /** * Source File Base name * @@ -116,12 +118,14 @@ class Smarty_Template_Source * @var Smarty */ public $smarty = null; + /** * Resource is source * * @var bool */ public $isConfig = false; + /** * Source is bypassing compiler * @@ -135,6 +139,7 @@ class Smarty_Template_Source * @var boolean */ public $recompiled = false; + /** * cache for Smarty_Template_Compiled instances * @@ -142,6 +147,13 @@ class Smarty_Template_Source */ public $compileds = array(); + /** + * Template source content eventually set by default handler + * + * @var string + */ + public $content = null; + /** * create Source Object container * @@ -205,15 +217,14 @@ class Smarty_Template_Source $source = new Smarty_Template_Source($resource, $smarty, $template_resource, $type, $name); $resource->populate($source, $_template); if ((!isset($source->exists) || !$source->exists) && isset($_template->smarty->default_template_handler_func)) { - Smarty_Internal_Extension_DefaultTemplateHandler::_getDefault($_template, $source, $resObj); + Smarty_Internal_Extension_DefaultTemplateHandler::_getDefault($_template, $source); } // on recompiling resources we are done if ($smarty->resource_caching && !$resource->recompiled) { // may by we have already $unique_resource $is_relative = false; if (!isset($unique_resource)) { - $is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') && - ($type == 'file' || (isset($_template->parent->source) && $_template->parent->source->type == 'extends')); + $is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') && ($type == 'file' || (isset($_template->parent->source) && $_template->parent->source->type == 'extends')); $unique_resource = $resource->buildUniqueResourceName($smarty, $is_relative ? $source->filepath . $name : $name); } $source->unique_resource = $unique_resource; @@ -229,6 +240,9 @@ class Smarty_Template_Source * render the uncompiled source * * @param Smarty_Internal_Template $_template template object + * + * @return string + * @throws \Exception */ public function renderUncompiled(Smarty_Internal_Template $_template) { @@ -251,52 +265,21 @@ class Smarty_Template_Source * * @return int */ - public function getTimeStamp() { + public function getTimeStamp() + { if (!isset($this->timestamp)) { $this->handler->populateTimestamp($this); - } + } return $this->timestamp; } /** - * <> Generic Setter. + * Get source content * - * @param string $property_name valid: timestamp, exists, content, template - * @param mixed $value new value (is not checked) - * - * @throws SmartyException if $property_name is not valid + * @return string */ - public function __set($property_name, $value) + public function getContent() { - switch ($property_name) { - // regular attributes - case 'content': - // required for extends: only - case 'template': - $this->$property_name = $value; - break; - - default: - throw new SmartyException("source property '$property_name' does not exist."); - } - } - - /** - * <> Generic getter. - * - * @param string $property_name valid: timestamp, exists, content - * - * @return mixed - * @throws SmartyException if $property_name is not valid - */ - public function __get($property_name) - { - switch ($property_name) { - case 'content': - return $this->content = $this->handler->getContent($this); - - default: - throw new SmartyException("source property '$property_name' does not exist."); - } + return isset($this->content) ? $this->content : $this->handler->getContent($this); } }