- improvement check if ini_get() and ini_set() not disabled

https://github.com/smarty-php/smarty/pull/362
This commit is contained in:
uwetews
2017-05-19 22:35:55 +02:00
parent 2782e83a92
commit da22c961bb
8 changed files with 130 additions and 105 deletions

View File

@@ -2,8 +2,10 @@
19.5.2017
- change properties $accessMap and $obsoleteProperties from private to protected
https://github.com/smarty-php/smarty/issues/351
- improvement The named capture buffers can now be accessed also as array
- new feature The named capture buffers can now be accessed also as array
See NEWS_FEATURES.txt https://github.com/smarty-php/smarty/issues/366
- improvement check if ini_get() and ini_set() not disabled
https://github.com/smarty-php/smarty/pull/362
24.4.2017
- fix spelling https://github.com/smarty-php/smarty/commit/e3eda8a5f5653d8abb960eb1bc47e3eca679b1b4#commitcomment-21803095

View File

@@ -33,7 +33,9 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
$_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) {
$cached->filepath .= preg_replace(array('![^\w|]+!', '![|]+!'), array('_', $_compile_dir_sep),
$cached->filepath .= preg_replace(array('![^\w|]+!',
'![|]+!'), array('_',
$_compile_dir_sep),
$_template->cache_id) . $_compile_dir_sep;
}
if (isset($_template->compile_id)) {
@@ -41,7 +43,8 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
}
// if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] . $smarty->ds .
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] .
$smarty->ds .
$_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
}
$cached->filepath .= $_filepath;
@@ -108,7 +111,9 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
$_template->smarty) === true
) {
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
if (function_exists('opcache_invalidate') &&
(!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1
) {
opcache_invalidate($_template->cached->filepath, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($_template->cached->filepath);

View File

@@ -102,7 +102,8 @@ class Smarty_Internal_Config_File_Compiler
{
$this->template = $template;
$this->template->compiled->file_dependency[ $this->template->source->uid ] =
array($this->template->source->filepath, $this->template->source->getTimeStamp(),
array($this->template->source->filepath,
$this->template->source->getTimeStamp(),
$this->template->source->type);
if ($this->smarty->debugging) {
if (!isset($this->smarty->_debug)) {
@@ -112,18 +113,21 @@ class Smarty_Internal_Config_File_Compiler
}
// init the lexer/parser to compile the config file
/* @var Smarty_Internal_ConfigFileLexer $this ->lex */
$this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n",
$this->lex = new $this->lexer_class(str_replace(array("\r\n",
"\r"), "\n", $template->source->getContent()) . "\n",
$this);
/* @var Smarty_Internal_ConfigFileParser $this ->parser */
$this->parser = new $this->parser_class($this->lex, $this);
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
if (function_exists('mb_internal_encoding')
&& function_exists('ini_get')
&& ((int) ini_get('mbstring.func_overload')) & 2
) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('ASCII');
} else {
$mbEncoding = null;
}
if ($this->smarty->_parserdebug) {
$this->parser->PrintTrace();
}

View File

@@ -111,7 +111,9 @@ class Smarty_Internal_Method_ClearCompiledTemplate
if ($unlink && @unlink($_filepath)) {
$_count ++;
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($_filepath, true);
}
}

View File

@@ -18,13 +18,6 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
*/
public $uncompiled = true;
/**
* container for short_open_tag directive's value before executing PHP templates
*
* @var string
*/
protected $short_open_tag;
/**
* Resource does implement populateCompiledFilepath() method
*
@@ -32,12 +25,19 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
*/
public $hasCompiledHandler = true;
/**
* container for short_open_tag directive's value before executing PHP templates
*
* @var string
*/
protected $short_open_tag;
/**
* Create a new PHP Resource
*/
public function __construct()
{
$this->short_open_tag = ini_get('short_open_tag');
$this->short_open_tag = function_exists('ini_get') ? ini_get('short_open_tag') : 1;
}
/**
@@ -56,6 +56,23 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
}
/**
* populate compiled object with compiled filepath
*
* @param Smarty_Template_Compiled $compiled compiled object
* @param Smarty_Internal_Template $_template template object (is ignored)
*/
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
{
$compiled->filepath = $_template->source->filepath;
$compiled->timestamp = $_template->source->timestamp;
$compiled->exists = $_template->source->exists;
$compiled->file_dependency[ $_template->source->uid ] =
array($compiled->filepath,
$compiled->timestamp,
$_template->source->type,);
}
/**
* Render and output the template (without using the compiler)
*
@@ -79,28 +96,16 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
extract($_template->getTemplateVars());
// include PHP template with short open tags enabled
if (function_exists('ini_set')) {
ini_set('short_open_tag', '1');
}
/** @var Smarty_Internal_Template $_smarty_template
* used in included file
*/
$_smarty_template = $_template;
include($source->filepath);
if (function_exists('ini_set')) {
ini_set('short_open_tag', $this->short_open_tag);
}
/**
* populate compiled object with compiled filepath
*
* @param Smarty_Template_Compiled $compiled compiled object
* @param Smarty_Internal_Template $_template template object (is ignored)
*/
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
{
$compiled->filepath = $_template->source->filepath;
$compiled->timestamp = $_template->source->timestamp;
$compiled->exists = $_template->source->exists;
$compiled->file_dependency[ $_template->source->uid ] =
array($compiled->filepath, $compiled->timestamp,
$_template->source->type,);
}
}

View File

@@ -125,7 +125,9 @@ class Smarty_Internal_Runtime_CacheResourceFile
}
}
$_count += @unlink($_filepath) ? 1 : 0;
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($_filepath, true);
}
}

