- optimization get rid of __get and __set in source object

This commit is contained in:
Uwe Tews
2015-07-06 03:25:03 +02:00
parent b198e6488c
commit b1fdc085e7
15 changed files with 46 additions and 72 deletions

View File

@@ -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

View File

@@ -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,8 +38,6 @@ class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends
if (!$sources) {
$source->exists = false;
$source->template = $_template;
return;
}
@@ -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;
}
}

View File

@@ -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

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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}'");

View File

@@ -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;
}
/**

View File

@@ -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]) {

View File

@@ -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
*

View File

@@ -225,6 +225,7 @@ class Smarty_Template_Compiled
}
// call compiler
try {
$_template->loadCompiler();
$code = $_template->compiler->compileTemplate($_template);
}
catch (Exception $e) {

View File

@@ -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;

View File

@@ -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,7 +265,8 @@ class Smarty_Template_Source
*
* @return int
*/
public function getTimeStamp() {
public function getTimeStamp()
{
if (!isset($this->timestamp)) {
$this->handler->populateTimestamp($this);
}
@@ -259,44 +274,12 @@ class Smarty_Template_Source
}
/**
* <<magic>> 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.");
}
}
/**
* <<magic>> 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);
}
}