- bugfix experimental getTags() method did not work

This commit is contained in:
uwe.tews@googlemail.com
2011-07-26 18:09:31 +00:00
parent c64813beb5
commit c4c0cd254c
5 changed files with 73 additions and 57 deletions

View File

@@ -1,4 +1,7 @@
===== SVN trunk ===== ===== SVN trunk =====
26/07/2011
- bugfix experimental getTags() method did not work
15/07/2011 15/07/2011
- bugfix individual cache_lifetime of {include} did not work correctly inside {block} tags - bugfix individual cache_lifetime of {include} did not work correctly inside {block} tags

View File

@@ -236,6 +236,8 @@ class Smarty extends Smarty_Internal_Data {
public $inheritance = false; public $inheritance = false;
// generate deprecated function call notices? // generate deprecated function call notices?
public $deprecation_notices = true; public $deprecation_notices = true;
// internal flag for getTags()
public $get_used_tags = false;
// Smarty 2 BC // Smarty 2 BC
public $_version = self::SMARTY_VERSION; public $_version = self::SMARTY_VERSION;
// self pointer to Smarty object // self pointer to Smarty object
@@ -714,6 +716,17 @@ class Smarty extends Smarty_Internal_Data {
return false; return false;
} }
/**
* Return array of tag/attributes of all tags used by an template
*
* @param object $templae template object
* @return array of tag/attributes
*/
public function getTags(Smarty_Internal_Template $template)
{
return Smarty_Internal_Utility::getTags($template);
}
/** /**
* clean up properties on cloned object * clean up properties on cloned object
*/ */

View File