View File

@@ -89,12 +89,16 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
then written to compiled files. */
// init the lexer/parser to compile the template
$this->parser =
new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $_content), $this),
new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n",
"\r"), "\n", $_content), $this),
$this);
if ($isTemplateSource && $this->template->caching) {
$this->parser->insertPhpCode("<?php\n\$_smarty_tpl->compiled->nocache_hash = '{$this->nocache_hash}';\n?>\n");
}
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
if (function_exists('mb_internal_encoding')
&& function_exists('ini_get')
&& ((int) ini_get('mbstring.func_overload')) & 2
) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('ASCII');
} else {

View File

@@ -80,64 +80,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
}
}
/**
* load compiled template or compile from source
*
* @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*
* @throws Exception
*/
public function process(Smarty_Internal_Template $_smarty_tpl)
{
$source = &$_smarty_tpl->source;
$smarty = &$_smarty_tpl->smarty;
if ($source->handler->recompiled) {
$source->handler->process($_smarty_tpl);
} elseif (!$source->handler->uncompiled) {
if (!$this->exists || $smarty->force_compile ||
($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp())
) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
} else {
$_smarty_tpl->mustCompile = true;
@include($this->filepath);
if ($_smarty_tpl->mustCompile) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
}
}
$_smarty_tpl->_subTemplateRegister();
$this->processed = true;
}
}
/**
* Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*/
private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl)
{
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
opcache_invalidate($this->filepath, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath);
}
if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath));
} else {
include($this->filepath);
}
}
/**
* render compiled template code
*
@@ -179,6 +121,44 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
}
}
/**
* load compiled template or compile from source
*
* @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*
* @throws Exception
*/
public function process(Smarty_Internal_Template $_smarty_tpl)
{
$source = &$_smarty_tpl->source;
$smarty = &$_smarty_tpl->smarty;
if ($source->handler->recompiled) {
$source->handler->process($_smarty_tpl);
} elseif (!$source->handler->uncompiled) {
if (!$this->exists || $smarty->force_compile ||
($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp())
) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
} else {
$_smarty_tpl->mustCompile = true;
@include($this->filepath);
if ($_smarty_tpl->mustCompile) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
}
}
$_smarty_tpl->_subTemplateRegister();
$this->processed = true;
}
}
/**
* compile template from source
*
@@ -238,6 +218,27 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
return true;
}
/**
* Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*/
private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl)
{
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath);
}
if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath));
} else {
include($this->filepath);
}
}
/**
* Read compiled content from handler
*