@@ -70,6 +70,8 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
// blocks for template inheritance // blocks for template inheritance
public $block_data = array(); public $block_data = array();
public $wrapper = null; public $wrapper = null;
// optional log of tag/attributes
public $used_tags = array();
/** /**
* Create template data object * Create template data object
* *

View File

@@ -23,8 +23,6 @@ class Smarty_Internal_TemplateCompilerBase {
public $_tag_stack = array(); public $_tag_stack = array();
// current template // current template
public $template = null; public $template = null;
// optional log of tag/attributes
public $used_tags = array();
/** /**
* Initialize compiler * Initialize compiler
@@ -114,7 +112,7 @@ class Smarty_Internal_TemplateCompilerBase {
$this->has_output = false; $this->has_output = false;
// log tag/attributes // log tag/attributes
if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) { if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
$this->used_tags[] = array($tag,$args); $this->template->used_tags[] = array($tag,$args);
} }
// check nocache option flag // check nocache option flag
if (in_array("'nocache'",$args) || in_array(array('nocache'=>'true'),$args) if (in_array("'nocache'",$args) || in_array(array('nocache'=>'true'),$args)

View File

@@ -4,29 +4,29 @@
* Project: Smarty: the PHP compiling template engine * Project: Smarty: the PHP compiling template engine
* File: smarty_internal_utility.php * File: smarty_internal_utility.php
* SVN: $Id: $ * SVN: $Id: $
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* For questions, help, comments, discussion, etc., please join the * For questions, help, comments, discussion, etc., please join the
* Smarty mailing list. Send a blank e-mail to * Smarty mailing list. Send a blank e-mail to
* smarty-discussion-subscribe@googlegroups.com * smarty-discussion-subscribe@googlegroups.com
* *
* @link http://www.smarty.net/ * @link http://www.smarty.net/
* @copyright 2008 New Digital Group, Inc. * @copyright 2008 New Digital Group, Inc.
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @author Uwe Tews * @author Uwe Tews
* @package Smarty * @package Smarty
* @subpackage PluginsInternal * @subpackage PluginsInternal
* @version 3-SVN$Rev: 3286 $ * @version 3-SVN$Rev: 3286 $
@@ -38,31 +38,31 @@ class Smarty_Internal_Utility {
function __construct($smarty) function __construct($smarty)
{ {
$this->smarty = $smarty; $this->smarty = $smarty;
} }
/** /**
* Compile all template files * Compile all template files
* *
* @param string $extension file extension * @param string $extension file extension
* @param bool $force_compile force all to recompile * @param bool $force_compile force all to recompile
* @param int $time_limit * @param int $time_limit
* @param int $max_errors * @param int $max_errors
* @return integer number of template files recompiled * @return integer number of template files recompiled
*/ */
function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null) function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
{ {
// switch off time limit // switch off time limit
if (function_exists('set_time_limit')) { if (function_exists('set_time_limit')) {
@set_time_limit($time_limit); @set_time_limit($time_limit);
} }
$this->smarty->force_compile = $force_compile; $this->smarty->force_compile = $force_compile;
$_count = 0; $_count = 0;
$_error_count = 0; $_error_count = 0;
// loop over array of template directories // loop over array of template directories
foreach((array)$this->smarty->template_dir as $_dir) { foreach((array)$this->smarty->template_dir as $_dir) {
if (strpos('/\\', substr($_dir, -1)) === false) { if (strpos('/\\', substr($_dir, -1)) === false) {
$_dir .= DS; $_dir .= DS;
} }
$_compileDirs = new RecursiveDirectoryIterator($_dir); $_compileDirs = new RecursiveDirectoryIterator($_dir);
$_compile = new RecursiveIteratorIterator($_compileDirs); $_compile = new RecursiveIteratorIterator($_compileDirs);
foreach ($_compile as $_fileinfo) { foreach ($_compile as $_fileinfo) {
@@ -91,7 +91,7 @@ class Smarty_Internal_Utility {
catch (Exception $e) { catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "<br><br>"; echo 'Error: ', $e->getMessage(), "<br><br>";
$_error_count++; $_error_count++;
} }
// free memory // free memory
$this->smarty->template_objects = array(); $this->smarty->template_objects = array();
$_tpl->smarty->template_objects = array(); $_tpl->smarty->template_objects = array();
@@ -99,35 +99,35 @@ class Smarty_Internal_Utility {
if ($max_errors !== null && $_error_count == $max_errors) { if ($max_errors !== null && $_error_count == $max_errors) {
echo '<br><br>too many errors'; echo '<br><br>too many errors';
exit(); exit();
} }
} }
} }
return $_count; return $_count;
} }
/** /**
* Compile all config files * Compile all config files
* *
* @param string $extension file extension * @param string $extension file extension
* @param bool $force_compile force all to recompile * @param bool $force_compile force all to recompile
* @param int $time_limit * @param int $time_limit
* @param int $max_errors * @param int $max_errors
* @return integer number of template files recompiled * @return integer number of template files recompiled
*/ */
function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null) function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
{ {
// switch off time limit // switch off time limit
if (function_exists('set_time_limit')) { if (function_exists('set_time_limit')) {
@set_time_limit($time_limit); @set_time_limit($time_limit);
} }
$this->smarty->force_compile = $force_compile; $this->smarty->force_compile = $force_compile;
$_count = 0; $_count = 0;
$_error_count = 0; $_error_count = 0;
// loop over array of template directories // loop over array of template directories
foreach((array)$this->smarty->config_dir as $_dir) { foreach((array)$this->smarty->config_dir as $_dir) {
if (strpos('/\\', substr($_dir, -1)) === false) { if (strpos('/\\', substr($_dir, -1)) === false) {
$_dir .= DS; $_dir .= DS;
} }
$_compileDirs = new RecursiveDirectoryIterator($_dir); $_compileDirs = new RecursiveDirectoryIterator($_dir);
$_compile = new RecursiveIteratorIterator($_compileDirs); $_compile = new RecursiveIteratorIterator($_compileDirs);
foreach ($_compile as $_fileinfo) { foreach ($_compile as $_fileinfo) {
@@ -138,7 +138,7 @@ class Smarty_Internal_Utility {
$_config_file = $_file; $_config_file = $_file;
} else { } else {
$_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file; $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
} }
echo '<br>', $_dir, '---', $_config_file; echo '<br>', $_dir, '---', $_config_file;
flush(); flush();
$_start_time = microtime(true); $_start_time = microtime(true);
@@ -151,24 +151,24 @@ class Smarty_Internal_Utility {
} else { } else {
echo ' is up to date'; echo ' is up to date';
flush(); flush();
} }
} }
catch (Exception $e) { catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "<br><br>"; echo 'Error: ', $e->getMessage(), "<br><br>";
$_error_count++; $_error_count++;
} }
if ($max_errors !== null && $_error_count == $max_errors) { if ($max_errors !== null && $_error_count == $max_errors) {
echo '<br><br>too many errors'; echo '<br><br>too many errors';
exit(); exit();
} }
} }
} }
return $_count; return $_count;
} }
/** /**
* Delete compiled template file * Delete compiled template file
* *
* @param string $resource_name template name * @param string $resource_name template name
* @param string $compile_id compile id * @param string $compile_id compile id
* @param integer $exp_time expiration time * @param integer $exp_time expiration time
@@ -183,14 +183,14 @@ class Smarty_Internal_Utility {
$_resource_part_2 = $resource_name . '.cache' . '.php'; $_resource_part_2 = $resource_name . '.cache' . '.php';
} else { } else {
$_resource_part = ''; $_resource_part = '';
} }
$_dir = $this->smarty->compile_dir; $_dir = $this->smarty->compile_dir;
if ($this->smarty->use_sub_dirs && isset($_compile_id)) { if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
$_dir .= $_compile_id . $_dir_sep; $_dir .= $_compile_id . $_dir_sep;
} }
if (isset($_compile_id)) { if (isset($_compile_id)) {
$_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep; $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
} }
$_count = 0; $_count = 0;
$_compileDirs = new RecursiveDirectoryIterator($_dir); $_compileDirs = new RecursiveDirectoryIterator($_dir);
$_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
@@ -200,7 +200,7 @@ class Smarty_Internal_Utility {
if (!$_compile->isDot()) { if (!$_compile->isDot()) {
// delete folder if empty // delete folder if empty
@rmdir($_file->getPathname()); @rmdir($_file->getPathname());
} }
} else { } else {
if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) && if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
(!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) || (!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) ||
@@ -208,29 +208,29 @@ class Smarty_Internal_Utility {
if (isset($exp_time)) { if (isset($exp_time)) {
if (time() - @filemtime($_file) >= $exp_time) { if (time() - @filemtime($_file) >= $exp_time) {
$_count += @unlink((string) $_file) ? 1 : 0; $_count += @unlink((string) $_file) ? 1 : 0;
} }
} else { } else {
$_count += @unlink((string) $_file) ? 1 : 0; $_count += @unlink((string) $_file) ? 1 : 0;
} }
} }
} }
} }
return $_count; return $_count;
} }
/** /**
* Return array of tag/attributes of all tags used by an template * Return array of tag/attributes of all tags used by an template
* *
* @param object $templae template object * @param object $templae template object
* @return array of tag/attributes * @return array of tag/attributes
*/ */
function getTags(Smarty_Internal_Template $template) public static function getTags(Smarty_Internal_Template $template)
{ {
$template->smarty->get_used_tags = true; $template->smarty->get_used_tags = true;
$template->compileTemplateSource(); $template->compileTemplateSource();
return $template->compiler_object->used_tags; return $template->used_tags;
} }
function testInstall() function testInstall()
{ {
echo "<PRE>\n"; echo "<PRE>\n";
@@ -246,7 +246,7 @@ class Smarty_Internal_Utility {
echo "FAILED: $template_dir is not readable.\n"; echo "FAILED: $template_dir is not readable.\n";
else else
echo "$template_dir is OK.\n"; echo "$template_dir is OK.\n";
} }
echo "Testing compile directory...\n"; echo "Testing compile directory...\n";
@@ -268,7 +268,7 @@ class Smarty_Internal_Utility {
echo "FAILED: $plugin_dir is not readable.\n"; echo "FAILED: $plugin_dir is not readable.\n";
else else
echo "$plugin_dir is OK.\n"; echo "$plugin_dir is OK.\n";
} }
echo "Testing cache directory...\n"; echo "Testing cache directory...\n";
@@ -295,6 +295,6 @@ class Smarty_Internal_Utility {
echo "</PRE>\n"; echo "</PRE>\n";
return true; return true;
} }
} }
?> ?>