From 60a56969ddce3254f72d5f78cb1811da15585399 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Mon, 2 Jan 2023 00:49:38 +0100 Subject: [PATCH] WIP moving Smarty to PSR-4. --- demo/plugins/resource.mysql.php | 104 --- demo/plugins/resource.mysqls.php | 80 --- docs/designers/chapter-debugging-console.md | 3 +- docs/index.md | 1 - docs/programmers/api-functions/api-display.md | 4 +- docs/programmers/api-variables.md | 1 - .../api-variables/variable-debug-template.md | 3 +- .../api-variables/variable-plugins-dir.md | 28 - docs/programmers/smarty-constants.md | 26 - src/BlockHandler/BlockPluginWrapper.php | 18 + src/Cacheresource/File.php | 4 +- ...ivateBlockPlugin.php => BlockCompiler.php} | 16 +- ...dFunction.php => FunctionCallCompiler.php} | 51 +- src/Compile/Modifier/BCPluginWrapper.php | 19 + .../Modifier/EscapeModifierCompiler.php | 2 +- src/Compile/ModifierCompiler.php | 79 +++ ...tion.php => ObjectMethodBlockCompiler.php} | 4 +- ...ction.php => ObjectMethodCallCompiler.php} | 12 +- ...ession.php => PrintExpressionCompiler.php} | 43 +- ...riable.php => SpecialVariableCompiler.php} | 22 +- src/Compile/Tag/BCPluginWrapper.php | 3 +- src/Compile/Tag/Base.php | 10 +- src/Compile/Tag/BlockClose.php | 2 +- src/Compile/Tag/Call.php | 6 +- src/Compile/Tag/EvalTag.php | 8 +- src/Compile/Tag/ForeachSection.php | 8 +- src/Compile/Tag/IncludeTag.php | 12 +- src/Compile/Tag/MakeNocache.php | 2 +- src/Compile/Tag/NocacheClose.php | 1 + src/Compile/Tag/PrivateFunctionPlugin.php | 79 --- src/Compile/Tag/PrivateModifier.php | 134 ---- src/Compile/Tag/TagCompilerInterface.php | 4 +- src/Compiler/BaseCompiler.php | 23 + src/Compiler/CodeFrame.php | 2 +- src/Compiler/Configfile.php | 38 +- src/Compiler/Template.php | 634 ++++++------------ src/Data.php | 13 +- src/Debug.php | 6 +- src/Extension/BCPluginsAdapter.php | 104 ++- src/Extension/Base.php | 10 +- src/Extension/CoreExtension.php | 55 ++ .../{Core.php => DefaultExtension.php} | 89 +-- src/Extension/ExtensionInterface.php | 4 +- src/FunctionHandler/BCPluginWrapper.php | 22 + src/FunctionHandler/Base.php | 18 + .../FunctionHandlerInterface.php | 3 +- src/Parser/TemplateParser.y | 26 +- src/Resource/BasePlugin.php | 2 +- src/Resource/RecompiledPlugin.php | 2 +- src/Runtime/InheritanceRuntime.php | 2 +- src/Security.php | 11 +- src/Smarty.php | 298 ++++++-- src/Template.php | 17 +- src/Template/Cached.php | 2 +- src/Template/Compiled.php | 2 +- src/Template/Config.php | 27 +- src/Template/Source.php | 36 +- src/TemplateBase.php | 73 +- tests/PHPUnit_Smarty.php | 3 +- .../PluginDirNormalizationTest.php | 145 ---- .../ProtectedFolderVarsTest.php | 47 +- .../A_Core/LoadPlugin/LoadPluginTest.php | 4 +- .../Apc/CacheResourceCustomApcTest.php | 3 +- .../PDO/CacheResourceCustomPDOTest.php | 62 +- .../CacheResourceCustomPDOGzipTest.php | 59 +- .../PHPunitplugins/cacheresource.apctest.php | 2 +- .../cacheresource.memcachetest.php | 2 +- .../cacheresource.mysqltest.php | 2 +- .../cacheresource.pdo_gziptest.php | 6 +- .../PHPunitplugins/cacheresource.pdotest.php | 6 +- .../DefaultConfigHandlerTest.php | 8 +- .../ConfigFileTests/file/ConfigVarTest.php | 2 +- .../file/PHPunitplugins/resource.db4.php | 30 - .../ResourceExtendsAllPluginTest.php | 22 +- .../PHPunitplugins/resource.mysqlstest.php | 21 - .../PHPunitplugins/resource.mysqltest.php | 22 - .../DefaultTemplateHandlerTest.php | 6 +- .../ResourceTests/Php/PhpResourceTest.php | 301 --------- .../ResourceTests/Php/cache/.gitignore | 2 - .../Php/templates/includephp.tpl | 1 - .../Php/templates/phphelloworld.php | 1 - .../Php/templates_2/helloworld.php | 1 - .../ResourceTests/Php/templates_c/.gitignore | 2 - .../ClearCompiledTest.php | 3 - .../RegisterBlock/RegisterBlockTest.php | 32 +- .../RegisterCompilerFunctionTest.php | 30 +- .../RegisterFunction/RegisterFunctionTest.php | 38 +- .../RegisterModifier/RegisterModifierTest.php | 8 +- .../TemplateSource/Comments/CommentsTest.php | 2 +- .../TemplateSource/Spacing/SpacingTest.php | 2 +- .../BlockPlugin/CompileBlockPluginTest.php | 13 - .../PHPunitplugins/block.teststack.php | 25 - .../PHPunitplugins/compiler.test.php | 16 +- .../PHPunitplugins/compiler.testclose.php | 12 +- .../549/MbSplitEncodingIssue549Test.php | 6 +- .../_Issues/topic26878/NewlineSpacing.php | 2 +- .../compiler.getparamsshort.php | 2 +- .../cacheresources}/cacheresource.apc.php | 0 .../cacheresource.memcache.php | 0 .../cacheresources}/cacheresource.mysql.php | 0 .../cacheresources}/cacheresource.pdo.php | 0 .../cacheresource.pdo_gzip.php | 3 +- .../resources}/resource.extendsall.php | 0 103 files changed, 1161 insertions(+), 2101 deletions(-) delete mode 100644 demo/plugins/resource.mysql.php delete mode 100644 demo/plugins/resource.mysqls.php delete mode 100644 docs/programmers/api-variables/variable-plugins-dir.md delete mode 100644 docs/programmers/smarty-constants.md create mode 100644 src/BlockHandler/BlockPluginWrapper.php rename src/Compile/{Tag/PrivateBlockPlugin.php => BlockCompiler.php} (92%) rename src/Compile/{Tag/PrivateRegisteredFunction.php => FunctionCallCompiler.php} (55%) create mode 100644 src/Compile/Modifier/BCPluginWrapper.php create mode 100644 src/Compile/ModifierCompiler.php rename src/Compile/{Tag/PrivateObjectBlockFunction.php => ObjectMethodBlockCompiler.php} (91%) rename src/Compile/{Tag/PrivateObjectFunction.php => ObjectMethodCallCompiler.php} (91%) rename src/Compile/{Tag/PrivatePrintExpression.php => PrintExpressionCompiler.php} (83%) rename src/Compile/{Tag/PrivateSpecialVariable.php => SpecialVariableCompiler.php} (89%) delete mode 100644 src/Compile/Tag/PrivateFunctionPlugin.php delete mode 100644 src/Compile/Tag/PrivateModifier.php create mode 100644 src/Compiler/BaseCompiler.php create mode 100644 src/Extension/CoreExtension.php rename src/Extension/{Core.php => DefaultExtension.php} (81%) create mode 100644 src/FunctionHandler/BCPluginWrapper.php delete mode 100644 tests/UnitTests/A_0/PathNormalization/PluginDirNormalizationTest.php delete mode 100644 tests/UnitTests/ConfigFileTests/file/PHPunitplugins/resource.db4.php delete mode 100644 tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php delete mode 100644 tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php delete mode 100644 tests/UnitTests/ResourceTests/Php/PhpResourceTest.php delete mode 100644 tests/UnitTests/ResourceTests/Php/cache/.gitignore delete mode 100644 tests/UnitTests/ResourceTests/Php/templates/includephp.tpl delete mode 100644 tests/UnitTests/ResourceTests/Php/templates/phphelloworld.php delete mode 100644 tests/UnitTests/ResourceTests/Php/templates_2/helloworld.php delete mode 100644 tests/UnitTests/ResourceTests/Php/templates_c/.gitignore delete mode 100644 tests/UnitTests/TemplateSource/TagTests/BlockPlugin/PHPunitplugins/block.teststack.php rename {demo/plugins => tests/UnitTests/__shared/cacheresources}/cacheresource.apc.php (100%) rename {demo/plugins => tests/UnitTests/__shared/cacheresources}/cacheresource.memcache.php (100%) rename {demo/plugins => tests/UnitTests/__shared/cacheresources}/cacheresource.mysql.php (100%) rename {demo/plugins => tests/UnitTests/__shared/cacheresources}/cacheresource.pdo.php (100%) rename {demo/plugins => tests/UnitTests/__shared/cacheresources}/cacheresource.pdo_gzip.php (95%) rename {demo/plugins => tests/UnitTests/__shared/resources}/resource.extendsall.php (100%) diff --git a/demo/plugins/resource.mysql.php b/demo/plugins/resource.mysql.php deleted file mode 100644 index 7b151a78..00000000 --- a/demo/plugins/resource.mysql.php +++ /dev/null @@ -1,104 +0,0 @@ -CREATE TABLE IF NOT EXISTS `templates` ( - * `name` varchar(100) NOT NULL, - * `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - * `source` text, - * PRIMARY KEY (`name`) - * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - * Demo data: - *
INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello
- * world"}{$x}');
- * - * - * @package Resource-examples - * @author Rodney Rehm - */ -class _MysqlPlugin extends CustomPlugin -{ - /** - * PDO instance - * - * @var \PDO - */ - protected $db; - - /** - * prepared fetch() statement - * - * @var \PDOStatement - */ - protected $fetch; - - /** - * prepared fetchTimestamp() statement - * - * @var \PDOStatement - */ - protected $mtime; - - /** - * Smarty_Resource_Mysql constructor. - * - * @throws \Smarty\Exception - */ - public function __construct() - { - try { - $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); - } catch (PDOException $e) { - throw new Exception('Mysql Resource failed: ' . $e->getMessage()); - } - $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); - $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); - } - - /** - * Fetch a template and its modification time from database - * - * @param string $name template name - * @param string $source template source - * @param integer $mtime template modification timestamp (epoch) - * - * @return void - */ - protected function fetch($name, &$source, &$mtime) - { - $this->fetch->execute(array('name' => $name)); - $row = $this->fetch->fetch(); - $this->fetch->closeCursor(); - if ($row) { - $source = $row[ 'source' ]; - $mtime = strtotime($row[ 'modified' ]); - } else { - $source = null; - $mtime = null; - } - } - - /** - * Fetch a template's modification time from database - * - * @note implementing this method is optional. Only implement it if modification times can be accessed faster than - * loading the comple template source. - * - * @param string $name template name - * - * @return integer timestamp (epoch) the template was modified - */ - protected function fetchTimestamp($name) - { - $this->mtime->execute(array('name' => $name)); - $mtime = $this->mtime->fetchColumn(); - $this->mtime->closeCursor(); - return strtotime($mtime); - } -} diff --git a/demo/plugins/resource.mysqls.php b/demo/plugins/resource.mysqls.php deleted file mode 100644 index 84e2ed20..00000000 --- a/demo/plugins/resource.mysqls.php +++ /dev/null @@ -1,80 +0,0 @@ -CREATE TABLE IF NOT EXISTS `templates` ( - * `name` varchar(100) NOT NULL, - * `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - * `source` text, - * PRIMARY KEY (`name`) - * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - * Demo data: - *
INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello
- * world"}{$x}');
- * - * - * @package Resource-examples - * @author Rodney Rehm - */ -class _MysqlsPlugin extends CustomPlugin -{ - /** - * PDO instance - * - * @var \PDO - */ - protected $db; - - /** - * prepared fetch() statement - * - * @var \PDOStatement - */ - protected $fetch; - - /** - * Smarty_Resource_Mysqls constructor. - * - * @throws \Smarty\Exception - */ - public function __construct() - { - try { - $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); - } catch (PDOException $e) { - throw new Exception('Mysql Resource failed: ' . $e->getMessage()); - } - $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); - } - - /** - * Fetch a template and its modification time from database - * - * @param string $name template name - * @param string $source template source - * @param integer $mtime template modification timestamp (epoch) - * - * @return void - */ - protected function fetch($name, &$source, &$mtime) - { - $this->fetch->execute(array('name' => $name)); - $row = $this->fetch->fetch(); - $this->fetch->closeCursor(); - if ($row) { - $source = $row[ 'source' ]; - $mtime = strtotime($row[ 'modified' ]); - } else { - $source = null; - $mtime = null; - } - } -} diff --git a/docs/designers/chapter-debugging-console.md b/docs/designers/chapter-debugging-console.md index 6429b487..787ec383 100644 --- a/docs/designers/chapter-debugging-console.md +++ b/docs/designers/chapter-debugging-console.md @@ -11,8 +11,7 @@ of the console. Set [`$debugging`](../programmers/api-variables/variable-debugging.md) to TRUE in Smarty, and if needed set [`$debug_tpl`](../programmers/api-variables/variable-debug-template.md) to the template resource -path to `debug.tpl` (this is in [`SMARTY_DIR`](../programmers/smarty-constants.md) by -default). When you load the page, a Javascript console window will pop +path to `debug.tpl`. When you load the page, a Javascript console window will pop up and give you the names of all the included templates and assigned variables for the current page. diff --git a/docs/index.md b/docs/index.md index cf2b2ddc..a4a2c72f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -37,7 +37,6 @@ and 480 for $height, the result is: ## Smarty for php developers - [Charset Encoding](./programmers/charset.md) -- [Constants](./programmers/smarty-constants.md) - [Smarty Class Variables](./programmers/api-variables.md) - [Smarty Class Methods](./programmers/api-functions.md) - [Caching](./programmers/caching.md) diff --git a/docs/programmers/api-functions/api-display.md b/docs/programmers/api-functions/api-display.md index 59726195..ced7513f 100644 --- a/docs/programmers/api-functions/api-display.md +++ b/docs/programmers/api-functions/api-display.md @@ -31,7 +31,9 @@ PARAMETER.COMPILEID setCaching(true); diff --git a/docs/programmers/api-variables.md b/docs/programmers/api-variables.md index be3f86a1..e3b8d885 100644 --- a/docs/programmers/api-variables.md +++ b/docs/programmers/api-variables.md @@ -37,7 +37,6 @@ them directly, or use the corresponding setter/getter methods. - [$left_delimiter](./api-variables/variable-left-delimiter.md) - [$locking_timeout](./api-variables/variable-locking-timeout.md) - [$merge_compiled_includes](./api-variables/variable-merge-compiled-includes.md) -- [$plugins_dir](./api-variables/variable-plugins-dir.md) - [$right_delimiter](./api-variables/variable-right-delimiter.md) - [$smarty_debug_id](./api-variables/variable-smarty-debug-id.md) - [$template_dir](./api-variables/variable-template-dir.md) diff --git a/docs/programmers/api-variables/variable-debug-template.md b/docs/programmers/api-variables/variable-debug-template.md index faec0e17..11a80529 100644 --- a/docs/programmers/api-variables/variable-debug-template.md +++ b/docs/programmers/api-variables/variable-debug-template.md @@ -2,8 +2,7 @@ ============ This is the name of the template file used for the debugging console. By -default, it is named `debug.tpl` and is located in the -[`SMARTY_DIR`](#constant.smarty.dir). +default, it is named `debug.tpl` and is located in `src/debug.tpl`. See also [`$debugging`](#variable.debugging) and the [debugging console](#chapter.debugging.console) section. diff --git a/docs/programmers/api-variables/variable-plugins-dir.md b/docs/programmers/api-variables/variable-plugins-dir.md deleted file mode 100644 index 8a7cfcdb..00000000 --- a/docs/programmers/api-variables/variable-plugins-dir.md +++ /dev/null @@ -1,28 +0,0 @@ -\$plugins\_dir {#variable.plugins.dir} -============== - -This is the directory or directories where Smarty will look for the -plugins that it needs. Default is `plugins/` under the -[`SMARTY_DIR`](#constant.smarty.dir). If you supply a relative path, -Smarty will first look under the [`SMARTY_DIR`](#constant.smarty.dir), -then relative to the current working directory, then relative to the PHP -include\_path. If `$plugins_dir` is an array of directories, Smarty will -search for your plugin in each plugin directory **in the order they are -given**. - -> **Note** -> -> For best performance, do not setup your `$plugins_dir` to have to use -> the PHP include path. Use an absolute pathname, or a path relative to -> `SMARTY_DIR` or the current working directory. - -> **Note** -> -> As of Smarty 3.1 the attribute \$plugins\_dir is no longer accessible -> directly. Use [`getPluginsDir()`](#api.get.plugins.dir), -> [`setPluginsDir()`](#api.set.plugins.dir) and -> [`addPluginsDir()`](#api.add.plugins.dir) instead. - -See also [`getPluginsDir()`](#api.get.plugins.dir), -[`setPluginsDir()`](#api.set.plugins.dir) and -[`addPluginsDir()`](#api.add.plugins.dir). diff --git a/docs/programmers/smarty-constants.md b/docs/programmers/smarty-constants.md deleted file mode 100644 index de04e1b5..00000000 --- a/docs/programmers/smarty-constants.md +++ /dev/null @@ -1,26 +0,0 @@ -Constants {#smarty.constants} -========= - -SMARTY\_DIR {#constant.smarty.dir} -=========== - -This is the **full system path** to the location of the Smarty class -files. If this is not defined in your script, then Smarty will attempt -to determine the appropriate value automatically. If defined, the path -**must end with a trailing slash/**. - - - - - - -See also [`$smarty.const`](../designers/language-variables/language-variables-smarty.md). diff --git a/src/BlockHandler/BlockPluginWrapper.php b/src/BlockHandler/BlockPluginWrapper.php new file mode 100644 index 00000000..ed5ee9b0 --- /dev/null +++ b/src/BlockHandler/BlockPluginWrapper.php @@ -0,0 +1,18 @@ +callback = $callback; + } + + public function handle($params, $content, Template $template, &$repeat) { + return call_user_func($this->callback, $params, $content, $template, $repeat); + } +} \ No newline at end of file diff --git a/src/Cacheresource/File.php b/src/Cacheresource/File.php index a0685f31..b026c38a 100644 --- a/src/Cacheresource/File.php +++ b/src/Cacheresource/File.php @@ -2,6 +2,8 @@ namespace Smarty\Cacheresource; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use Smarty\Smarty; use Smarty\Template; use Smarty\Template\Cached; @@ -206,7 +208,7 @@ class File extends Base if (isset($resource_name)) { $_save_stat = $smarty->caching; $smarty->caching = \Smarty\Smarty::CACHING_LIFETIME_CURRENT; - $tpl = new $smarty->template_class($resource_name, $smarty); + $tpl = new \Smarty\Template($resource_name, $smarty); $smarty->caching = $_save_stat; // remove from template cache $tpl->source; // have the template registered before unset() diff --git a/src/Compile/Tag/PrivateBlockPlugin.php b/src/Compile/BlockCompiler.php similarity index 92% rename from src/Compile/Tag/PrivateBlockPlugin.php rename to src/Compile/BlockCompiler.php index 48eb01ad..47423ea6 100644 --- a/src/Compile/Tag/PrivateBlockPlugin.php +++ b/src/Compile/BlockCompiler.php @@ -8,7 +8,9 @@ * @author Uwe Tews */ -namespace Smarty\Compile\Tag; +namespace Smarty\Compile; + +use Smarty\Compile\Tag\Base; /** * Smarty Internal Plugin Compile Block Plugin Class @@ -16,7 +18,7 @@ namespace Smarty\Compile\Tag; * @package Smarty * @subpackage Compiler */ -class PrivateBlockPlugin extends Base { +class BlockCompiler extends Base { /** * Attribute definition: Overwrites base class. @@ -83,14 +85,8 @@ class PrivateBlockPlugin extends Base { $mod_content2 = "\$_block_content{$this->nesting}"; $mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n"; $mod_pre = "ob_start();\n"; - $mod_post = 'echo ' . $compiler->compileTag( - 'private_modifier', - [], - [ - 'modifierlist' => $parameter['modifier_list'], - 'value' => 'ob_get_clean()', - ] - ) . ";\n"; + $mod_post = 'echo ' . $compiler->compileModifier($parameter['modifier_list'], 'ob_get_clean()') + . ";\n"; } $output = "getAttributes($compiler, $args); unset($_attr['nocache']); - if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION][$tag])) { - $tag_info = $compiler->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION][$tag]; - $is_registered = true; - } else { - $tag_info = $compiler->default_handler_plugins[Smarty::PLUGIN_FUNCTION][$tag]; - $is_registered = false; - } + + + $functionHandler = $compiler->smarty->getFunctionHandler($function); + // not cacheable? - $compiler->tag_nocache = $compiler->tag_nocache || !$tag_info[1]; + $compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable(); // convert attributes into parameter array string $_paramsArray = []; foreach ($_attr as $_key => $_value) { if (is_int($_key)) { $_paramsArray[] = "$_key=>$_value"; - } elseif ($compiler->template->caching && in_array($_key, $tag_info[2])) { + } elseif ($compiler->template->caching && in_array($_key, $functionHandler->getCacheAttributes())) { $_value = str_replace('\'', "^#^", $_value); $_paramsArray[] = "'$_key'=>^#^.var_export($_value,true).^#^"; } else { @@ -67,29 +66,13 @@ class PrivateRegisteredFunction extends Base { } } $_params = 'array(' . implode(',', $_paramsArray) . ')'; - // compile code - if ($is_registered) { - $output = - "call_user_func_array( \$_smarty_tpl->smarty->registered_plugins[\\Smarty\\Smarty::PLUGIN_FUNCTION]['{$tag}'][0], array( {$_params},\$_smarty_tpl ) )"; - } else { - $function = $tag_info[0]; - if (!is_array($function)) { - $output = "{$function}({$_params},\$_smarty_tpl)"; - } else { - $output = "{$function[0]}::{$function[1]}({$_params},\$_smarty_tpl)"; - } - } + + $output = "\$_smarty_tpl->smarty->getFunctionHandler(" . var_export($function, true) . ")"; + $output .= "->handle($_params, \$_smarty_tpl)"; + if (!empty($parameter['modifierlist'])) { - $output = $compiler->compileTag( - 'private_modifier', - [], - [ - 'modifierlist' => $parameter['modifierlist'], - 'value' => $output, - ] - ); + $output = $compiler->compileModifier($parameter['modifierlist'], $output); } - $output = "\n"; - return $output; + return "\n"; } } diff --git a/src/Compile/Modifier/BCPluginWrapper.php b/src/Compile/Modifier/BCPluginWrapper.php new file mode 100644 index 00000000..0147651f --- /dev/null +++ b/src/Compile/Modifier/BCPluginWrapper.php @@ -0,0 +1,19 @@ +callback = $callback; + } + + /** + * @inheritDoc + */ + public function compile($params, \Smarty\Compiler\Template $compiler) { + return call_user_func($this->callback, $params, $compiler); + } +} \ No newline at end of file diff --git a/src/Compile/Modifier/EscapeModifierCompiler.php b/src/Compile/Modifier/EscapeModifierCompiler.php index 0c839232..4847ecd3 100644 --- a/src/Compile/Modifier/EscapeModifierCompiler.php +++ b/src/Compile/Modifier/EscapeModifierCompiler.php @@ -50,6 +50,6 @@ class EscapeModifierCompiler extends Base { } catch (Exception $e) { // pass through to regular plugin fallback } - return 'smarty_modifier_escape(' . join(', ', $params) . ')'; + return '$_smarty_tpl->smarty->getModifierCallback(\'escape\')(' . join(', ', $params) . ')'; } } \ No newline at end of file diff --git a/src/Compile/ModifierCompiler.php b/src/Compile/ModifierCompiler.php new file mode 100644 index 00000000..59e7b969 --- /dev/null +++ b/src/Compile/ModifierCompiler.php @@ -0,0 +1,79 @@ +has_code = true; + + // check and get attributes + $_attr = $this->getAttributes($compiler, $args); + $output = $parameter['value']; + // loop over list of modifiers + foreach ($parameter['modifierlist'] as $single_modifier) { + /* @var string $modifier */ + $modifier = $single_modifier[0]; + $single_modifier[0] = $output; + $params = implode(',', $single_modifier); + + if (!is_object($compiler->smarty->security_policy) + || $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler) + ) { + + if ($handler = $compiler->getModifierCompiler($modifier)) { + return $handler->compile($single_modifier, $compiler); + + } elseif ($compiler->getSmarty()->getModifierCallback($modifier)) { + return sprintf( + '$_smarty_tpl->smarty->getModifierCallback(%s)(%s)', + var_export($modifier, true), + $params + ); + } elseif ($callback = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)) { + $wrapper = new \Smarty\Compile\Modifier\BCPluginWrapper($callback); + return $wrapper->compile($single_modifier, $compiler); + } elseif ($function = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) { + if (!is_array($function)) { + return "{$function}({$params})"; + } + $operator = is_object($function[0]) ? '->' : '::'; + return $function[0] . $operator . $function[1] . '(' . $params . ')'; + } else { + $compiler->trigger_template_error("unknown modifier '{$modifier}'", null, true); + } + + } + + } + return $output; + } +} diff --git a/src/Compile/Tag/PrivateObjectBlockFunction.php b/src/Compile/ObjectMethodBlockCompiler.php similarity index 91% rename from src/Compile/Tag/PrivateObjectBlockFunction.php rename to src/Compile/ObjectMethodBlockCompiler.php index 424654c7..3334deae 100644 --- a/src/Compile/Tag/PrivateObjectBlockFunction.php +++ b/src/Compile/ObjectMethodBlockCompiler.php @@ -8,7 +8,7 @@ * @author Uwe Tews */ -namespace Smarty\Compile\Tag; +namespace Smarty\Compile; use Smarty\Compiler\Template; @@ -18,7 +18,7 @@ use Smarty\Compiler\Template; * @package Smarty * @subpackage Compiler */ -class PrivateObjectBlockFunction extends PrivateBlockPlugin { +class ObjectMethodBlockCompiler extends BlockCompiler { /** * Setup callback and parameter array diff --git a/src/Compile/Tag/PrivateObjectFunction.php b/src/Compile/ObjectMethodCallCompiler.php similarity index 91% rename from src/Compile/Tag/PrivateObjectFunction.php rename to src/Compile/ObjectMethodCallCompiler.php index 65163f30..3c73d2fc 100644 --- a/src/Compile/Tag/PrivateObjectFunction.php +++ b/src/Compile/ObjectMethodCallCompiler.php @@ -8,7 +8,9 @@ * @author Uwe Tews */ -namespace Smarty\Compile\Tag; +namespace Smarty\Compile; + +use Smarty\Compile\Tag\Base; /** * Smarty Internal Plugin Compile Object Function Class @@ -16,7 +18,7 @@ namespace Smarty\Compile\Tag; * @package Smarty * @subpackage Compiler */ -class PrivateObjectFunction extends Base { +class ObjectMethodCallCompiler extends Base { /** * Attribute definition: Overwrites base class. @@ -71,11 +73,7 @@ class PrivateObjectFunction extends Base { $output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}"; } if (!empty($parameter['modifierlist'])) { - $output = $compiler->compileTag( - 'private_modifier', - [], - ['modifierlist' => $parameter['modifierlist'], 'value' => $output] - ); + $output = $compiler->compileModifier($parameter['modifierlist'], $output); } if (empty($_assign)) { return "\n"; diff --git a/src/Compile/Tag/PrivatePrintExpression.php b/src/Compile/PrintExpressionCompiler.php similarity index 83% rename from src/Compile/Tag/PrivatePrintExpression.php rename to src/Compile/PrintExpressionCompiler.php index 471f6487..657a4899 100644 --- a/src/Compile/Tag/PrivatePrintExpression.php +++ b/src/Compile/PrintExpressionCompiler.php @@ -8,7 +8,9 @@ * @author Uwe Tews */ -namespace Smarty\Compile\Tag; +namespace Smarty\Compile; + +use Smarty\Compile\Tag\Base; /** * Smarty Internal Plugin Compile Print Expression Class @@ -16,13 +18,13 @@ namespace Smarty\Compile\Tag; * @package Smarty * @subpackage Compiler */ -class PrivatePrintExpression extends Base { +class PrintExpressionCompiler extends Base { /** * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $optional_attributes = ['assign']; @@ -30,7 +32,7 @@ class PrivatePrintExpression extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ protected $option_flags = ['nocache', 'nofilter']; @@ -45,19 +47,15 @@ class PrivatePrintExpression extends Base { * @throws \Smarty\Exception */ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { + + $compiler->has_code = true; + // check and get attributes $_attr = $this->getAttributes($compiler, $args); $output = $parameter['value']; // tag modifier if (!empty($parameter['modifierlist'])) { - $output = $compiler->compileTag( - 'private_modifier', - [], - [ - 'modifierlist' => $parameter['modifierlist'], - 'value' => $output, - ] - ); + $output = $compiler->compileModifier($parameter['modifierlist'], $output); } if (isset($_attr['assign'])) { // assign output to variable @@ -83,14 +81,7 @@ class PrivatePrintExpression extends Base { } $compiler->default_modifier_list = $modifierlist; } - $output = $compiler->compileTag( - 'private_modifier', - [], - [ - 'modifierlist' => $compiler->default_modifier_list, - 'value' => $output, - ] - ); + $output = $compiler->compileModifier($compiler->default_modifier_list, $output); } // autoescape html if ($compiler->template->smarty->escape_html) { @@ -111,11 +102,13 @@ class PrivatePrintExpression extends Base { } } foreach ($compiler->variable_filters as $filter) { - $output = $compiler->compileTag( - 'private_modifier', - [], - ['modifierlist' => [$filter], 'value' => $output] - ); + if (count($filter) === 1 + && ($result = $this->compile_variable_filter($compiler, $filter[0], $output)) !== false + ) { + $output = $result; + } else { + $output = $compiler->compileModifier([$filter], $output); + } } } $output = "\n"; diff --git a/src/Compile/Tag/PrivateSpecialVariable.php b/src/Compile/SpecialVariableCompiler.php similarity index 89% rename from src/Compile/Tag/PrivateSpecialVariable.php rename to src/Compile/SpecialVariableCompiler.php index b8b4e028..72de08c1 100644 --- a/src/Compile/Tag/PrivateSpecialVariable.php +++ b/src/Compile/SpecialVariableCompiler.php @@ -8,7 +8,14 @@ * @author Uwe Tews */ -namespace Smarty\Compile\Tag; +namespace Smarty\Compile; + +use Smarty\Compile\Tag\Base; +use Smarty\Compile\Tag\Capture; +use Smarty\Compile\Tag\ForeachTag; +use Smarty\Compile\Tag\Section; +use Smarty\Compiler\Template; +use Smarty\CompilerException; /** * Smarty Internal Plugin Compile special Smarty Variable Class @@ -16,19 +23,24 @@ namespace Smarty\Compile\Tag; * @package Smarty * @subpackage Compiler */ -class PrivateSpecialVariable extends Base { +class SpecialVariableCompiler extends Base { /** * Compiles code for the special $smarty variables * * @param array $args array with attributes from parser - * @param \Smarty\Compiler\Template $compiler compiler object - * @param $parameter + * @param Template $compiler compiler object + * @param array $parameter + * @param null $tag + * @param null $function * * @return string compiled code - * @throws \Smarty\CompilerException + * @throws CompilerException */ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { + + $compiler->has_code = true; + $_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2)); $variable = smarty_strtolower_ascii($compiler->getId($_index[0])); if ($variable === false) { diff --git a/src/Compile/Tag/BCPluginWrapper.php b/src/Compile/Tag/BCPluginWrapper.php index 751772d4..a34d0bea 100644 --- a/src/Compile/Tag/BCPluginWrapper.php +++ b/src/Compile/Tag/BCPluginWrapper.php @@ -6,8 +6,9 @@ class BCPluginWrapper extends Base { private $callback; - public function __construct($callback) { + public function __construct($callback, bool $cacheable = true) { $this->callback = $callback; + $this->cacheable = $cacheable; } /** diff --git a/src/Compile/Tag/Base.php b/src/Compile/Tag/Base.php index ae7efc9e..afb75533 100644 --- a/src/Compile/Tag/Base.php +++ b/src/Compile/Tag/Base.php @@ -40,6 +40,10 @@ abstract class Base implements TagCompilerInterface { * @var array */ protected $option_flags = ['nocache']; + /** + * @var bool + */ + protected $cacheable = true; /** * Mapping array for boolean option value @@ -55,6 +59,10 @@ abstract class Base implements TagCompilerInterface { */ protected $mapCache = []; + public function isCacheable(): bool { + return $this->cacheable; + } + /** * This function checks if the attributes passed are valid * The attributes passed for the tag to compile are checked against the list of required and @@ -201,7 +209,7 @@ abstract class Base implements TagCompilerInterface { * @param \Smarty\Compiler\Template $compiler compiler object * @param array $parameter array with compilation parameter * - * @return string compiled code + * @return bool|string compiled code or true if no code has been compiled * @throws \Smarty\CompilerException */ abstract public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null); diff --git a/src/Compile/Tag/BlockClose.php b/src/Compile/Tag/BlockClose.php index bd0af275..94579ac7 100644 --- a/src/Compile/Tag/BlockClose.php +++ b/src/Compile/Tag/BlockClose.php @@ -39,7 +39,7 @@ class BlockClose extends Inheritance { $compiler->parser->current_buffer = new Template(); $output = "cStyleComment(" {block {$_name}} ") . "\n"; - $output .= "class {$_className} extends \\Smarty\\Block\n"; + $output .= "class {$_className} extends \\Smarty\\Runtime\\Block\n"; $output .= "{\n"; foreach ($_block as $property => $value) { $output .= "public \${$property} = " . var_export($value, true) . ";\n"; diff --git a/src/Compile/Tag/Call.php b/src/Compile/Tag/Call.php index eaf31014..00c42ced 100644 --- a/src/Compile/Tag/Call.php +++ b/src/Compile/Tag/Call.php @@ -22,7 +22,7 @@ class Call extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $required_attributes = ['name']; @@ -30,7 +30,7 @@ class Call extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $shorttag_order = ['name']; @@ -38,7 +38,7 @@ class Call extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $optional_attributes = ['_any']; diff --git a/src/Compile/Tag/EvalTag.php b/src/Compile/Tag/EvalTag.php index 98fd5722..fca6b6f4 100644 --- a/src/Compile/Tag/EvalTag.php +++ b/src/Compile/Tag/EvalTag.php @@ -22,7 +22,7 @@ class EvalTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $required_attributes = ['var']; @@ -30,7 +30,7 @@ class EvalTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $optional_attributes = ['assign']; @@ -38,7 +38,7 @@ class EvalTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ public $shorttag_order = ['var', 'assign']; @@ -59,7 +59,7 @@ class EvalTag extends Base { } // create template object $_output = - "\$_template = new {$compiler->smarty->template_class}('eval:'.{$_attr[ 'var' ]}, \$_smarty_tpl->smarty, \$_smarty_tpl);"; + "\$_template = new \\Smarty\\Template('eval:'.{$_attr[ 'var' ]}, \$_smarty_tpl->smarty, \$_smarty_tpl);"; //was there an assign attribute? if (isset($_assign)) { $_output .= "\$_smarty_tpl->assign($_assign,\$_template->fetch());"; diff --git a/src/Compile/Tag/ForeachSection.php b/src/Compile/Tag/ForeachSection.php index b419fe9c..f127b4b9 100644 --- a/src/Compile/Tag/ForeachSection.php +++ b/src/Compile/Tag/ForeachSection.php @@ -172,13 +172,7 @@ abstract class ForeachSection extends Base { $_content = $nextCompiler->template->source->getContent(); if ($_content !== '') { // run pre filter if required - if (isset($nextCompiler->smarty->registered_filters['pre'])) { - $_content = $nextCompiler->smarty->runFilter( - 'pre', - $_content, - $nextCompiler->template - ); - } + $_content = $nextCompiler->smarty->runPreFilters($_content, $nextCompiler->template); $this->matchProperty($_content); } } diff --git a/src/Compile/Tag/IncludeTag.php b/src/Compile/Tag/IncludeTag.php index 26167000..60e29098 100644 --- a/src/Compile/Tag/IncludeTag.php +++ b/src/Compile/Tag/IncludeTag.php @@ -31,7 +31,7 @@ class IncludeTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ protected $required_attributes = ['file']; @@ -39,7 +39,7 @@ class IncludeTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ protected $shorttag_order = ['file']; @@ -47,7 +47,7 @@ class IncludeTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ protected $option_flags = ['nocache', 'inline', 'caching']; @@ -55,7 +55,7 @@ class IncludeTag extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ protected $optional_attributes = ['_any']; @@ -206,7 +206,7 @@ class IncludeTag extends Base { $t_hash = sha1($c_id . ($_caching ? '--caching' : '--nocaching')); $compiler->smarty->allow_ambiguous_resources = true; /* @var \Smarty\Template $tpl */ - $tpl = new $compiler->smarty->template_class( + $tpl = new \Smarty\Template( trim($fullResourceName, '"\''), $compiler->smarty, $compiler->template, @@ -328,7 +328,7 @@ class IncludeTag extends Base { $compiled_code .= "?>\n" . $tpl->compiler->compileTemplateSource($tpl, null, $compiler->parent_compiler); $compiled_code .= "\n"; - $compiled_code .= $tpl->compiler->postFilter($tpl->compiler->blockOrFunctionCode); + $compiled_code .= $tpl->smarty->runPostFilters($tpl->compiler->blockOrFunctionCode, $tpl); $compiled_code .= "cStyleComment(" End inline template \"{$sourceInfo}\" =============================") . "\n"; $compiled_code .= '?>'; diff --git a/src/Compile/Tag/MakeNocache.php b/src/Compile/Tag/MakeNocache.php index ad7ba804..29ce01db 100644 --- a/src/Compile/Tag/MakeNocache.php +++ b/src/Compile/Tag/MakeNocache.php @@ -22,7 +22,7 @@ class MakeNocache extends Base { * Attribute definition: Overwrites base class. * * @var array - * @see Base + * @see BaseCompiler */ protected $option_flags = []; diff --git a/src/Compile/Tag/NocacheClose.php b/src/Compile/Tag/NocacheClose.php index a5b75d95..b5712ed1 100644 --- a/src/Compile/Tag/NocacheClose.php +++ b/src/Compile/Tag/NocacheClose.php @@ -33,6 +33,7 @@ class NocacheClose extends Base { [$compiler->nocache] = $this->closeTag($compiler, ['nocache']); // this tag does not return compiled code $compiler->has_code = false; + echo "has_code set to false in " . __METHOD__ . "\n"; return true; } } diff --git a/src/Compile/Tag/PrivateFunctionPlugin.php b/src/Compile/Tag/PrivateFunctionPlugin.php deleted file mode 100644 index 222398e0..00000000 --- a/src/Compile/Tag/PrivateFunctionPlugin.php +++ /dev/null @@ -1,79 +0,0 @@ -getAttributes($compiler, $args); - unset($_attr['nocache']); - // convert attributes into parameter array string - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } - $_params = 'array(' . implode(',', $_paramsArray) . ')'; - // compile code - $output = "{$function}({$_params},\$_smarty_tpl)"; - if (!empty($parameter['modifierlist'])) { - $output = $compiler->compileTag( - 'private_modifier', - [], - [ - 'modifierlist' => $parameter['modifierlist'], - 'value' => $output, - ] - ); - } - $output = "\n"; - return $output; - } -} diff --git a/src/Compile/Tag/PrivateModifier.php b/src/Compile/Tag/PrivateModifier.php deleted file mode 100644 index e7fd3990..00000000 --- a/src/Compile/Tag/PrivateModifier.php +++ /dev/null @@ -1,134 +0,0 @@ -getAttributes($compiler, $args); - $output = $parameter['value']; - // loop over list of modifiers - foreach ($parameter['modifierlist'] as $single_modifier) { - /* @var string $modifier */ - $modifier = $single_modifier[0]; - $single_modifier[0] = $output; - $params = implode(',', $single_modifier); - // check if we know already the type of modifier - if (isset($compiler->known_modifier_type[$modifier])) { - $modifier_types = [$compiler->known_modifier_type[$modifier]]; - } else { - $modifier_types = [1, 2, 3, 4, 6]; - } - foreach ($modifier_types as $type) { - switch ($type) { - case 1: - // registered modifier - if (isset($compiler->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIER][$modifier])) { - if (is_callable($compiler->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIER][$modifier][0])) { - $output = - sprintf( - 'call_user_func_array($_smarty_tpl->registered_plugins[ \'%s\' ][ %s ][ 0 ], array( %s ))', - \Smarty\Smarty::PLUGIN_MODIFIER, - var_export($modifier, true), - $params - ); - $compiler->known_modifier_type[$modifier] = $type; - break 2; - } - } - break; - case 2: - // registered modifier compiler - if (isset($compiler->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0])) { - $output = - call_user_func( - $compiler->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0], - $single_modifier, - $compiler->smarty - ); - $compiler->known_modifier_type[$modifier] = $type; - break 2; - } - break; - case 3: - // modifiercompiler plugin - // check if modifier allowed - if ($compiler->getModifierCompiler($modifier)) { - $output = $compiler->compileModifier($modifier, $single_modifier); - } - $compiler->known_modifier_type[$modifier] = $type; - break 2; - case 4: - // modifier plugin - if ($function = $compiler->getPlugin($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) { - // check if modifier allowed - if (!is_object($compiler->smarty->security_policy) - || $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler) - ) { - $output = "{$function}({$params})"; - } - $compiler->known_modifier_type[$modifier] = $type; - break 2; - } - break; - // Case 5 was a direct call to a callable (usually PHP function). - // This was removed in Smarty v5 after being deprecated in 4.3. - case 6: - // default plugin handler - if (isset($compiler->default_handler_plugins[\Smarty\Smarty::PLUGIN_MODIFIER][$modifier]) - || (is_callable($compiler->smarty->default_plugin_handler_func) - && $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) - ) { - $function = $compiler->default_handler_plugins[\Smarty\Smarty::PLUGIN_MODIFIER][$modifier][0]; - // check if modifier allowed - if (!is_object($compiler->smarty->security_policy) - || $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler) - ) { - if (!is_array($function)) { - $output = "{$function}({$params})"; - } else { - if (is_object($function[0])) { - $output = $function[0] . '->' . $function[1] . '(' . $params . ')'; - } else { - $output = $function[0] . '::' . $function[1] . '(' . $params . ')'; - } - } - } - $compiler->known_modifier_type[$modifier] = $type; - break 2; - } - } - } - if (!isset($compiler->known_modifier_type[$modifier])) { - $compiler->trigger_template_error("unknown modifier '{$modifier}'", null, true); - } - } - return $output; - } -} diff --git a/src/Compile/Tag/TagCompilerInterface.php b/src/Compile/Tag/TagCompilerInterface.php index f4b0a37f..da27bfe4 100644 --- a/src/Compile/Tag/TagCompilerInterface.php +++ b/src/Compile/Tag/TagCompilerInterface.php @@ -17,8 +17,10 @@ interface TagCompilerInterface { * @param \Smarty\Compiler\Template $compiler compiler object * @param array $parameter array with compilation parameter * - * @return string compiled code + * @return bool|string compiled code or true if no code has been compiled * @throws \Smarty\CompilerException */ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null); + + public function isCacheable(): bool; } \ No newline at end of file diff --git a/src/Compiler/BaseCompiler.php b/src/Compiler/BaseCompiler.php new file mode 100644 index 00000000..84963ea3 --- /dev/null +++ b/src/Compiler/BaseCompiler.php @@ -0,0 +1,23 @@ +smarty; + } + +} \ No newline at end of file diff --git a/src/Compiler/CodeFrame.php b/src/Compiler/CodeFrame.php index 9d4cb1eb..4d9ae81a 100644 --- a/src/Compiler/CodeFrame.php +++ b/src/Compiler/CodeFrame.php @@ -58,7 +58,7 @@ class CodeFrame str_replace('*/', '* /', $this->_template->source->filepath) ); $output .= "/* @var \\Smarty\\Template \$_smarty_tpl */\n"; - $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' . + $dec = "\$_smarty_tpl->isFresh(\$_smarty_tpl, " . var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ')'; $output .= "if ({$dec}) {\n"; $output .= "function {$properties['unifunc']} (\\Smarty\\Template \$_smarty_tpl) {\n"; diff --git a/src/Compiler/Configfile.php b/src/Compiler/Configfile.php index 0ede6a86..91cefb2d 100644 --- a/src/Compiler/Configfile.php +++ b/src/Compiler/Configfile.php @@ -10,9 +10,9 @@ */ namespace Smarty\Compiler; -use Smarty\Smarty; use Smarty\Lexer\ConfigfileLexer; use Smarty\Parser\ConfigfileParser; +use Smarty\Smarty; use Smarty\Template; use Smarty\CompilerException; @@ -22,33 +22,19 @@ use Smarty\CompilerException; * @package Smarty * @subpackage Config */ -class Configfile { - - /** - * Lexer class name - * - * @var string - */ - public $lexer_class; - - /** - * Parser class name - * - * @var string - */ - public $parser_class; +class Configfile extends BaseCompiler { /** * Lexer object * - * @var object + * @var ConfigfileLexer */ public $lex; /** * Parser object * - * @var object + * @var ConfigfileParser */ public $parser; @@ -76,15 +62,11 @@ class Configfile { /** * Initialize compiler * - * @param string $lexer_class class name - * @param string $parser_class class name * @param Smarty $smarty global instance */ - public function __construct($lexer_class, $parser_class, Smarty $smarty) { + public function __construct(Smarty $smarty) { $this->smarty = $smarty; // get required plugins - $this->lexer_class = $lexer_class; - $this->parser_class = $parser_class; $this->smarty = $smarty; $this->config_data['sections'] = []; $this->config_data['vars'] = []; @@ -113,8 +95,8 @@ class Configfile { $this->smarty->_debug->start_compile($this->template); } // init the lexer/parser to compile the config file - /* @var \Smarty\Lexer\ConfigfileLexer $this->lex */ - $this->lex = new $this->lexer_class( + /* @var ConfigfileLexer $this->lex */ + $this->lex = new ConfigfileLexer( str_replace( [ "\r\n", @@ -125,8 +107,8 @@ class Configfile { ) . "\n", $this ); - /* @var \Smarty\Parser\ConfigfileParser $this->parser */ - $this->parser = new $this->parser_class($this->lex, $this); + /* @var ConfigfileParser $this->parser */ + $this->parser = new ConfigfileParser($this->lex, $this); if (function_exists('mb_internal_encoding') && function_exists('ini_get') && ((int)ini_get('mbstring.func_overload')) & 2 @@ -142,7 +124,7 @@ class Configfile { // get tokens from lexer and parse them while ($this->lex->yylex()) { if ($this->smarty->_parserdebug) { - echo "
Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n"; + echo "Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n"; } $this->parser->doParse($this->lex->token, $this->lex->value); } diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index 9504fb81..7413850e 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -10,12 +10,21 @@ namespace Smarty\Compiler; -use Smarty\Smarty; -use Smarty\Compile\Tag\Base; -use Smarty\Compile\Tag\ExtendsTag; +use Smarty\Compile\BlockCompiler; +use Smarty\Compile\ModifierCompiler; +use Smarty\Compile\ObjectMethodBlockCompiler; +use Smarty\Compile\ObjectMethodCallCompiler; +use Smarty\Compile\FunctionCallCompiler; +use Smarty\Lexer\TemplateLexer; use Smarty\Parser\TemplateParser; +use Smarty\Smarty; +use Smarty\Compile\Tag\ExtendsTag; use Smarty\CompilerException; use Smarty\Exception; +use function array_merge; +use function is_array; +use function strlen; +use function substr; /** * Class SmartyTemplateCompiler @@ -23,7 +32,7 @@ use Smarty\Exception; * @package Smarty * @subpackage Compiler */ -class Template { +class Template extends BaseCompiler { /** * counter for prefix variable number @@ -32,13 +41,6 @@ class Template { */ public static $prefixVariableNumber = 0; - /** - * Smarty object - * - * @var Smarty - */ - public $smarty = null; - /** * Parser object * @@ -123,13 +125,6 @@ class Template { */ public $trace_filepath = ''; - /** - * plugins loaded by default plugin handler - * - * @var array - */ - public $default_handler_plugins = []; - /** * saved preprocessed modifier list * @@ -165,13 +160,6 @@ class Template { */ public $modifier_plugins = []; - /** - * type of already compiled modifier - * - * @var array - */ - public $known_modifier_type = []; - /** * parent compiler object for merged subtemplates and template functions * @@ -250,16 +238,11 @@ class Template { public $stripRegEx = '![\t ]*[\r\n]+[\t ]*!'; /** - * plugin search order + * General storage area for tag compiler plugins * * @var array */ - public $plugin_search_order = [ - 'function', - 'block', - 'compiler', - 'class', - ]; + public $_cache = array(); /** * Lexer preg pattern for left delimiter @@ -296,20 +279,6 @@ class Template { */ private $literalPreg = ''; - /** - * Lexer class name - * - * @var string - */ - public $lexer_class; - - /** - * Parser class name - * - * @var string - */ - public $parser_class; - /** * array of callbacks called when the normal compile process of template is finished * @@ -334,11 +303,9 @@ class Template { /** * Initialize compiler * - * @param string $lexer_class class name - * @param string $parser_class class name * @param Smarty $smarty global instance */ - public function __construct($lexer_class, $parser_class, Smarty $smarty) { + public function __construct(Smarty $smarty) { $this->smarty = $smarty; $this->nocache_hash = str_replace( [ @@ -348,9 +315,6 @@ class Template { '_', uniqid(mt_rand(), true) ); - // get required plugins - $this->lexer_class = $lexer_class; - $this->parser_class = $parser_class; } /** @@ -375,7 +339,7 @@ class Template { $nocache, $parent_compiler ), - $this->postFilter($this->blockOrFunctionCode) . + $this->smarty->runPostFilters($this->blockOrFunctionCode, $this->template) . join('', $this->mergedSubTemplatesCode), false, $this @@ -442,8 +406,14 @@ class Template { // get template source $_content = $this->template->source->getContent(); } - $_compiled_code = $this->postFilter($this->doCompile($this->preFilter($_content), true)); - } catch (Exception $e) { + $_compiled_code = $this->smarty->runPostFilters( + $this->doCompile( + $this->smarty->runPreFilters($_content, $this->template), + true + ), + $this->template + ); + } catch (\Exception $e) { if ($this->smarty->debugging) { $this->smarty->_debug->end_compile($this->template); } @@ -462,40 +432,6 @@ class Template { return $_compiled_code; } - /** - * Optionally process compiled code by post filter - * - * @param string $code compiled code - * - * @return string - * @throws \Smarty\Exception - */ - public function postFilter($code) { - // run post filter if on code - if (!empty($code) && isset($this->smarty->registered_filters['post'])) { - return $this->smarty->runFilter('post', $code, $this->template); - } else { - return $code; - } - } - - /** - * Run optional prefilter - * - * @param string $_content template source - * - * @return string - * @throws \Smarty\Exception - */ - public function preFilter($_content) { - // run pre filter if required - if ($_content !== '' && isset($this->smarty->registered_filters['pre'])) { - return $this->smarty->runFilter('pre', $_content, $this->template); - } else { - return $_content; - } - } - /** * Compile Tag * This is a call back from the lexer/parser @@ -521,10 +457,18 @@ class Template { return $result; } - public function compileModifier($modifier, $args) { - if ($modifierCompiler = $this->getModifierCompiler($modifier)) { - return $modifierCompiler->compile($args, $this); - } + /** + * Compiles code for modifier execution + * + * @param $modifierlist + * @param $value + * + * @return string compiled code + * @throws CompilerException + * @throws Exception + */ + public function compileModifier($modifierlist, $value) { + return (new ModifierCompiler())->compile([], $this, ['modifierlist' => $modifierlist, 'value' => $value]); } /** @@ -697,27 +641,6 @@ class Template { return $text; } - /** - * lazy loads internal compile plugin for tag and calls the compile method - * compile objects cached for reuse. - * class name format: \Smarty\Compile\TagName - * - * @param string $tag tag name - * @param array $args list of tag attributes - * @param mixed $param1 optional parameter - * @param mixed $param2 optional parameter - * @param mixed $param3 optional parameter - * - * @return bool|string compiled code or false - * @throws \Smarty\CompilerException - */ - private function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) { - /* @var Base $tagCompiler */ - $tagCompiler = $this->getTagCompiler($tag); - // compile this tag - return $tagCompiler === false ? false : $tagCompiler->compile($args, $this, $param1, $param2, $param3); - } - /** * lazy loads internal compile plugin for tag compile objects cached for reuse. * @@ -725,21 +648,21 @@ class Template { * * @param string $tag tag name * - * @return bool|\Smarty\Compile\Tag\TagCompilerInterface tag compiler object or false if not found or untrusted by security policy + * @return ?\Smarty\Compile\Tag\TagCompilerInterface tag compiler object or null if not found or untrusted by security policy */ - public function getTagCompiler($tag) { + public function getTagCompiler($tag): ?\Smarty\Compile\Tag\TagCompilerInterface { if (isset($this->smarty->security_policy) && !$this->smarty->security_policy->isTrustedTag($tag, $this)) { - return false; + return null; } foreach ($this->smarty->getExtensions() as $extension) { - if ($tag = $extension->getTagCompiler($tag)) { - return $tag; + if ($compiler = $extension->getTagCompiler($tag)) { + return $compiler; } } - return false; + return null; } /** @@ -764,42 +687,34 @@ class Template { return false; } - /** - * Check for plugins and return function name - * - * @param $plugin_name - * @param string $plugin_type type of plugin - * - * @return string call name of function - */ - public function getPlugin($plugin_name, $plugin_type) { - // loop through plugin dirs and find the plugin - $function = 'smarty_' . $plugin_type . '_' . $plugin_name; - if ($plugin_type === 'modifier') { - $this->modifier_plugins[$plugin_name] = true; - } - return $function; - } - /** * Check for plugins by default plugin handler * * @param string $tag name of tag * @param string $plugin_type type of plugin * - * @return bool true if found + * @return callback|null * @throws \Smarty\CompilerException */ - public function getPluginFromDefaultHandler($tag, $plugin_type) { + public function getPluginFromDefaultHandler($tag, $plugin_type): ?callback { + + $defaultPluginHandlerFunc = $this->smarty->getDefaultPluginHandlerFunc(); + + if (!is_callable($defaultPluginHandlerFunc)) { + return null; + } + + $callback = null; $script = null; $cacheable = true; + $result = call_user_func_array( - $this->smarty->default_plugin_handler_func, + $defaultPluginHandlerFunc, [ $tag, $plugin_type, - $this->template, + null, // This used to pass $this->template, but this parameter has been removed in 5.0 &$callback, &$script, &$cacheable, @@ -815,17 +730,12 @@ class Template { } } if (is_callable($callback)) { - $this->default_handler_plugins[$plugin_type][$tag] = [ - $callback, - true, - [], - ]; - return true; + return $callback; } else { $this->trigger_template_error("Default plugin handler: Returned callback for '{$tag}' not callable"); } } - return false; + return null; } /** @@ -1269,6 +1179,23 @@ class Template { return '/*' . str_replace('*/', '* /', $string) . '*/'; } + private function argsContainNocache(array $args): bool { + foreach ($args as $arg) { + if (!is_array($arg)) { + if ($arg === "'nocache'" || $arg === 'nocache') { + return true; + } + } else { + foreach ($arg as $k => $v) { + if (($k === "'nocache'" || $k === 'nocache') && (trim($v, "'\" ") === 'true')) { + return true; + } + } + } + } + return false; + } + /** * Compile Tag * @@ -1281,284 +1208,97 @@ class Template { * @throws CompilerException */ private function compileTag2($tag, $args, $parameter) { - $plugin_type = ''; // $args contains the attributes parsed and compiled by the lexer/parser // assume that tag does compile into code, but creates no HTML output $this->has_code = true; // check nocache option flag - foreach ($args as $arg) { - if (!is_array($arg)) { - if ($arg === "'nocache'" || $arg === 'nocache') { - $this->tag_nocache = true; - } - } else { - foreach ($arg as $k => $v) { - if (($k === "'nocache'" || $k === 'nocache') && (trim($v, "'\" ") === 'true')) { - $this->tag_nocache = true; - } - } + if ($this->argsContainNocache($args)) { + $this->tag_nocache = true; + } + + // compile built-in tags + if ($tagCompiler = $this->getTagCompiler($tag)) { + if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) { + $this->tag_nocache = !$tagCompiler->isCacheable(); + $_output = $tagCompiler->compile($args, $this, $parameter); + return $this->has_code ? $_output : null; } } - // compile the smarty tag (required compile classes to compile the tag are auto loaded) - if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) { - if (isset($this->parent_compiler->tpl_function[$tag]) - || ($this->template->smarty->hasRuntime('TplFunction') - && $this->template->smarty->getRuntime('TplFunction')->getTplFunction($this->template, $tag) !== false) - ) { - // template defined by {template} tag - $args['_attr']['name'] = "'{$tag}'"; - $_output = $this->callTagCompiler('call', $args, $parameter); + + // template defined by {template} tag + if ($this->canCompileTemplateCall($tag)) { + $args['_attr']['name'] = "'{$tag}'"; + $tagCompiler = $this->getTagCompiler($tag); + // compile this tag + $_output = $tagCompiler === null ? false : $tagCompiler->compile($args, $this, $parameter); + return $this->has_code ? $_output : null; + } + +// @TODO: This code was in here in v4, but it might not do anything (anymore) +// foreach ($args['_attr'] ?? [] as $attribute) { +// if (is_array($attribute)) { +// $args = array_merge($args, $attribute); +// } +// } + + // remaining tastes: (object-)function, (object-function-)block, custom-compiler + // opening and closing tags for these are handled with the same handler + $base_tag = $this->getBaseTag($tag); + + // check if tag is a registered object + if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_method'])) { + return $this->compileRegisteredObjectMethodCall($base_tag, $args, $parameter, $tag); + } + + // check if tag is a function + if ($this->smarty->getFunctionHandler($base_tag)) { + if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($base_tag, $this)) { + $compiler = new FunctionCallCompiler(); + return $compiler->compile($args, $this, $parameter, $tag, $base_tag); } } - if ($_output !== false) { - if ($_output !== true) { - // did we get compiled code - if ($this->has_code) { - // return compiled code - return $_output; + + // check if tag is a block + if ($this->smarty->getBlockHandler($base_tag)) { + if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($base_tag, $this)) { + $compiler = new BlockCompiler(); + return $compiler->compile($args, $this, $parameter, $tag, $base_tag); + } + } + + // the default plugin handler is a handler of last resort, it may also handle not specifically registered tags. + if ($callback = $this->getPluginFromDefaultHandler($tag, Smarty::PLUGIN_COMPILER)) { + $tagCompiler = new \Smarty\Compile\Tag\BCPluginWrapper($callback); + $new_args = []; + foreach ($args as $key => $mixed) { + if (is_array($mixed)) { + $new_args = array_merge($new_args, $mixed); + } else { + $new_args[$key] = $mixed; } } - // tag did not produce compiled code - return null; + return $tagCompiler->compile($new_args, $this, $parameter); + } + + if ($this->getPluginFromDefaultHandler($tag, Smarty::PLUGIN_FUNCTION)) { + $compiler = new FunctionCallCompiler(); + return $compiler->compile($args, $this, $parameter, $tag, $base_tag); + } + + if ($this->getPluginFromDefaultHandler($tag, Smarty::PLUGIN_BLOCK)) { + $compiler = new BlockCompiler(); + return $compiler->compile($args, $this, $parameter, $tag, $base_tag); + } + + $this->trigger_template_error("unknown tag '{$tag}'", null, true); + } + + private function getBaseTag($tag) { + if (strlen($tag) < 6 || substr($tag, -5) !== 'close') { + return $tag; } else { - // map_named attributes - if (isset($args['_attr'])) { - foreach ($args['_attr'] as $key => $attribute) { - if (is_array($attribute)) { - $args = array_merge($args, $attribute); - } - } - } - // not an internal compiler tag - if (strlen($tag) < 6 || substr($tag, -5) !== 'close') { - // check if tag is a registered object - if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_method'])) { - $method = $parameter['object_method']; - if (!in_array($method, $this->smarty->registered_objects[$tag][3]) - && (empty($this->smarty->registered_objects[$tag][1]) - || in_array($method, $this->smarty->registered_objects[$tag][1])) - ) { - return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $method); - } elseif (in_array($method, $this->smarty->registered_objects[$tag][3])) { - return $this->callTagCompiler( - 'private_object_block_function', - $args, - $parameter, - $tag, - $method - ); - } else { - // throw exception - $this->trigger_template_error( - 'not allowed method "' . $method . '" in registered object "' . - $tag . '"', - null, - true - ); - } - } - // check if tag is registered - foreach ([ - \Smarty\Smarty::PLUGIN_COMPILER, - \Smarty\Smarty::PLUGIN_FUNCTION, - \Smarty\Smarty::PLUGIN_BLOCK, - ] as $plugin_type) { - if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) { - // if compiler function plugin call it now - if ($plugin_type === \Smarty\Smarty::PLUGIN_COMPILER) { - $new_args = []; - foreach ($args as $key => $mixed) { - if (is_array($mixed)) { - $new_args = array_merge($new_args, $mixed); - } else { - $new_args[$key] = $mixed; - } - } - if (!$this->smarty->registered_plugins[$plugin_type][$tag][1]) { - $this->tag_nocache = true; - } - return call_user_func_array( - $this->smarty->registered_plugins[$plugin_type][$tag][0], - [ - $new_args, - $this, - ] - ); - } - // compile registered function or block function - if ($plugin_type === \Smarty\Smarty::PLUGIN_FUNCTION || $plugin_type === \Smarty\Smarty::PLUGIN_BLOCK) { - return $this->callTagCompiler( - 'private_registered_' . $plugin_type, - $args, - $parameter, - $tag - ); - } - } - } - // check plugins from plugins folder - foreach ($this->plugin_search_order as $plugin_type) { - if ($plugin_type === \Smarty\Smarty::PLUGIN_COMPILER - && (!isset($this->smarty->security_policy) - || $this->smarty->security_policy->isTrustedTag($tag, $this)) - ) { - $plugin = 'smarty_compiler_' . $tag; - if (is_callable($plugin)) { - // convert arguments format for old compiler plugins - $new_args = []; - foreach ($args as $key => $mixed) { - if (is_array($mixed)) { - $new_args = array_merge($new_args, $mixed); - } else { - $new_args[$key] = $mixed; - } - } - return $plugin($new_args, $this->smarty); - } - if (class_exists($plugin, false)) { - $plugin_object = new $plugin; - if (method_exists($plugin_object, 'compile')) { - return $plugin_object->compile($args, $this); - } - } - throw new Exception("Plugin '{$tag}' not callable"); - } else { - if ($function = $this->getPlugin($tag, $plugin_type)) { - if (!isset($this->smarty->security_policy) - || $this->smarty->security_policy->isTrustedTag($tag, $this) - ) { - return $this->callTagCompiler( - 'private_' . $plugin_type . '_plugin', - $args, - $parameter, - $tag, - $function - ); - } - } - } - } - if (is_callable($this->smarty->default_plugin_handler_func)) { - $found = false; - // look for already resolved tags - foreach ($this->plugin_search_order as $plugin_type) { - if (isset($this->default_handler_plugins[$plugin_type][$tag])) { - $found = true; - break; - } - } - if (!$found) { - // call default handler - foreach ($this->plugin_search_order as $plugin_type) { - if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) { - $found = true; - break; - } - } - } - if ($found) { - // if compiler function plugin call it now - if ($plugin_type === \Smarty\Smarty::PLUGIN_COMPILER) { - $new_args = []; - foreach ($args as $key => $mixed) { - if (is_array($mixed)) { - $new_args = array_merge($new_args, $mixed); - } else { - $new_args[$key] = $mixed; - } - } - return call_user_func_array( - $this->default_handler_plugins[$plugin_type][$tag][0], - [ - $new_args, - $this, - ] - ); - } else { - return $this->callTagCompiler( - 'private_registered_' . $plugin_type, - $args, - $parameter, - $tag - ); - } - } - } - } else { - // compile closing tag of block function - $base_tag = substr($tag, 0, -5); - // check if closing tag is a registered object - if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_method'])) { - $method = $parameter['object_method']; - if (in_array($method, $this->smarty->registered_objects[$base_tag][3])) { - return $this->callTagCompiler( - 'private_object_block_function', - $args, - $parameter, - $tag, - $method - ); - } else { - // throw exception - $this->trigger_template_error( - 'not allowed closing tag method "' . $method . - '" in registered object "' . $base_tag . '"', - null, - true - ); - } - } - // registered block tag ? - if (isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_BLOCK][$base_tag]) - || isset($this->default_handler_plugins[\Smarty\Smarty::PLUGIN_BLOCK][$base_tag]) - ) { - return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag); - } - // registered function tag ? - if (isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_FUNCTION][$tag])) { - return $this->callTagCompiler('private_registered_function', $args, $parameter, $tag); - } - // block plugin? - if ($function = $this->getPlugin($base_tag, \Smarty\Smarty::PLUGIN_BLOCK)) { - return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function); - } - // function plugin? - if ($function = $this->getPlugin($tag, \Smarty\Smarty::PLUGIN_FUNCTION)) { - if (!isset($this->smarty->security_policy) - || $this->smarty->security_policy->isTrustedTag($tag, $this) - ) { - return $this->callTagCompiler('private_function_plugin', $args, $parameter, $tag, $function); - } - } - // registered compiler plugin ? - if (isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_COMPILER][$tag])) { - // if compiler function plugin call it now - $args = []; - if (!$this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_COMPILER][$tag][1]) { - $this->tag_nocache = true; - } - return call_user_func_array( - $this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_COMPILER][$tag][0], - [ - $args, - $this, - ] - ); - } - $plugin = 'smarty_compiler_' . $tag; - if (is_callable($plugin)) { - return $plugin($args, $this->smarty); - } - if (class_exists($plugin, false)) { - $plugin_object = new $plugin; - if (method_exists($plugin_object, 'compile')) { - return $plugin_object->compile($args, $this); - } - } - throw new Exception("Plugin '{$tag}' not callable"); - } - $this->trigger_template_error("unknown tag '{$tag}'", null, true); + return substr($tag, 0, -5); } } @@ -1576,9 +1316,8 @@ class Template { tags in the templates are replaces with PHP code, then written to compiled files. */ // init the lexer/parser to compile the template - $this->parser = - new $this->parser_class( - new $this->lexer_class( + $this->parser = new TemplateParser( + new TemplateLexer( str_replace( [ "\r\n", @@ -1610,8 +1349,8 @@ class Template { // get tokens from lexer and parse them while ($this->parser->lex->yylex()) { if ($this->smarty->_parserdebug) { - echo "
Line {$this->parser->lex->line} Parsing  {$this->parser->yyTokenName[$this->parser->lex->token]} Token " .
-					htmlentities($this->parser->lex->value) . "
"; + echo "Line {$this->parser->lex->line} Parsing {$this->parser->yyTokenName[$this->parser->lex->token]} Token " . + $this->parser->lex->value; } $this->parser->doParse($this->parser->lex->token, $this->parser->lex->value); } @@ -1667,4 +1406,61 @@ class Template { public function unregisterPostCompileCallback($key) { unset($this->postCompileCallbacks[$key]); } + + /** + * @param string $tag + * + * @return bool + * @throws Exception + */ + private function canCompileTemplateCall(string $tag): bool { + return + isset($this->parent_compiler->tpl_function[$tag]) + || ( + $this->template->smarty->hasRuntime('TplFunction') + && ($this->template->smarty->getRuntime('TplFunction')->getTplFunction($this->template, $tag) !== false) + ); + } + + /** + * @throws CompilerException + */ + private function compileRegisteredObjectMethodCall(string $base_tag, array $args, array $parameter, string $tag) { + + $method = $parameter['object_method']; + $allowedAsBlockFunction = in_array($method, $this->smarty->registered_objects[$base_tag][3]); + + if ($base_tag === $tag) { + // opening tag + + $allowedAsNormalFunction = empty($this->smarty->registered_objects[$base_tag][1]) + || in_array($method, $this->smarty->registered_objects[$base_tag][1]); + + if ($allowedAsBlockFunction) { + return (new ObjectMethodBlockCompiler())->compile($args, $this, $parameter, $tag, $method); + } elseif ($allowedAsNormalFunction) { + return (new ObjectMethodCallCompiler())->compile($args, $this, $parameter, $tag, $method); + } + + $this->trigger_template_error( + 'not allowed method "' . $method . '" in registered object "' . + $tag . '"', + null, + true + ); + } + + // closing tag + if ($allowedAsBlockFunction) { + return (new ObjectMethodBlockCompiler())->compile($args, $this, $parameter, $tag, $method); + } + + $this->trigger_template_error( + 'not allowed closing tag method "' . $method . + '" in registered object "' . $base_tag . '"', + null, + true + ); + } + } diff --git a/src/Data.php b/src/Data.php index 6f7c1137..a9d7e35e 100644 --- a/src/Data.php +++ b/src/Data.php @@ -31,13 +31,6 @@ abstract class Data */ public $_objType = 4; - /** - * name of class used for templates - * - * @var string - */ - public $template_class = 'Smarty\Template'; - /** * template variables * @@ -119,7 +112,7 @@ abstract class Data // $tpl_var is an array, ignore $value foreach ($tpl_var as $_key => $_val) { if ($_key !== '') { - $this->append($this, $_key, $_val, $merge, $nocache); + $this->append($_key, $_val, $merge, $nocache); } } } else { @@ -211,7 +204,7 @@ abstract class Data * * @return Data */ - public function assignByRef($tpl_var, &$value, $nocache) + public function assignByRef($tpl_var, &$value, $nocache = false) { if ($tpl_var !== '') { $this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache); @@ -439,7 +432,7 @@ abstract class Data */ public function configLoad($config_file, $sections = null) { - $this->_loadConfigfile($this, $config_file, $sections, null); + $this->_loadConfigfile($config_file, $sections, null); return $this; } diff --git a/src/Debug.php b/src/Debug.php index 4f483d12..cdeba945 100644 --- a/src/Debug.php +++ b/src/Debug.php @@ -200,7 +200,7 @@ class Debug extends Data $smarty = $obj->_getSmartyObj(); // create fresh instance of smarty for displaying the debug console // to avoid problems if the application did overload the Smarty class - $debObj = new \Smarty(); + $debObj = new \Smarty\Smarty(); // copy the working dirs from application $debObj->setCompileDir($smarty->getCompileDir()); $debObj->force_compile = false; @@ -211,9 +211,7 @@ class Debug extends Data $debObj->debugging = false; $debObj->debugging_ctrl = 'NONE'; $debObj->error_reporting = E_ALL & ~E_NOTICE; - $debObj->debug_tpl = - isset($smarty->debug_tpl) ? $smarty->debug_tpl : 'file:' . __DIR__ . '/../debug.tpl'; - $debObj->registered_plugins = array(); + $debObj->debug_tpl = $smarty->debug_tpl ?? 'file:' . __DIR__ . '/../debug.tpl'; $debObj->registered_resources = array(); $debObj->registered_filters = array(); $debObj->default_modifiers = array(); diff --git a/src/Extension/BCPluginsAdapter.php b/src/Extension/BCPluginsAdapter.php index 87ca99e5..95c9538f 100644 --- a/src/Extension/BCPluginsAdapter.php +++ b/src/Extension/BCPluginsAdapter.php @@ -2,7 +2,10 @@ namespace Smarty\Extension; -use Smarty\Compile\Tag\BCPluginWrapper; +use Smarty\BlockHandler\BlockPluginWrapper; +use Smarty\Compile\Modifier\BCPluginWrapper as ModifierCompilerPluginWrapper; +use Smarty\Compile\Tag\BCPluginWrapper as TagPluginWrapper; +use Smarty\FunctionHandler\BCPluginWrapper as FunctionPluginWrapper; class BCPluginsAdapter extends Base { @@ -15,13 +18,104 @@ class BCPluginsAdapter extends Base { $this->smarty = $smarty; } - //$smarty->registered_plugins[$type][$name] = [$callback, (bool)$cacheable, (array)$cache_attr]; + private function findPlugin($type, $name): ?array { + if (null !== $plugin = $this->smarty->getRegisteredPlugin($type, $name)) { + return $plugin; + } + + return null; + } + public function getTagCompiler(string $tag): ?\Smarty\Compile\Tag\TagCompilerInterface { - if (!isset($smarty->registered_plugins['compiler'][$tag])) { + + $plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_COMPILER, $tag); + if ($plugin === null) { return null; } - $callback = reset($smarty->registered_plugins['compiler'][$tag]); - return new BCPluginWrapper($callback); + $callback = $plugin[0]; + $cacheable = (bool) $plugin[1] ?? true; + return new TagPluginWrapper($callback, $cacheable); } + + public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface { + $plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, $functionName); + if ($plugin === null) { + return null; + } + $callback = $plugin[0]; + $cacheable = (bool) $plugin[1] ?? true; + $cache_attributes = (array) $plugin[2] ?? []; + + return new FunctionPluginWrapper($callback, $cacheable, $cache_attributes); + + } + + public function getBlockHandler(string $blockTagName): ?\Smarty\BlockHandler\BlockHandlerInterface { + $plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_BLOCK, $blockTagName); + if ($plugin === null) { + return null; + } + $callback = $plugin[0]; + + return new BlockPluginWrapper($callback); + } + + public function getModifierCallback(string $modifierName) { + + $plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, $modifierName); + if ($plugin === null) { + return null; + } + return $plugin[0]; + } + + public function getModifierCompiler(string $modifier): ?\Smarty\Compile\Modifier\ModifierCompilerInterface { + $plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_MODIFIERCOMPILER, $modifier); + if ($plugin === null) { + return null; + } + $callback = $plugin[0]; + + return new ModifierCompilerPluginWrapper($callback); + } + + public function loadPluginsFromDir(string $path) { + + foreach([ + 'function', + 'modifier', + 'block', + 'compiler', + 'prefilter', + 'postfilter', + 'outputfilter', + 'resource', + 'insert', + ] as $type) { + foreach (glob($path . $type . '.?*.php') as $filename) { + $pluginName = $this->getPluginNameFromFilename($filename); + if ($pluginName !== null) { + require_once $filename; + if (function_exists($functionName = 'smarty_' . $type . '_' . $pluginName)) { + $this->smarty->registerPlugin($type, $pluginName, $functionName, true, []); + } + } + } + } + + } + + /** + * @param $filename + * + * @return string|null + */ + private function getPluginNameFromFilename($filename) { + if (!preg_match('/.*\.([a-z_A-Z0-9]+)\.php$/',$filename,$matches)) { + return null; + } + return $matches[1]; + } + } \ No newline at end of file diff --git a/src/Extension/Base.php b/src/Extension/Base.php index b5fa73af..1b363e95 100644 --- a/src/Extension/Base.php +++ b/src/Extension/Base.php @@ -18,7 +18,7 @@ class Base implements ExtensionInterface { return null; } - public function getBlockHandler(string $functionName): ?\Smarty\FunctionHandler\BlockHandlerInterface { + public function getBlockHandler(string $blockTagName): ?\Smarty\BlockHandler\BlockHandlerInterface { return null; } @@ -26,6 +26,14 @@ class Base implements ExtensionInterface { return null; } + public function getPreFilters(): array { + return []; + } + + public function getPostFilters(): array { + return []; + } + public function getOutputFilters(): array { return []; } diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php new file mode 100644 index 00000000..17b2b403 --- /dev/null +++ b/src/Extension/CoreExtension.php @@ -0,0 +1,55 @@ + * @author Rodney Rehm */ - private function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false) + public function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false) { $string = (string) $string; @@ -239,7 +182,7 @@ class Core extends Base { * * @return int */ - private function smarty_modifier_count($arrayOrObject, $mode = 0) { + public function smarty_modifier_count($arrayOrObject, $mode = 0) { /* * @see https://www.php.net/count * > Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, @@ -275,7 +218,7 @@ class Core extends Base { * @return string |void * @uses smarty_make_timestamp() */ - private function smarty_modifier_date_format($string, $format = null, $default_date = '', $formatter = 'auto') + public function smarty_modifier_date_format($string, $format = null, $default_date = '', $formatter = 'auto') { if ($format === null) { $format = \Smarty\Smarty::$_DATE_FORMAT; @@ -341,7 +284,7 @@ class Core extends Base { * * @return string */ - private function smarty_modifier_debug_print_var($var, $max = 10, $length = 40, $depth = 0, $objects = array()) + public function smarty_modifier_debug_print_var($var, $max = 10, $length = 40, $depth = 0, $objects = array()) { $_replace = array("\n" => '\n', "\r" => '\r', "\t" => '\t'); switch (gettype($var)) { @@ -426,7 +369,7 @@ class Core extends Base { * * @return string escaped input string */ - private function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true) + public function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true) { if (!$char_set) { $char_set = \Smarty\Smarty::$_CHARSET; @@ -549,7 +492,7 @@ class Core extends Base { * * @return array */ - private function smarty_modifier_explode($separator, $string, ?int $limit = null) + public function smarty_modifier_explode($separator, $string, ?int $limit = null) { // provide $string default to prevent deprecation errors in PHP >=8.1 return explode($separator, $string ?? '', $limit ?? PHP_INT_MAX); @@ -571,7 +514,7 @@ class Core extends Base { * @return string wrapped string * @author Rodney Rehm */ - private function smarty_modifier_mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) + public function smarty_modifier_mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) { return smarty_mb_wordwrap($str, $width, $break, $cut); } @@ -589,7 +532,7 @@ class Core extends Base { * * @return string */ - private function smarty_modifier_number_format(?float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ",") + public function smarty_modifier_number_format(?float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ",") { // provide $num default to prevent deprecation errors in PHP >=8.1 return number_format($num ?? 0.0, $decimals, $decimal_separator, $thousands_separator); @@ -612,7 +555,7 @@ class Core extends Base { * * @return string */ - private function smarty_modifier_regex_replace($string, $search, $replace, $limit = -1) + public function smarty_modifier_regex_replace($string, $search, $replace, $limit = -1) { if (is_array($search)) { foreach ($search as $idx => $s) { @@ -660,7 +603,7 @@ class Core extends Base { * * @return string */ - private function smarty_modifier_replace($string, $search, $replace) + public function smarty_modifier_replace($string, $search, $replace) { return smarty_mb_str_replace($search, $replace, $string); } @@ -684,7 +627,7 @@ class Core extends Base { * * @return string truncated string */ - private function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) + public function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) { if ($length === 0) { return ''; diff --git a/src/Extension/ExtensionInterface.php b/src/Extension/ExtensionInterface.php index b8a65c57..791b8e3a 100644 --- a/src/Extension/ExtensionInterface.php +++ b/src/Extension/ExtensionInterface.php @@ -10,10 +10,12 @@ interface ExtensionInterface { public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface; - public function getBlockHandler(string $blockTagName): ?\Smarty\FunctionHandler\BlockHandlerInterface; + public function getBlockHandler(string $blockTagName): ?\Smarty\BlockHandler\BlockHandlerInterface; public function getModifierCallback(string $modifierName); + public function getPreFilters(): array; + public function getPostFilters(): array; public function getOutputFilters(): array; } \ No newline at end of file diff --git a/src/FunctionHandler/BCPluginWrapper.php b/src/FunctionHandler/BCPluginWrapper.php new file mode 100644 index 00000000..2cd6dbc6 --- /dev/null +++ b/src/FunctionHandler/BCPluginWrapper.php @@ -0,0 +1,22 @@ +callback = $callback; + $this->cacheable = $cacheable; + $this->cache_attributes = $cache_attributes; + } + + public function handle($params, Template $template) { + $func = $this->callback; + return $func($params, $template->smarty); + } + +} \ No newline at end of file diff --git a/src/FunctionHandler/Base.php b/src/FunctionHandler/Base.php index a4e55eae..f22d3e13 100644 --- a/src/FunctionHandler/Base.php +++ b/src/FunctionHandler/Base.php @@ -6,6 +6,24 @@ use Smarty\Template; class Base implements FunctionHandlerInterface { + /** + * @var bool + */ + protected $cacheable = true; + + /** + * @var array + */ + protected $cache_attributes = []; + + public function isCacheable(): bool { + return $this->cacheable; + } + + public function getCacheAttributes(): array { + return $this->cache_attributes; + } + public function handle($params, Template $template) { // TODO: Implement handle() method. } diff --git a/src/FunctionHandler/FunctionHandlerInterface.php b/src/FunctionHandler/FunctionHandlerInterface.php index cf7c8219..c73e5375 100644 --- a/src/FunctionHandler/FunctionHandlerInterface.php +++ b/src/FunctionHandler/FunctionHandlerInterface.php @@ -6,5 +6,6 @@ use Smarty\Template; interface FunctionHandlerInterface { public function handle($params, Template $template); - + public function isCacheable(): bool; + public function getCacheAttributes(): array; } \ No newline at end of file diff --git a/src/Parser/TemplateParser.y b/src/Parser/TemplateParser.y index 7508e8fb..f688dbb3 100644 --- a/src/Parser/TemplateParser.y +++ b/src/Parser/TemplateParser.y @@ -304,9 +304,9 @@ template ::= . smartytag(A) ::= SIMPELOUTPUT(B). { $var = trim(substr(B, $this->compiler->getLdelLength(), -$this->compiler->getRdelLength()), ' $'); if (preg_match('/^(.*)(\s+nocache)$/', $var, $match)) { - A = $this->compiler->compileTag('private_print_expression',array('nocache'),array('value'=>$this->compiler->compileVariable('\''.$match[1].'\''))); + A = (new \Smarty\Compile\PrintExpressionCompiler())->compile(array('nocache'),$this->compiler,array('value'=>$this->compiler->compileVariable('\''.$match[1].'\''))); } else { - A = $this->compiler->compileTag('private_print_expression',array(),array('value'=>$this->compiler->compileVariable('\''.$var.'\''))); + A = (new \Smarty\Compile\PrintExpressionCompiler())->compile(array(),$this->compiler,array('value'=>$this->compiler->compileVariable('\''.$var.'\''))); } } @@ -321,7 +321,7 @@ smartytag(A)::= SIMPLETAG(B). { if ($this->security) { $this->security->isTrustedConstant($tag, $this->compiler); } - A = $this->compiler->compileTag('private_print_expression',array(),array('value'=>$tag)); + A = (new \Smarty\Compile\PrintExpressionCompiler())->compile(array(),$this->compiler,array('value'=>$tag)); } else { if (preg_match('/^(.*)(\s+nocache)$/', $tag, $match)) { A = $this->compiler->compileTag($match[1],array('\'nocache\'')); @@ -352,7 +352,7 @@ smartytag(A) ::= LDEL tagbody(B) RDEL. { } // output with optional attributes tagbody(A) ::= outattr(B). { - A = $this->compiler->compileTag('private_print_expression',B[1],array('value'=>B[0])); + A = (new \Smarty\Compile\PrintExpressionCompiler())->compile(B[1],$this->compiler,array('value'=>B[0])); } // @@ -392,7 +392,7 @@ tag(res) ::= LDEL ID(i) attributes(a). { if ($this->security) { $this->security->isTrustedConstant(i, $this->compiler); } - res = $this->compiler->compileTag('private_print_expression',a,array('value'=>i)); + res = (new \Smarty\Compile\PrintExpressionCompiler())->compile(a,$this->compiler,array('value'=>i)); } else { res = $this->compiler->compileTag(i,a); } @@ -402,7 +402,7 @@ tag(res) ::= LDEL ID(i). { if ($this->security) { $this->security->isTrustedConstant(i, $this->compiler); } - res = $this->compiler->compileTag('private_print_expression',array(),array('value'=>i)); + res = (new \Smarty\Compile\PrintExpressionCompiler())->compile(array(),$this->compiler,array('value'=>i)); } else { res = $this->compiler->compileTag(i,array()); } @@ -415,7 +415,7 @@ tag(res) ::= LDEL ID(i) modifierlist(l)attributes(a). { if ($this->security) { $this->security->isTrustedConstant(i, $this->compiler); } - res = $this->compiler->compileTag('private_print_expression',a,array('value'=>i, 'modifierlist'=>l)); + res = (new \Smarty\Compile\PrintExpressionCompiler())->compile(a,$this->compiler,array('value'=>i, 'modifierlist'=>l)); } else { res = $this->compiler->compileTag(i,a, array('modifierlist'=>l)); } @@ -764,7 +764,7 @@ value(res) ::= varindexed(vi) DOUBLECOLON static_class_access(r). { } $prefixVar = $this->compiler->getNewPrefixVariable(); if (vi['var'] === '\'smarty\'') { - $this->compiler->appendPrefixCode("compiler->compileTag('private_special_variable',array(),vi['smarty_internal_index']).';?>'); + $this->compiler->appendPrefixCode("compile(array(),$this->compiler,vi['smarty_internal_index']).';?>'); } else { $this->compiler->appendPrefixCode("compiler->compileVariable(vi['var']).vi['smarty_internal_index'].';?>'); } @@ -780,7 +780,7 @@ value(res) ::= smartytag(st). { } value(res) ::= value(v) modifierlist(l). { - res = $this->compiler->compileTag('private_modifier',array(),array('value'=>v,'modifierlist'=>l)); + res = $this->compiler->compileModifier(l, v); } // name space constant value(res) ::= NAMESPACE(c). { @@ -827,7 +827,7 @@ variable(res) ::= DOLLARID(i). { } variable(res) ::= varindexed(vi). { if (vi['var'] === '\'smarty\'') { - $smarty_var = $this->compiler->compileTag('private_special_variable',array(),vi['smarty_internal_index']); + $smarty_var = (new \Smarty\Compile\SpecialVariableCompiler())->compile(array(),$this->compiler,vi['smarty_internal_index']); res = $smarty_var; } else { // used for array reset,next,prev,end,current @@ -912,11 +912,11 @@ indexdef(res) ::= DOT LDEL expr(e) RDEL. { // section tag index indexdef(res) ::= OPENB ID(i)CLOSEB. { - res = '['.$this->compiler->compileTag('private_special_variable',array(),'[\'section\'][\''.i.'\'][\'index\']').']'; + res = '['.(new \Smarty\Compile\SpecialVariableCompiler())->compile(array(),$this->compiler,'[\'section\'][\''.i.'\'][\'index\']').']'; } indexdef(res) ::= OPENB ID(i) DOT ID(i2) CLOSEB. { - res = '['.$this->compiler->compileTag('private_special_variable',array(),'[\'section\'][\''.i.'\'][\''.i2.'\']').']'; + res = '['.(new \Smarty\Compile\SpecialVariableCompiler())->compile(array(),$this->compiler,'[\'section\'][\''.i.'\'][\''.i2.'\']').']'; } indexdef(res) ::= OPENB SINGLEQUOTESTRING(s) CLOSEB. { res = '['.s.']'; @@ -982,7 +982,7 @@ varvarele(res) ::= LDEL expr(e) RDEL. { // object(res) ::= varindexed(vi) objectchain(oc). { if (vi['var'] === '\'smarty\'') { - res = $this->compiler->compileTag('private_special_variable',array(),vi['smarty_internal_index']).oc; + res = (new \Smarty\Compile\SpecialVariableCompiler())->compile(array(),$this->compiler,vi['smarty_internal_index']).oc; } else { res = $this->compiler->compileVariable(vi['var']).vi['smarty_internal_index'].oc; } diff --git a/src/Resource/BasePlugin.php b/src/Resource/BasePlugin.php index 6738c96b..42254f59 100644 --- a/src/Resource/BasePlugin.php +++ b/src/Resource/BasePlugin.php @@ -177,7 +177,7 @@ abstract class BasePlugin */ public static function source( \Smarty\Template $_template = null, - \Smarty $smarty = null, + \Smarty\Smarty $smarty = null, $template_resource = null ) { return \Smarty\Template\Source::load($_template, $smarty, $template_resource); diff --git a/src/Resource/RecompiledPlugin.php b/src/Resource/RecompiledPlugin.php index 19b8a389..a16ab990 100644 --- a/src/Resource/RecompiledPlugin.php +++ b/src/Resource/RecompiledPlugin.php @@ -55,7 +55,7 @@ abstract class RecompiledPlugin extends BasePlugin { // call compiler try { eval('?>' . $_smarty_tpl->compiler->compileTemplate($_smarty_tpl)); - } catch (Exception $e) { + } catch (\Exception $e) { unset($_smarty_tpl->compiler); while (ob_get_level() > $level) { ob_end_clean(); diff --git a/src/Runtime/InheritanceRuntime.php b/src/Runtime/InheritanceRuntime.php index 4626dfaf..72663ef5 100644 --- a/src/Runtime/InheritanceRuntime.php +++ b/src/Runtime/InheritanceRuntime.php @@ -131,7 +131,7 @@ class InheritanceRuntime { } /** - * \Smarty\Block constructor. + * \Smarty\Runtime\Block constructor. * - if outer level {block} of child template ($state === 1) save it as child root block * - otherwise process inheritance and render * diff --git a/src/Security.php b/src/Security.php index 40bbf85b..4bad8b48 100644 --- a/src/Security.php +++ b/src/Security.php @@ -353,16 +353,7 @@ class Security { */ public function isTrustedTag($tag_name, $compiler) { // check for internal always required tags - if (in_array( - $tag_name, - [ - 'assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', - 'private_object_block_function', 'private_object_function', 'private_registered_function', - 'private_registered_block', 'private_special_variable', 'private_print_expression', - 'private_modifier', - ] - ) - ) { + if (in_array($tag_name, ['assign', 'call'])) { return true; } // check security settings diff --git a/src/Smarty.php b/src/Smarty.php index 00000539..c5a28ec5 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -2,8 +2,12 @@ namespace Smarty; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use Smarty\Extension\Base; -use Smarty\Extension\Core; +use Smarty\Extension\BCPluginsAdapter; +use Smarty\Extension\CoreExtension; +use Smarty\Extension\DefaultExtension; use Smarty\Extension\ExtensionInterface; use Smarty\Smarty\Runtime\CaptureRuntime; use Smarty\Smarty\Runtime\ForeachRuntime; @@ -49,7 +53,7 @@ class Smarty extends \Smarty\TemplateBase /** * smarty version */ - const SMARTY_VERSION = '4.3.0'; + const SMARTY_VERSION = '5.0.0'; /** * define variable scopes */ @@ -184,7 +188,7 @@ class Smarty extends \Smarty\TemplateBase * * @var callable */ - public $default_plugin_handler_func = null; + private $default_plugin_handler_func = null; /** * flag if template_dir is normalized @@ -193,13 +197,6 @@ class Smarty extends \Smarty\TemplateBase */ public $_compileDirNormalized = false; - /** - * flag if plugins_dir is normalized - * - * @var bool - */ - public $_pluginsDirNormalized = false; - /** * flag if template_dir is normalized * @@ -572,8 +569,12 @@ class Smarty extends \Smarty\TemplateBase * @var ExtensionInterface[] */ private $extensions = []; + /** + * @var BCPluginsAdapter + */ + private $BCPluginsAdapter; - /** + /** * Initialize new Smarty object */ public function __construct() @@ -593,8 +594,12 @@ class Smarty extends \Smarty\TemplateBase if (\Smarty\Smarty::$_CHARSET !== 'UTF-8') { \Smarty\Smarty::$_UTF8_MODIFIER = ''; } + + $this->BCPluginsAdapter = new BCPluginsAdapter($this); - $this->extensions[] = new Core(); + $this->extensions[] = new CoreExtension(); + $this->extensions[] = new DefaultExtension(); + $this->extensions[] = $this->BCPluginsAdapter; } /** @@ -785,17 +790,95 @@ class Smarty extends \Smarty\TemplateBase return $this->setTemplateDir($config_dir, true); } - /** + + /** + * Registers plugin to be used in templates + * + * @param string $type plugin type + * @param string $name name of template tag + * @param callable $callback PHP callback to register + * @param bool $cacheable if true (default) this function is cache able + * @param mixed $cache_attr caching attributes if any + * + * @return $this + * @throws \Smarty\Exception + * @link https://www.smarty.net/docs/en/api.register.plugin.tpl + * + * @api Smarty::registerPlugin() + */ + public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null) { + if (isset($this->registered_plugins[$type][$name])) { + throw new Exception("Plugin tag '{$name}' already registered"); + } elseif (!is_callable($callback)) { + throw new Exception("Plugin '{$name}' not callable"); + } elseif ($cacheable && $cache_attr) { + throw new Exception("Cannot set caching attributes for plugin '{$name}' when it is cacheable."); + } else { + $this->registered_plugins[$type][$name] = [$callback, (bool)$cacheable, (array)$cache_attr]; + } + return $this; + } + + /** + * Returns plugin previously registered using ::registerPlugin as a numerical array as follows or null if not found: + * [ + * 0 => the callback + * 1 => (bool) $cacheable + * 2 => (array) $cache_attr + * ] + * + * @param string $type plugin type + * @param string $name name of template tag + * + * @return array|null + * @link https://www.smarty.net/docs/en/api.unregister.plugin.tpl + * + * @api Smarty::unregisterPlugin() + */ + public function getRegisteredPlugin($type, $name): ?array { + if (isset($this->registered_plugins[$type][$name])) { + return $this->registered_plugins[$type][$name]; + } + return null; + } + + /** + * Unregisters plugin previously registered using ::registerPlugin + * + * @param string $type plugin type + * @param string $name name of template tag + * + * @return $this + * @link https://www.smarty.net/docs/en/api.unregister.plugin.tpl + * + * @api Smarty::unregisterPlugin() + */ + public function unregisterPlugin($type, $name) { + if (isset($this->registered_plugins[$type][$name])) { + unset($this->registered_plugins[$type][$name]); + } + return $this; + } + + /** * Adds directory of plugin files * * @param null|array|string $plugins_dir * * @return Smarty current Smarty instance for chaining + * @deprecated since 5.0 */ public function addPluginsDir($plugins_dir) { - $this->plugins_dir = array_merge($this->plugins_dir, (array)$plugins_dir); - $this->_pluginsDirNormalized = false; + trigger_error('Using Smarty::addPluginsDir() to load plugins is deprecated and will be ' . + 'removed in a future release. Use Smarty::addExtension() to add an extension or Smarty::registerPlugin to ' . + 'quickly register a plugin using a callback function.', E_USER_DEPRECATED); + + foreach ((array) $plugins_dir as $v) { + $path = $this->_realpath(rtrim($v ?? '', '/\\') . DIRECTORY_SEPARATOR, true); + $this->BCPluginsAdapter->loadPluginsFromDir($path); + } + return $this; } @@ -803,17 +886,13 @@ class Smarty extends \Smarty\TemplateBase * Get plugin directories * * @return array list of plugin directories + * @deprecated since 5.0 */ public function getPluginsDir() { - if (!$this->_pluginsDirNormalized) { - foreach ($this->plugins_dir as $k => $v) { - $this->plugins_dir[ $k ] = $this->_realpath(rtrim($v ?? '', '/\\') . DIRECTORY_SEPARATOR, true); - } - $this->_cache[ 'plugin_files' ] = array(); - $this->_pluginsDirNormalized = true; - } - return $this->plugins_dir; + trigger_error('Using Smarty::getPluginsDir() is deprecated and will be ' . + 'removed in a future release. It will always return an empty array.', E_USER_DEPRECATED); + return []; } @@ -823,14 +902,52 @@ class Smarty extends \Smarty\TemplateBase * @param string|array $plugins_dir directory(s) of plugins * * @return Smarty current Smarty instance for chaining + * @deprecated since 5.0 */ public function setPluginsDir($plugins_dir) { - $this->plugins_dir = (array)$plugins_dir; - $this->_pluginsDirNormalized = false; - return $this; + trigger_error('Using Smarty::getPluginsDir() is deprecated and will be ' . + 'removed in a future release. For now, it will remove the DefaultExtension from the extensions list and ' . + 'proceed to call Smartyy::addPluginsDir..', E_USER_DEPRECATED); + + $this->extensions = array_filter( + $this->extensions, + function ($extension) { + return !($extension instanceof DefaultExtension); + } + ); + + return $this->addPluginsDir($plugins_dir); } + /** + * Registers a default plugin handler + * + * @param callable $callback class/method name + * + * @return $this + * @throws Exception if $callback is not callable + * @link https://www.smarty.net/docs/en/api.register.default.plugin.handler.tpl + * + * @api Smarty::registerDefaultPluginHandler() + * + * @deprecated since 5.0 + */ + public function registerDefaultPluginHandler($callback) { + + trigger_error('Using Smarty::registerDefaultPluginHandler() is deprecated and will be ' . + 'removed in a future release. Please rewrite your plugin handler as an extension.', + E_USER_DEPRECATED); + + if (is_callable($callback)) { + $this->default_plugin_handler_func = $callback; + } else { + throw new Exception("Default plugin handler '$callback' not callable"); + } + return $this; + } + + /** * Get compiled directory * @@ -925,8 +1042,7 @@ class Smarty extends \Smarty\TemplateBase $tpl->inheritance = null; $tpl->tpl_vars = $tpl->config_vars = array(); } else { - /* @var \Smarty\Template $tpl */ - $tpl = new $this->template_class($template, $this, null, $cache_id, $compile_id, null, null); + $tpl = new \Smarty\Template($template, $this, null, $cache_id, $compile_id, null, null); $tpl->templateId = $_templateId; } if ($do_clone) { @@ -1368,8 +1484,8 @@ class Smarty extends \Smarty\TemplateBase $_count = 0; try { $_compileDirs = new RecursiveDirectoryIterator($_dir); - // NOTE: UnexpectedValueException thrown for PHP >= 5.3 - } catch (Exception $e) { + } catch (\UnexpectedValueException $e) { + // path not found / not a dir return 0; } $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); @@ -1515,16 +1631,14 @@ class Smarty extends \Smarty\TemplateBase if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) { $_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file; } - echo "\n
", $_dir, '---', $_file; + echo "\n", $_dir, '---', $_file; flush(); $_start_time = microtime(true); $_smarty = clone $this; // - $_smarty->_cache = array(); $_smarty->force_compile = $force_compile; try { - /* @var Template $_tpl */ - $_tpl = new $this->template_class($_file, $_smarty); + $_tpl = new \Smarty\Template($_file, $_smarty); $_tpl->caching = self::CACHING_OFF; $_tpl->source = $isConfig ? \Smarty\Template\Config::load($_tpl) : \Smarty\Template\Source::load($_tpl); @@ -1537,20 +1651,20 @@ class Smarty extends \Smarty\TemplateBase echo ' is up to date'; flush(); } - } catch (Exception $e) { - echo "\n
------>Error: ", $e->getMessage(), "

\n"; + } catch (\Exception $e) { + echo "\n ------>Error: ", $e->getMessage(), "\n"; $_error_count++; } // free memory unset($_tpl); $_smarty->_clearTemplateCache(); if ($max_errors !== null && $_error_count === $max_errors) { - echo "\n

too many errors\n"; + echo "\ntoo many errors\n"; exit(1); } } } - echo "\n
"; + echo "\n"; return $_count; } @@ -1611,25 +1725,106 @@ class Smarty extends \Smarty\TemplateBase } } + public function getModifierCallback(string $modifierName) { + foreach ($this->getExtensions() as $extension) { + if ($callback = $extension->getModifierCallback($modifierName)) { + return $callback; + } + } + return null; + } + + public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface { + foreach ($this->getExtensions() as $extension) { + if ($handler = $extension->getFunctionHandler($functionName)) { + return $handler; + } + } + return null; + } + + public function getBlockHandler(string $blockTagName): ?\Smarty\BlockHandler\BlockHandlerInterface { + foreach ($this->getExtensions() as $extension) { + if ($handler = $extension->getBlockHandler($blockTagName)) { + return $handler; + } + } + return null; + } + + public function getModifierCompiler(string $modifier): ?\Smarty\Compile\Modifier\ModifierCompilerInterface { + foreach ($this->getExtensions() as $extension) { + if ($handler = $extension->getModifierCompiler($modifier)) { + return $handler; + } + } + return null; + } + /** - * Run filters over content - * The filters will be lazy loaded if required + * Run pre-filters over template source + * + * @param string $source the content which shall be processed by the filters + * @param Template $template template object + * + * @return string the filtered source + */ + public function runPreFilters($source, Template $template) + { + + foreach ($this->getExtensions() as $extension) { + /** @var \Smarty\Filter\FilterInterface $filter */ + foreach ($extension->getPreFilters() as $filter) { + $source = $filter->filter($source, $template); + } + } + + // return filtered output + return $source; + } + + /** + * Run post-filters over template's compiled code + * + * @param string $code the content which shall be processed by the filters + * @param Template $template template object + * + * @return string the filtered code + */ + public function runPostFilters($code, Template $template) + { + + foreach ($this->getExtensions() as $extension) { + /** @var \Smarty\Filter\FilterInterface $filter */ + foreach ($extension->getPostFilters() as $filter) { + $code = $filter->filter($code, $template); + } + } + + // return filtered output + return $code; + } + + + + /** + * Run filters over template output * - * @param string $type the type of filter ('pre','post','output') which shall run * @param string $content the content which shall be processed by the filters * @param Template $template template object * - * @return string the filtered content - *@throws Exception + * @return string the filtered (modified) output */ - public function runFilter($type, $content, Template $template) + public function runOutputFilters($content, Template $template) { - // loop over registered filters of specified type - if (!empty($this->registered_filters[ $type ])) { - foreach ($this->registered_filters[ $type ] as $key => $name) { - $content = call_user_func($this->registered_filters[ $type ][ $key ], $content, $template); + + foreach ($this->getExtensions() as $extension) { + /** @var \Smarty\Filter\FilterInterface $filter */ + foreach ($extension->getOutputFilters() as $filter) { + $content = $filter->filter($content, $template); } } + // return filtered output return $content; } @@ -1911,4 +2106,11 @@ class Smarty extends \Smarty\TemplateBase } } + /** + * @return callable|null + */ + public function getDefaultPluginHandlerFunc(): ?callable { + return $this->default_plugin_handler_func; + } + } diff --git a/src/Template.php b/src/Template.php index 9b6309ac..c3129e91 100644 --- a/src/Template.php +++ b/src/Template.php @@ -23,7 +23,6 @@ use Smarty\Template\Config; * @property Compiled $compiled * @property Cached $cached * @property \Smarty\Compiler\Template $compiler - * @property mixed|\Smarty\Template\Cached registered_plugins */ #[\AllowDynamicProperties] class Template extends TemplateBase { @@ -224,7 +223,7 @@ class Template extends TemplateBase { if ((!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) && !$no_output_filter && isset($this->smarty->registered_filters['output']) ) { - echo $this->smarty->runFilter('output', ob_get_clean(), $this); + echo $this->smarty->runOutputFilters(ob_get_clean(), $this); } else { echo ob_get_clean(); } @@ -247,7 +246,7 @@ class Template extends TemplateBase { && (!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) && isset($this->smarty->registered_filters['output']) ) { - return $this->smarty->runFilter('output', ob_get_clean(), $this); + return $this->smarty->runOutputFilters(ob_get_clean(), $this); } // return cache content return null; @@ -360,7 +359,6 @@ class Template extends TemplateBase { $this->cached->hashes[$tpl->compiled->nocache_hash] = true; } } - $tpl->_cache = []; if (isset($uid)) { if ($smarty->debugging) { if (!isset($smarty->_debug)) { @@ -442,7 +440,7 @@ class Template extends TemplateBase { * @return bool flag if compiled or cache file is valid * @throws \Smarty\Exception */ - public function _decodeProperties(Template $tpl, $properties, $cache = false) { + public function isFresh(Template $tpl, $properties, $cache = false) { // on cache resources other than file check version stored in cache code if (!isset($properties['version']) || \Smarty\Smarty::SMARTY_VERSION !== $properties['version']) { if ($cache) { @@ -468,7 +466,7 @@ class Template extends TemplateBase { $mtime = is_file($_file_to_check[0]) ? filemtime($_file_to_check[0]) : false; } } else { - $handler = Smarty\Resource\BasePlugin::load($tpl->smarty, $_file_to_check[2]); + $handler = \Smarty\Resource\BasePlugin::load($tpl->smarty, $_file_to_check[2]); if ($handler->checkTimestamps()) { $source = Source::load($tpl, $tpl->smarty, $_file_to_check[0]); $mtime = $source->getTimeStamp(); @@ -601,12 +599,7 @@ class Template extends TemplateBase { * @throws \Smarty\Exception */ public function loadCompiler() { - $this->compiler = - new $this->source->compiler_class( - $this->source->template_lexer_class, - $this->source->template_parser_class, - $this->smarty - ); + $this->compiler = $this->source->createCompiler(); } /** diff --git a/src/Template/Cached.php b/src/Template/Cached.php index 1a05bf17..869e2ea1 100644 --- a/src/Template/Cached.php +++ b/src/Template/Cached.php @@ -370,7 +370,7 @@ class Cached extends ResourceBase { && !$_template->cached->has_nocache_code && isset($_template->smarty->registered_filters['output']) ) { - $content = $_template->smarty->runFilter('output', $content, $_template); + $content = $_template->smarty->runOutputFilters($content, $_template); } // write cache file content $_template->writeCachedContent($content); diff --git a/src/Template/Compiled.php b/src/Template/Compiled.php index 5b6060b5..54036089 100644 --- a/src/Template/Compiled.php +++ b/src/Template/Compiled.php @@ -181,7 +181,7 @@ class Compiled extends ResourceBase { // call compiler $_template->loadCompiler(); $this->write($_template, $_template->compiler->compileTemplate($_template)); - } catch (Exception $e) { + } catch (\Exception $e) { // restore old timestamp in case of error if ($saved_timestamp && is_file($this->filepath)) { touch($this->filepath, $saved_timestamp); diff --git a/src/Template/Config.php b/src/Template/Config.php index cf6d0c91..a26e8af9 100644 --- a/src/Template/Config.php +++ b/src/Template/Config.php @@ -35,27 +35,6 @@ class Config extends Source { */ public $isConfig = true; - /** - * Name of the Class to compile this resource's contents with - * - * @var string - */ - public $compiler_class = \Smarty\Compiler\Configfile::class; - - /** - * Name of the Class to tokenize this resource's contents with - * - * @var string - */ - public $template_lexer_class = \Smarty\Lexer\ConfigfileLexer::class; - - /** - * Name of the Class to parse this resource's contents with - * - * @var string - */ - public $template_parser_class = \Smarty\Parser\ConfigfileParser::class; - /** * initialize Source Object for given resource * Either [$_template] or [$smarty, $template_resource] must be specified @@ -81,7 +60,7 @@ class Config extends Source { throw new Exception('Source: Missing name'); } // parse resource_name, load resource handler - [$name, $type] = Smarty\Resource\BasePlugin::parseResourceName($template_resource, $smarty->default_config_type); + [$name, $type] = \Smarty\Resource\BasePlugin::parseResourceName($template_resource, $smarty->default_config_type); // make sure configs are not loaded via anything smarty can't handle if (isset($_incompatible_resources[$type])) { throw new Exception("Unable to use resource '{$type}' for config"); @@ -94,4 +73,8 @@ class Config extends Source { } return $source; } + + public function createCompiler(): \Smarty\Compiler\BaseCompiler { + return new \Smarty\Compiler\Configfile($this->smarty); + } } diff --git a/src/Template/Source.php b/src/Template/Source.php index f5a6b0b9..ff71d176 100644 --- a/src/Template/Source.php +++ b/src/Template/Source.php @@ -107,27 +107,6 @@ class Source { */ public $content = null; - /** - * Name of the Class to compile this resource's contents with - * - * @var string - */ - public $compiler_class = \Smarty\Compiler\Template::class; - - /** - * Name of the Class to tokenize this resource's contents with - * - * @var string - */ - public $template_lexer_class = \Smarty\Lexer\TemplateLexer::class; - - /** - * Name of the Class to parse this resource's contents with - * - * @var string - */ - public $template_parser_class = \Smarty\Parser\TemplateParser::class; - /** * create Source Object container * @@ -152,9 +131,9 @@ class Source { * initialize Source Object for given resource * Either [$_template] or [$smarty, $template_resource] must be specified * - * @param Template $_template template object - * @param Smarty $smarty smarty object - * @param string $template_resource resource identifier + * @param Template|null $_template template object + * @param Smarty|null $smarty smarty object + * @param null $template_resource resource identifier * * @return Source Source Object * @throws Exception @@ -210,7 +189,7 @@ class Source { * @throws \Smarty\Exception */ public function getContent() { - return isset($this->content) ? $this->content : $this->handler->getContent($this); + return $this->content ?? $this->handler->getContent($this); } /** @@ -241,7 +220,7 @@ class Source { $this->content = $_content; $this->exists = true; $this->uid = $this->name = sha1($_content); - $this->handler = Smarty\Resource\BasePlugin::load($this->smarty, 'eval'); + $this->handler = \Smarty\Resource\BasePlugin::load($this->smarty, 'eval'); } else { $this->exists = false; throw new Exception( @@ -250,4 +229,9 @@ class Source { ); } } + + public function createCompiler(): \Smarty\Compiler\BaseCompiler { + return new \Smarty\Compiler\Template($this->smarty); + } + } diff --git a/src/TemplateBase.php b/src/TemplateBase.php index bef82dfa..0813be7f 100644 --- a/src/TemplateBase.php +++ b/src/TemplateBase.php @@ -237,7 +237,7 @@ abstract class TemplateBase extends Data { error_reporting($_smarty_old_error_level); } return $result; - } catch (Throwable $e) { + } catch (\Throwable $e) { while (ob_get_level() > $level) { ob_end_clean(); } @@ -252,54 +252,6 @@ abstract class TemplateBase extends Data { } } - /** - * Registers plugin to be used in templates - * - * @param string $type plugin type - * @param string $name name of template tag - * @param callable $callback PHP callback to register - * @param bool $cacheable if true (default) this function is cache able - * @param mixed $cache_attr caching attributes if any - * - * @return \Smarty|\Smarty\Template - * @throws \Smarty\Exception - * @link https://www.smarty.net/docs/en/api.register.plugin.tpl - * - * @api Smarty::registerPlugin() - */ - public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null) { - $smarty = $this->_getSmartyObj(); - if (isset($smarty->registered_plugins[$type][$name])) { - throw new Exception("Plugin tag '{$name}' already registered"); - } elseif (!is_callable($callback)) { - throw new Exception("Plugin '{$name}' not callable"); - } elseif ($cacheable && $cache_attr) { - throw new Exception("Cannot set caching attributes for plugin '{$name}' when it is cacheable."); - } else { - $smarty->registered_plugins[$type][$name] = [$callback, (bool)$cacheable, (array)$cache_attr]; - } - return $this; - } - - /** - * Registers plugin to be used in templates - * - * @param string $type plugin type - * @param string $name name of template tag - * - * @return \Smarty|\Smarty\Template - * @link https://www.smarty.net/docs/en/api.unregister.plugin.tpl - * - * @api Smarty::unregisterPlugin() - */ - public function unregisterPlugin($type, $name) { - $smarty = $this->_getSmartyObj(); - if (isset($smarty->registered_plugins[$type][$name])) { - unset($smarty->registered_plugins[$type][$name]); - } - return $this; - } - /** * load a filter of specified type and name * @@ -780,27 +732,6 @@ abstract class TemplateBase extends Data { return $this; } - /** - * Registers a default plugin handler - * - * @param callable $callback class/method name - * - * @return TemplateBase - * @throws Exception if $callback is not callable - * @link https://www.smarty.net/docs/en/api.register.default.plugin.handler.tpl - * - * @api Smarty::registerDefaultPluginHandler() - */ - public function registerDefaultPluginHandler($callback) { - $smarty = $this->_getSmartyObj(); - if (is_callable($callback)) { - $smarty->default_plugin_handler_func = $callback; - } else { - throw new Exception("Default plugin handler '$callback' not callable"); - } - return $this; - } - /** * Register template default handler * @@ -832,7 +763,7 @@ abstract class TemplateBase extends Data { * * @api Smarty::registerResource() */ - public function registerResource($name, Smarty\Resource\Base $resource_handler) { + public function registerResource($name, \Smarty\Resource\BasePlugin $resource_handler) { $smarty = $this->_getSmartyObj(); $smarty->registered_resources[$name] = $resource_handler; return $this; diff --git a/tests/PHPUnit_Smarty.php b/tests/PHPUnit_Smarty.php index bc2fb363..fc1f6157 100644 --- a/tests/PHPUnit_Smarty.php +++ b/tests/PHPUnit_Smarty.php @@ -604,8 +604,9 @@ KEY `name` (`name`) $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; return sha1($uid . $_cache_id . $_compile_id); + case 'memcache': case 'memcachetest': - case 'acp': + case 'apc': $sp = $this->buildSourcePath($tpl, $name, $type, $dir); $uid = $this->buildUid($tpl, $sp, $name, $type); $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; diff --git a/tests/UnitTests/A_0/PathNormalization/PluginDirNormalizationTest.php b/tests/UnitTests/A_0/PathNormalization/PluginDirNormalizationTest.php deleted file mode 100644 index 4d623876..00000000 --- a/tests/UnitTests/A_0/PathNormalization/PluginDirNormalizationTest.php +++ /dev/null @@ -1,145 +0,0 @@ -setUpSmarty(__DIR__); - } - - public function testGetPluginsDefaultDir() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(SMARTY_PLUGINS_DIR, $result[ 0 ]); - } - public function testGetPluginsDefaultDir2() - { - $this->smarty->addPluginsDir('./foo/'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(2, count($result)); - $this->assertEquals(SMARTY_PLUGINS_DIR, $result[ 0 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 1 ]); - } - - public function testSetPluginsDir1() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('foo'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - } - - public function testSetPluginsDir2() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('foo/'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - } - - public function testSetPluginsDir3() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('foo/.'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - } - - public function testSetPluginsDir4() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('foo/./'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - } - - public function testSetPluginsDir5() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('.foo'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . '.foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - } - - public function testSetPluginsDir6() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('..foo'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . '..foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - } - - public function testSetPluginsDir7() - { - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->smarty->setPluginsDir('../foo'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(1, count($result)); - $this->assertEquals(substr(getcwd(), 0, - strlen(basename(getcwd()))) . 'foo' . DIRECTORY_SEPARATOR, - $result[ 0 ]); - } - - public function testSetPluginsDir8() - { - $this->smarty->setPluginsDir(array('foo', 'bah')); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(2, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'bah' . DIRECTORY_SEPARATOR, $result[ 1 ]); - } - - public function testSetPluginsDir9() - { - $this->smarty->setPluginsDir(array('foo', 'bah')); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(2, count($result)); - $this->smarty->addPluginsDir('blar'); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(3, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'bah' . DIRECTORY_SEPARATOR, $result[ 1 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'blar' . DIRECTORY_SEPARATOR, $result[ 2 ]); - } - - public function testSetPluginsDir10() - { - $this->smarty->setPluginsDir(array('foo', 'bah')); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(2, count($result)); - $this->smarty->addPluginsDir(array('blar', 'smarty')); - $result = $this->smarty->getPluginsDir(); - $this->assertEquals(4, count($result)); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $result[ 0 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'bah' . DIRECTORY_SEPARATOR, $result[ 1 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'blar' . DIRECTORY_SEPARATOR, $result[ 2 ]); - $this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'smarty' . DIRECTORY_SEPARATOR, $result[ 3 ]); - } - -} diff --git a/tests/UnitTests/A_1/ProtectedFolderVars/ProtectedFolderVarsTest.php b/tests/UnitTests/A_1/ProtectedFolderVars/ProtectedFolderVarsTest.php index 86c0e41e..981a39b7 100644 --- a/tests/UnitTests/A_1/ProtectedFolderVars/ProtectedFolderVarsTest.php +++ b/tests/UnitTests/A_1/ProtectedFolderVars/ProtectedFolderVarsTest.php @@ -3,6 +3,8 @@ * This file is part of the Smarty PHPUnit tests. */ +use Smarty\Smarty; + /** * class for protected $template_dir, $compile_dir, $cache_dir, $config_dir, $plugins_dir property tests * @@ -112,51 +114,6 @@ class ProtectedFolderVarsTest extends PHPUnit_Smarty $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR, $d[ 0 ]); } - /* - * plugins_dir - */ - - public function testPluginDirDirectRelative() - { - $s = new Smarty(); - $s->setPluginsDir('./foo'); - $d = $s->getPluginsDir(); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $d[ 0 ]); - } - - public function testPluginDirDirectRelativeArray() - { - $s = new Smarty(); - $s->setPluginsDir(array('./foo', './bar/')); - $d = $s->getPluginsDir(); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $d[ 0 ]); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR, $d[ 1 ]); - } - - public function testPluginDirDirectRelativeArrayAdd() - { - $s = new Smarty(); - $s->setPluginsDir('./foo'); - $s->addPluginsDir('./bar/'); - $d = $s->getPluginsDir(); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR, $d[ 0 ]); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR, $d[ 1 ]); - } - - public function testPluginDirDirectRelativeExtends() - { - $s = new FolderT(); - $d = $s->getPluginsDir(); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'plug' . DIRECTORY_SEPARATOR, $d[ 0 ]); - } - - public function testPluginDirDirectRelativeExtends2() - { - $s = new FolderT(); - $s->setPluginsDir('./bar'); - $d = $s->getPluginsDir(); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR, $d[ 0 ]); - } /* * compile_dir */ diff --git a/tests/UnitTests/A_Core/LoadPlugin/LoadPluginTest.php b/tests/UnitTests/A_Core/LoadPlugin/LoadPluginTest.php index 530f2a15..5706d14c 100644 --- a/tests/UnitTests/A_Core/LoadPlugin/LoadPluginTest.php +++ b/tests/UnitTests/A_Core/LoadPlugin/LoadPluginTest.php @@ -37,11 +37,11 @@ class LoadPluginTest extends PHPUnit_Smarty } /** - * loadPlugin test $template_class exists + * loadPlugin test \Smarty\Template exists */ public function testLoadPluginSmartyTemplateClass() { - $this->assertTrue($this->smarty->loadPlugin($this->smarty->template_class) == true); + $this->assertTrue($this->smarty->loadPlugin(\Smarty\Template) == true); } /** diff --git a/tests/UnitTests/CacheResourceTests/Apc/CacheResourceCustomApcTest.php b/tests/UnitTests/CacheResourceTests/Apc/CacheResourceCustomApcTest.php index fa3ca2b1..6b75c0cb 100644 --- a/tests/UnitTests/CacheResourceTests/Apc/CacheResourceCustomApcTest.php +++ b/tests/UnitTests/CacheResourceTests/Apc/CacheResourceCustomApcTest.php @@ -6,6 +6,7 @@ * @author Uwe Tews */ include_once __DIR__ . '/../Memcache/CacheResourceCustomMemcacheTest.php'; +include_once __DIR__ . '/../_shared/PHPunitplugins/cacheresource.apctest.php'; /** * class for cache resource file tests @@ -24,7 +25,7 @@ class CacheResourceCustomApcTest extends CacheResourceCustomMemcacheTest $this->setUpSmarty(__DIR__); parent::setUp(); $this->smarty->setCachingType('apc'); - $this->smarty->addPluginsDir(SMARTY_DIR . '../demo/plugins/'); + $this->smarty->registerCacheResource('apc', new Smarty_CacheResource_Apctest()); } } diff --git a/tests/UnitTests/CacheResourceTests/PDO/CacheResourceCustomPDOTest.php b/tests/UnitTests/CacheResourceTests/PDO/CacheResourceCustomPDOTest.php index fc9d0afd..39225f9c 100644 --- a/tests/UnitTests/CacheResourceTests/PDO/CacheResourceCustomPDOTest.php +++ b/tests/UnitTests/CacheResourceTests/PDO/CacheResourceCustomPDOTest.php @@ -5,43 +5,41 @@ * @package PHPunit * @author Uwe Tews */ -if (PdoCacheEnable == true) { - include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php'; +include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php'; +include_once __DIR__ . '/../_shared/PHPunitplugins/cacheresource.pdotest.php'; - /** - * class for cache resource file tests - * - * @runTestsInSeparateProcess - * @preserveGlobalState disabled - * @backupStaticAttributes enabled - */ - class CacheResourceCustomPDOTest extends CacheResourceTestCommon +/** + * class for cache resource file tests + * + * @runTestsInSeparateProcess + * @preserveGlobalState disabled + * @backupStaticAttributes enabled + */ +class CacheResourceCustomPDOTest extends CacheResourceTestCommon +{ + + public function setUp($dir = null, $clear = true): void { - - public function setUp($dir = null, $clear = true) - { - if (PdoCacheEnable != true) { - $this->markTestSkipped('mysql Pdo tests are disabled'); - } - if (self::$init) { - $this->getConnection(); - } - $this->setUpSmarty(__DIR__); - parent::setUp(); - $this->smarty->setCachingType('pdo'); - $this->smarty->addPluginsDir(SMARTY_DIR . '../demo/plugins/'); - $this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdotest'), - 'loadPlugin() could not load PDO cache resource'); - $this->smarty->registerCacheResource('pdo', - new Smarty_CacheResource_Pdotest($this->getPDO(), 'output_cache')); + if (PdoCacheEnable != true) { + $this->markTestSkipped('mysql Pdo tests are disabled'); } - - public function testInit() - { - $this->cleanDirs(); - $this->initMysqlCache(); + if (self::$init) { + $this->getConnection(); } + $this->setUpSmarty(__DIR__); + parent::setUp(); + $this->smarty->setCachingType('pdo'); + $this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdotest'), + 'loadPlugin() could not load PDO cache resource'); + $this->smarty->registerCacheResource('pdo', + new Smarty_CacheResource_Pdotest($this->getPDO(), 'output_cache')); + } + + public function testInit() + { + $this->cleanDirs(); + $this->initMysqlCache(); } } diff --git a/tests/UnitTests/CacheResourceTests/PDOgzip/CacheResourceCustomPDOGzipTest.php b/tests/UnitTests/CacheResourceTests/PDOgzip/CacheResourceCustomPDOGzipTest.php index 5df30e25..74d5732a 100644 --- a/tests/UnitTests/CacheResourceTests/PDOgzip/CacheResourceCustomPDOGzipTest.php +++ b/tests/UnitTests/CacheResourceTests/PDOgzip/CacheResourceCustomPDOGzipTest.php @@ -5,41 +5,40 @@ * @package PHPunit * @author Uwe Tews */ -if (PdoGzipCacheEnable == true) { - include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php'; - /** - * class for cache resource file tests - * - * @backupStaticAttributes enabled - */ - class CacheResourceCustomPDOGzipTest extends CacheResourceTestCommon +include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php'; +include_once __DIR__ . '/../_shared/PHPunitplugins/cacheresource.pdo_gziptest.php'; + +/** + * class for cache resource file tests + * + * @backupStaticAttributes enabled + */ +class CacheResourceCustomPDOGzipTest extends CacheResourceTestCommon +{ + + public function setUp($dir = null, $clear = true): void { - - public function setUp($dir = null, $clear = true) - { - if (PdoGzipCacheEnable != true) { - $this->markTestSkipped('mysql Pdo Gzip tests are disabled'); - } - if (self::$init) { - $this->getConnection(); - } - $this->setUpSmarty(__DIR__); - parent::setUp(); - $this->smarty->setCachingType('pdo'); - $this->smarty->addPluginsDir(SMARTY_DIR . '../demo/plugins/'); - $this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdo_Gziptest'), - 'loadPlugin() could not load PDOGzip cache resource'); - $this->smarty->registerCacheResource('pdo', new Smarty_CacheResource_Pdo_Gziptest($this->getPDO(), - 'output_cache')); + if (PdoGzipCacheEnable != true) { + $this->markTestSkipped('mysql Pdo Gzip tests are disabled'); } - - public function testInit() - { - $this->cleanDirs(); - $this->initMysqlCache(); + if (self::$init) { + $this->getConnection(); } + $this->setUpSmarty(__DIR__); + parent::setUp(); + $this->smarty->setCachingType('pdo'); + $this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdo_Gziptest'), + 'loadPlugin() could not load PDOGzip cache resource'); + $this->smarty->registerCacheResource('pdo', new Smarty_CacheResource_Pdo_Gziptest($this->getPDO(), + 'output_cache')); + } + + public function testInit() + { + $this->cleanDirs(); + $this->initMysqlCache(); } } diff --git a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.apctest.php b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.apctest.php index 8befb7d9..cf8098c0 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.apctest.php +++ b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.apctest.php @@ -4,7 +4,7 @@ use Smarty\Smarty; use Smarty\Template; use Smarty\Template\Cached; -require_once SMARTY_DIR . '../demo/plugins/cacheresource.apc.php'; +require_once __DIR__ . '/../../../__shared/cacheresources/cacheresource.apc.php'; class Smarty_CacheResource_Apctest extends My_CacheResource_Apc { diff --git a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.memcachetest.php b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.memcachetest.php index 70770d8e..2435c256 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.memcachetest.php +++ b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.memcachetest.php @@ -3,7 +3,7 @@ use Smarty\Template; use Smarty\Template\Cached; -require_once SMARTY_DIR . '../demo/plugins/cacheresource.memcache.php'; +require_once __DIR__ . '/../../../__shared/cacheresources/cacheresource.memcache.php'; class Smarty_CacheResource_Memcachetest extends Smarty_CacheResource_Memcache { diff --git a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php index bde93205..1c7e6376 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php +++ b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php @@ -3,7 +3,7 @@ use Smarty\Exception; use Smarty\Template\Cached; -require_once SMARTY_DIR . '../demo/plugins/cacheresource.mysql.php'; +require_once __DIR__ . '/../../../__shared/cacheresources/cacheresource.mysql.php'; class Smarty_CacheResource_Mysqltest extends Smarty_CacheResource_Mysql { diff --git a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdo_gziptest.php b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdo_gziptest.php index 30915b68..32bc3358 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdo_gziptest.php +++ b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdo_gziptest.php @@ -2,13 +2,13 @@ use Smarty\Template\Cached; -require_once SMARTY_DIR . '../demo/plugins/cacheresource.pdo_gzip.php'; +require_once __DIR__ . '/../../../__shared/cacheresources/cacheresource.pdo_gzip.php'; -class Smarty_CacheResource_Pdo_Gziptest extends Smarty_CacheResource_Pdo_Gzip +class Smarty_CacheResource_Pdo_Gziptest extends My_CacheResource_Pdo_Gzip { public $lockTime = 0; - public function hasLock(Smarty $smarty, Cached $cached) + public function hasLock(\Smarty\Smarty $smarty, Cached $cached) { if ($this->lockTime) { $this->lockTime--; diff --git a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdotest.php b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdotest.php index b2f5d9f5..af0fb328 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdotest.php +++ b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.pdotest.php @@ -2,14 +2,14 @@ use Smarty\Template\Cached; -require_once SMARTY_DIR . '../demo/plugins/cacheresource.pdo.php'; +require_once __DIR__ . '/../../../__shared/cacheresources/cacheresource.pdo.php'; -class Smarty_CacheResource_Pdotest extends Smarty_CacheResource_Pdo +class Smarty_CacheResource_Pdotest extends My_CacheResource_Pdo { public $lockTime = 0; - public function hasLock(Smarty $smarty, Cached $cached) + public function hasLock(\Smarty\Smarty $smarty, Cached $cached) { if ($this->lockTime) { $this->lockTime--; diff --git a/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php b/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php index 6153d2c1..d9efbb1f 100644 --- a/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php +++ b/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\Smarty; + /** * class for default config handler test * @@ -117,7 +119,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty * * @return bool */ -function configHandlerData($resource_type, $resource_name, &$config_source, &$config_timestamp, Smarty $smarty) +function configHandlerData($resource_type, $resource_name, &$config_source, &$config_timestamp, \Smarty\Smarty $smarty) { if ($resource_name !== 'foo.conf') { return false; @@ -140,7 +142,7 @@ function configHandlerData($resource_type, $resource_name, &$config_source, &$co * * @return string */ -function configHandlerFile($resource_type, $resource_name, &$config_source, &$config_timestamp, Smarty $smarty) +function configHandlerFile($resource_type, $resource_name, &$config_source, &$config_timestamp, \Smarty\Smarty $smarty) { if ($resource_name !== 'foo.conf') { return 'no.conf'; @@ -160,7 +162,7 @@ function configHandlerFile($resource_type, $resource_name, &$config_source, &$co * * @return bool */ -function configHandlerFalse($resource_type, $resource_name, &$config_source, &$config_timestamp, Smarty $smarty) +function configHandlerFalse($resource_type, $resource_name, &$config_source, &$config_timestamp, \Smarty\Smarty $smarty) { return false; } diff --git a/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php b/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php index b4a75439..fa2e6e71 100644 --- a/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php +++ b/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php @@ -387,7 +387,7 @@ class ConfigVarTest extends PHPUnit_Smarty public function testConfigResourceDb4() { - $this->smarty->addPluginsDir(__DIR__ . "/PHPunitplugins/"); + $this->smarty->addPluginsDir(__DIR__ . "/../../ResourceTests/ResourcePlugins/PHPunitplugins/"); $this->smarty->configLoad('db4:foo.conf'); $this->assertEquals("bar", $this->smarty->fetch('foo.tpl')); } diff --git a/tests/UnitTests/ConfigFileTests/file/PHPunitplugins/resource.db4.php b/tests/UnitTests/ConfigFileTests/file/PHPunitplugins/resource.db4.php deleted file mode 100644 index 6b3b55dd..00000000 --- a/tests/UnitTests/ConfigFileTests/file/PHPunitplugins/resource.db4.php +++ /dev/null @@ -1,30 +0,0 @@ -filepath = 'db4:'; - $source->uid = sha1($source->resource); - $source->timestamp = 0; - $source->exists = true; - } - - public function getContent(Source $source) - { - return "foo = 'bar'\n"; - } -} diff --git a/tests/UnitTests/ResourceTests/Custom/DemoPluginExtendsAll/ResourceExtendsAllPluginTest.php b/tests/UnitTests/ResourceTests/Custom/DemoPluginExtendsAll/ResourceExtendsAllPluginTest.php index 5e81a8c3..af6df790 100644 --- a/tests/UnitTests/ResourceTests/Custom/DemoPluginExtendsAll/ResourceExtendsAllPluginTest.php +++ b/tests/UnitTests/ResourceTests/Custom/DemoPluginExtendsAll/ResourceExtendsAllPluginTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +require_once __DIR__ . '/../../../__shared/resources/resource.extendsall.php'; + /** * class for demo resource plugin extendsall tests * @@ -33,7 +35,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty */ public function testResourcePluginExtendsall() { - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('root' => './templates', './templates_2', './templates_3', './templates_4',)); @@ -51,8 +53,8 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty public function testResourcePluginExtendsall2() { - $this->smarty->setMergeCompiledIncludes(true); - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); + $this->smarty->setMergeCompiledIncludes(true); $this->smarty->setTemplateDir(array('./templates_3', './templates_4',)); $expected = "templates_3templates_3templates_3templates_4"; @@ -61,7 +63,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty public function testResourcePluginExtendsallOne() { - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('root' => './templates', './templates_2', './templates_3', './templates_4',)); @@ -77,7 +79,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty */ public function testResourcePluginExtendsallSpecialApplication() { - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('./templates_2', './templates_1',)); $this->smarty->setDefaultResourceType('extendsall'); $this->assertEquals('

data1 from templates1

data1 from templates2

data2 from templates1

', @@ -92,7 +94,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty */ public function testResourcePluginExtendsallSpecialApplication2() { - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('./templates_3', './templates_2', './templates_1',)); $this->smarty->setDefaultResourceType('extendsall'); $this->assertEquals('

data1 from templates1

data1 from templates3

data1 from templates2

data2 from templates1

', @@ -108,7 +110,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty public function testResourcePluginExtendsallSpecialApplication3() { $this->smarty->setMergeCompiledIncludes(true); - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('./templates_2', './templates_1',)); $this->smarty->setDefaultResourceType('extendsall'); $this->assertEquals('

data1 from templates1

data1 from templates2

data2 from templates1

', @@ -124,7 +126,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty public function testResourcePluginExtendsallSpecialApplication4() { $this->smarty->setMergeCompiledIncludes(true); - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('./templates_2', './templates_1',)); $this->smarty->setDefaultResourceType('extendsall'); $this->assertEquals('

data1 from templates1

data1 from templates2

data2 from templates1

', @@ -140,7 +142,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty public function testResourcePluginExtendsallSpecialApplication5() { $this->smarty->setMergeCompiledIncludes(true); - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('./templates_3', './templates_2', './templates_1',)); $this->smarty->setDefaultResourceType('extendsall'); $this->assertEquals('

data1 from templates1

data1 from templates3

data1 from templates2

data2 from templates1

', @@ -156,7 +158,7 @@ class ResourceExtendsAllPluginTest extends PHPUnit_Smarty public function testResourcePluginExtendsallSpecialApplication6() { $this->smarty->setMergeCompiledIncludes(true); - $this->smarty->addPluginsDir(SMARTY_DIR . "../demo/plugins/"); + $this->smarty->registerResource('extendsall', new My_Resource_Extendsall()); $this->smarty->setTemplateDir(array('./templates_3', './templates_2', './templates_1',)); $this->smarty->setDefaultResourceType('extendsall'); $this->assertEquals('

data1 from templates1

data1 from templates3

data1 from templates2

data2 from templates1

', diff --git a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php deleted file mode 100644 index 393fce2d..00000000 --- a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php +++ /dev/null @@ -1,21 +0,0 @@ -db = PHPUnit_Smarty::$pdo; - } - catch - (PDOException $e) { - throw new Exception('Mysql Resource failed: ' . $e->getMessage()); - } - $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); - } -} - diff --git a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php deleted file mode 100644 index b9e55564..00000000 --- a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php +++ /dev/null @@ -1,22 +0,0 @@ -db = PHPUnit_Smarty::$pdo; - } - catch - (PDOException $e) { - throw new Exception('Mysql Resource failed: ' . $e->getMessage()); - } - $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); - $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); - } -} - diff --git a/tests/UnitTests/ResourceTests/DefaultHandler/DefaultTemplateHandlerTest.php b/tests/UnitTests/ResourceTests/DefaultHandler/DefaultTemplateHandlerTest.php index 64c7174b..61be6e07 100644 --- a/tests/UnitTests/ResourceTests/DefaultHandler/DefaultTemplateHandlerTest.php +++ b/tests/UnitTests/ResourceTests/DefaultHandler/DefaultTemplateHandlerTest.php @@ -93,7 +93,7 @@ class DefaultTemplateHandlerTest extends PHPUnit_Smarty } } -function my_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, Smarty $smarty) +function my_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty) { $output = "Recsource $resource_name of type $resource_type not found"; $template_source = $output; @@ -102,12 +102,12 @@ function my_template_handler($resource_type, $resource_name, &$template_source, return true; } -function my_template_handler_file($resource_type, $resource_name, &$template_source, &$template_timestamp, Smarty $smarty) +function my_template_handler_file($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty) { return $smarty->getTemplateDir(0) . 'helloworld.tpl'; } -function my_false($resource_type, $resource_name, &$template_source, &$template_timestamp, Smarty $smarty) +function my_false($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty) { return false; } diff --git a/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php b/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php deleted file mode 100644 index fdb62085..00000000 --- a/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php +++ /dev/null @@ -1,301 +0,0 @@ -setUpSmarty(__DIR__); - } - - public function testInit() - { - $this->cleanDirs(); - } - - protected function relative($path) - { - $path = str_replace(str_replace("\\", "/", __DIR__), '.', str_replace("\\", "/", $path)); - - return $path; - } - - /** - * test getTemplateFilepath - */ - public function testGetTemplateFilepath() - { - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertEquals($this->normalizePath("./templates/phphelloworld.php"), $tpl->source->filepath); - } - - /** - * test getTemplateTimestamp - */ - public function testGetTemplateTimestamp() - { - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertTrue(is_integer($tpl->source->getTimeStamp())); - $this->assertEquals(10, strlen($tpl->source->getTimeStamp())); - } - - /** - * test getTemplateSource - *-/ - * public function testGetTemplateSource() - * { - * $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - * $this->assertStringContainsString('php hello world', $tpl->source->getContent()); - * } - * /** - * test usesCompiler - */ - public function testUsesCompiler() - { - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertTrue($tpl->source->handler->uncompiled); - } - - /** - * test isEvaluated - */ - public function testIsEvaluated() - { - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertFalse($tpl->source->handler->recompiled); - } - - /** - * test mustCompile - */ - public function testMustCompile() - { - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertFalse($tpl->mustCompile()); - } - - /** - * test getCachedFilepath - */ - public function testGetCachedFilepath() - { - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $expected = $this->buildCachedPath($tpl, false, null, null, 'phphelloworld.php', 'php', - $this->smarty->getTemplateDir(0), 'file'); - $this->assertEquals($expected, $tpl->cached->filepath); - } - - /** - * test create cache file used by the following tests - */ - public function testCreateCacheFile() - { - // create dummy cache file - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertStringContainsString('php hello world', $this->smarty->fetch($tpl)); - } - - /** - * test getCachedTimestamp caching enabled - * - * @runInSeparateProcess - * @preserveGlobalState disabled - * - */ - public function testGetCachedTimestamp() - { - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertTrue(is_integer($tpl->cached->timestamp)); - $this->assertEquals(10, strlen($tpl->cached->timestamp)); - } - - /** - * test isCached - * - * @runInSeparateProcess - * @preserveGlobalState disabled - * - */ - public function testIsCached() - { - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 10000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertTrue($tpl->isCached()); - } - - /** - * test isCached caching disabled - */ - public function testIsCachedCachingDisabled() - { - $this->smarty->setAllowPhpTemplates(true); - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertFalse($tpl->isCached()); - } - - /** - * test isCached on touched source - * - * @runInSeparateProcess - * @preserveGlobalState disabled - * @doesNotPerformAssertions - */ - public function testIsCachedTouchedSourcePrepare() - { - $this->smarty->setAllowPhpTemplates(true); - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - sleep(2); - touch($tpl->source->filepath); - } - - /** - * - * @runInSeparateProcess - * @preserveGlobalState disabled - * - */ - public function testIsCachedTouchedSource() - { - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertFalse($tpl->isCached()); - } - - /** - * test is cache file is written - */ - public function testWriteCachedContent() - { - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $this->cleanCacheDir(); - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->smarty->fetch($tpl); - $this->assertTrue(file_exists($tpl->cached->filepath)); - } - - /** - * test getRenderedTemplate - */ - public function testGetRenderedTemplate() - { - $this->smarty->setAllowPhpTemplates(true); - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertStringContainsString('php hello world', $tpl->fetch()); - } - - /** - * test $smarty->is_cached - * @doesNotPerformAssertions - */ - public function testSmartyIsCachedPrepare() - { - // clean up for next tests - $this->cleanCacheDir(); - $this->smarty->setAllowPhpTemplates(true); - // prepare files for next test - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->smarty->fetch($tpl); - } - - /** - * - * @runInSeparateProcess - * @preserveGlobalState disabled - * - */ - public function testSmartyIsCached() - { - $this->smarty->setAllowPhpTemplates(true); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertTrue($this->smarty->isCached($tpl)); - } - - /** - * test $smarty->is_cached caching disabled - */ - public function testSmartyIsCachedCachingDisabled() - { - $this->smarty->setAllowPhpTemplates(true); - $tpl = $this->smarty->createTemplate('php:phphelloworld.php'); - $this->assertFalse($this->smarty->isCached($tpl)); - } - - public function testGetTemplateFilepathName() - { - $this->smarty->addTemplateDir('./templates_2', 'foo'); - $tpl = $this->smarty->createTemplate('php:[foo]helloworld.php'); - $this->assertEquals('./templates_2/helloworld.php', $this->relative($tpl->source->filepath)); - } - - public function testGetCachedFilepathName() - { - $this->smarty->addTemplateDir('./templates_2', 'foo'); - $this->smarty->caching = true; - $this->smarty->cache_lifetime = 1000; - $tpl = $this->smarty->createTemplate('php:[foo]helloworld.php'); - $path = $tpl->cached->filepath; - $expected = $this->buildCachedPath($tpl, false, null, null, 'helloworld.php', 'php', - $this->smarty->getTemplateDir('foo'), 'file'); - $this->assertEquals($expected, $path); - } - - /** - * test {include} php resource - */ - public function testIncludePhpTemplate() - { - $this->smarty->setAllowPhpTemplates(true); - $this->assertStringContainsString('php hello world', $this->smarty->fetch('includephp.tpl')); - } - - /** - * test {include} php resource caching - */ - public function testIncludePhpTemplateCaching() - { - $this->smarty->caching = true; - $this->smarty->setAllowPhpTemplates(true); - $this->assertStringContainsString('php hello world', $this->smarty->fetch('includephp.tpl')); - } - - /** - * test clearCompiledTemplate() - */ - public function testClearCompiled() - { - $this->smarty->setAllowPhpTemplates(true); - $this->assertEquals(0, $this->smarty->clearCompiledTemplate('php:phphelloworld.php')); - } - -} diff --git a/tests/UnitTests/ResourceTests/Php/cache/.gitignore b/tests/UnitTests/ResourceTests/Php/cache/.gitignore deleted file mode 100644 index d88cc144..00000000 --- a/tests/UnitTests/ResourceTests/Php/cache/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore anything in here, but keep this directory -* diff --git a/tests/UnitTests/ResourceTests/Php/templates/includephp.tpl b/tests/UnitTests/ResourceTests/Php/templates/includephp.tpl deleted file mode 100644 index 5baa76d2..00000000 --- a/tests/UnitTests/ResourceTests/Php/templates/includephp.tpl +++ /dev/null @@ -1 +0,0 @@ -{include 'php:phphelloworld.php'} \ No newline at end of file diff --git a/tests/UnitTests/ResourceTests/Php/templates/phphelloworld.php b/tests/UnitTests/ResourceTests/Php/templates/phphelloworld.php deleted file mode 100644 index 6fe5b4ba..00000000 --- a/tests/UnitTests/ResourceTests/Php/templates/phphelloworld.php +++ /dev/null @@ -1 +0,0 @@ -php hello world diff --git a/tests/UnitTests/ResourceTests/Php/templates_2/helloworld.php b/tests/UnitTests/ResourceTests/Php/templates_2/helloworld.php deleted file mode 100644 index 6fe5b4ba..00000000 --- a/tests/UnitTests/ResourceTests/Php/templates_2/helloworld.php +++ /dev/null @@ -1 +0,0 @@ -php hello world diff --git a/tests/UnitTests/ResourceTests/Php/templates_c/.gitignore b/tests/UnitTests/ResourceTests/Php/templates_c/.gitignore deleted file mode 100644 index d88cc144..00000000 --- a/tests/UnitTests/ResourceTests/Php/templates_c/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore anything in here, but keep this directory -* diff --git a/tests/UnitTests/SmartyMethodsTests/ClearCompiledTemplate/ClearCompiledTest.php b/tests/UnitTests/SmartyMethodsTests/ClearCompiledTemplate/ClearCompiledTest.php index 0944928a..e902c9a0 100644 --- a/tests/UnitTests/SmartyMethodsTests/ClearCompiledTemplate/ClearCompiledTest.php +++ b/tests/UnitTests/SmartyMethodsTests/ClearCompiledTemplate/ClearCompiledTest.php @@ -71,9 +71,6 @@ class ClearCompiledTest extends PHPUnit_Smarty $this->_files[$template . '#' . $compile_id] = substr($tpl->compiled->filepath, $directory_length); } } - // TODO - //Smarty::$_resource_cache = array(); - //$this->getSmartyObj()->_cache['template_objects'] = array(); return $this->_files; } diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php index d9b2ece1..a26e50bf 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\Smarty; + /** * class for register->block / unregister->block methods tests * @@ -31,21 +33,21 @@ class RegisterBlockTest extends PHPUnit_Smarty */ public function testRegisterBlockFunction() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); $this->smarty->assign('value', 1); $this->assertEquals('function hello world 1 1 function hello world 1 2 function hello world 1 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}')); } public function testRegisterBlockFunctionModifier1() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); $this->smarty->assign('value', 1); $this->assertEquals(strtoupper('function hello world 1 1 function hello world 1 2 function hello world 1 3 '), $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock|upper}')); } public function testRegisterBlockFunctionModifier2() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); $this->smarty->assign('value', 1); $this->assertEquals(strtoupper('function hello world 1 1 function hello world 1 2 function hello world 1 3 '), $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock|default:""|upper}')); } @@ -62,7 +64,7 @@ class RegisterBlockTest extends PHPUnit_Smarty */ public function testRegisterBlockClass() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', array('myblockclass', 'static_method')); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', array('myblockclass', 'static_method')); $this->smarty->assign('value', 2); $this->assertEquals('static hello world 2 1 static hello world 2 2 static hello world 2 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}')); } @@ -80,7 +82,7 @@ class RegisterBlockTest extends PHPUnit_Smarty public function testRegisterBlockObject() { $myblock_object = new myblockclass; - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', array($myblock_object, 'object_method')); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', array($myblock_object, 'object_method')); $this->smarty->assign('value', 3); $this->assertEquals('object hello world 3 1 object hello world 3 2 object hello world 3 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}')); } @@ -104,7 +106,7 @@ class RegisterBlockTest extends PHPUnit_Smarty $this->smarty->assign('x', 1); $this->smarty->assign('y', 10); $this->smarty->assign('z', 100); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache'); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache'); $this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl')); } @@ -120,7 +122,7 @@ class RegisterBlockTest extends PHPUnit_Smarty $this->smarty->assign('x', 2); $this->smarty->assign('y', 20); $this->smarty->assign('z', 200); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache'); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache'); $this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl')); } @@ -137,7 +139,7 @@ class RegisterBlockTest extends PHPUnit_Smarty $this->smarty->assign('x', 3); $this->smarty->assign('y', 30); $this->smarty->assign('z', 300); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false); $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl')); } @@ -153,7 +155,7 @@ class RegisterBlockTest extends PHPUnit_Smarty $this->smarty->assign('x', 4); $this->smarty->assign('y', 40); $this->smarty->assign('z', 400); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false); $this->assertEquals('3 40 300', $this->smarty->fetch('test_register_block.tpl')); } @@ -228,16 +230,16 @@ class RegisterBlockTest extends PHPUnit_Smarty */ public function testUnregisterBlock() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_BLOCK]['testblock'])); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock'); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_BLOCK, 'testblock'); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testblock')); } public function testUnregisterBlockWrapper() { $this->smarty->registerPlugin('block', 'testblock', 'myblock'); $this->smarty->unregisterPlugin('block', 'testblock'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_BLOCK]['testblock'])); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testblock')); } /** @@ -245,8 +247,8 @@ class RegisterBlockTest extends PHPUnit_Smarty */ public function testUnregisterBlockNotRegistered() { - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testblock'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_BLOCK]['testblock'])); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_BLOCK, 'testblock'); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testblock')); } } diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php index 376c8207..0100cd4a 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\Smarty; + /** * class for register->compilerFunction / unregister->compilerFunction methods tests * @@ -30,8 +32,8 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty */ public function testRegisterCompilerFunction() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction'); - $this->assertEquals('mycompilerfunction', $this->smarty->registered_plugins['compiler']['testcompilerfunction'][0]); + $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction'); + $this->assertEquals('mycompilerfunction', $this->smarty->getRegisteredPlugin('compiler', 'testcompilerfunction')[0]); $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testcompilerfunction var=1}')); } @@ -40,8 +42,8 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty */ public function testRegisterCompilerFunctionBlock() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'foo', 'mycompilerfunctionopen'); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'fooclose', 'mycompilerfunctionclose'); + $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'foo', 'mycompilerfunctionopen'); + $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'fooclose', 'mycompilerfunctionclose'); $result = $this->smarty->fetch('eval:{foo} hallo {/foo}'); $this->assertEquals('open tag hallo close tag', $result); } @@ -51,7 +53,7 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty */ public function testRegisterCompilerFunctionClass() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array('mycompilerfunctionclass', 'execute')); + $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array('mycompilerfunctionclass', 'execute')); $this->assertEquals('hello world 2', $this->smarty->fetch('eval:{testcompilerfunction var1=2}')); } @@ -61,7 +63,7 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty public function testRegisterCompilerFunctionObject() { $obj = new mycompilerfunctionclass; - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array($obj, 'compile')); + $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array($obj, 'compile')); $this->assertEquals('hello world 3', $this->smarty->fetch('eval:{testcompilerfunction var2=3}')); } @@ -70,9 +72,9 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty */ public function testUnregisterCompilerFunction() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction'); - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_COMPILER]['testcompilerfunction'])); + $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction'); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction'); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction')); } /** @@ -80,8 +82,8 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty */ public function testUnregisterCompilerFunctionNotRegistered() { - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_COMPILER]['testcompilerfunction'])); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction'); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction')); } /** @@ -89,9 +91,9 @@ class RegisterCompilerFunctionTest extends PHPUnit_Smarty */ public function testUnregisterCompilerFunctionOtherRegistered() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testcompilerfunction', 'mycompilerfunction'); - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_COMPILER, 'testcompilerfunction'); - $this->assertTrue(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_BLOCK]['testcompilerfunction'])); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testcompilerfunction', 'mycompilerfunction'); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction'); + $this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testcompilerfunction')); } } diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php index 97958eb4..8178e140 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\Smarty; + /** * class for register/unregister function plugins methods tests * @@ -31,9 +33,9 @@ class RegisterFunctionTest extends PHPUnit_Smarty */ public function testRegisterFunction() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); $this->assertEquals('myfunction', - $this->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_FUNCTION ][ 'testfunction' ][ 0 ]); + $this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction')[0]); $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testfunction value=1}')); } @@ -42,7 +44,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty */ public function testRegisterFunctionClass() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', array('myfunctionclass', 'execute')); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', array('myfunctionclass', 'execute')); $this->assertEquals('hello world 2', $this->smarty->fetch('eval:{testfunction value=2}')); } @@ -52,7 +54,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty public function testRegisterFunctionObject() { $myfunction_object = new myfunctionclass; - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', array($myfunction_object, 'execute')); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', array($myfunction_object, 'execute')); $this->assertEquals('hello world 3', $this->smarty->fetch('eval:{testfunction value=3}')); } @@ -69,7 +71,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty $this->smarty->setForceCompile(true); $this->smarty->assign('x', 0); $this->smarty->assign('y', 10); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); $this->assertEquals('hello world 0 10', $this->smarty->fetch('test_register_function.tpl')); } @@ -86,7 +88,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty $this->smarty->cache_lifetime = 1000; $this->smarty->assign('x', 1); $this->smarty->assign('y', 20); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); $this->assertEquals('hello world 0 10', $this->smarty->fetch('test_register_function.tpl')); } @@ -104,7 +106,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty $this->smarty->setForceCompile(true); $this->smarty->assign('x', 2); $this->smarty->assign('y', 30); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction', false); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction', false); $this->assertEquals('hello world 2 30', $this->smarty->fetch('test_register_function.tpl')); } @@ -121,7 +123,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty $this->smarty->cache_lifetime = 1000; $this->smarty->assign('x', 3); $this->smarty->assign('y', 40); - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction', false); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction', false); $this->assertEquals('hello world 3 30', $this->smarty->fetch('test_register_function.tpl')); } @@ -130,28 +132,28 @@ class RegisterFunctionTest extends PHPUnit_Smarty */ public function testUnregisterFunction() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction'); - $this->assertFalse(isset($this->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_FUNCTION ][ 'testfunction' ])); + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction'); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction')); } /** - * test test unregisterPlugin method for function not registered + * test unregisterPlugin method for function not registered */ public function testUnregisterFunctionNotRegistered() { - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction'); - $this->assertFalse(isset($this->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_FUNCTION ][ 'testfunction' ])); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction')); } /** - * test test unregisterPlugin method for function other registered + * test unregisterPlugin method for function other registered */ public function testUnregisterFunctionOtherRegistered() { - $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testfunction', 'myfunction'); - $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'testfunction'); - $this->assertTrue(isset($this->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_BLOCK ][ 'testfunction' ])); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testfunction', 'myfunction'); + $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'); + $this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testfunction')); } } diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php index fd8392c7..fbbdae86 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php @@ -31,7 +31,7 @@ class RegisterModifierTest extends PHPUnit_Smarty public function testRegisterModifier() { $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier'); - $this->assertEquals('mymodifier', $this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIER]['testmodifier'][0]); + $this->assertEquals('mymodifier', $this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier')[0]); $this->smarty->assign('foo', 'foo'); $this->smarty->assign('bar', 'bar'); $this->assertEquals('foo function blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}')); @@ -67,7 +67,7 @@ class RegisterModifierTest extends PHPUnit_Smarty { $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier'); $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIER]['testmodifier'])); + $this->assertNull($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier')); } /** @@ -76,7 +76,7 @@ class RegisterModifierTest extends PHPUnit_Smarty public function testUnregisterModifierNotRegistered() { $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'); - $this->assertFalse(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_MODIFIER]['testmodifier'])); + $this->assertNull($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier')); } /** @@ -86,7 +86,7 @@ class RegisterModifierTest extends PHPUnit_Smarty { $this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier', 'mymodifier'); $this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'); - $this->assertTrue(isset($this->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_BLOCK]['testmodifier'])); + $this->assertIsArray($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier')); } } diff --git a/tests/UnitTests/TemplateSource/Comments/CommentsTest.php b/tests/UnitTests/TemplateSource/Comments/CommentsTest.php index b8bd2cc8..b0a8ec6c 100644 --- a/tests/UnitTests/TemplateSource/Comments/CommentsTest.php +++ b/tests/UnitTests/TemplateSource/Comments/CommentsTest.php @@ -36,7 +36,7 @@ class CommentsTest extends PHPUnit_Smarty $name = empty($testName) ? $testNumber : $testName; $file = "testComments_{$name}.tpl"; $this->makeTemplateFile($file, $code); - $this->smarty->template_dir = './templates_tmp'; + $this->smarty->setTemplateDir('./templates_tmp'); $this->assertEquals($result, $this->smarty->fetch($file), $file); diff --git a/tests/UnitTests/TemplateSource/Spacing/SpacingTest.php b/tests/UnitTests/TemplateSource/Spacing/SpacingTest.php index 7f92fb03..ff22a0a9 100644 --- a/tests/UnitTests/TemplateSource/Spacing/SpacingTest.php +++ b/tests/UnitTests/TemplateSource/Spacing/SpacingTest.php @@ -37,7 +37,7 @@ class SpacingTest extends PHPUnit_Smarty $name = empty($testName) ? $testNumber : $testName; $file = "Spacing_{$name}.tpl"; $this->makeTemplateFile($file, $code); - $this->smarty->template_dir = './templates_tmp'; + $this->smarty->setTemplateDir('./templates_tmp'); $this->smarty->assign('file', $file); $this->smarty->assign('foo', 'bar'); $this->assertEquals($result, diff --git a/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php b/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php index 293c9074..fd2c1224 100644 --- a/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php @@ -248,19 +248,6 @@ class CompileBlockPluginTest extends PHPUnit_Smarty $this->assertEquals('defaultblock hello world', $this->smarty->fetch('default2.tpl')); } - /** - * test tag stack - * - * @runInSeparateProcess - * @preserveGlobalState disabled - * - */ - public function testBlockPluginTagStack() - { - $this->assertEquals('noop-teststack', $this->smarty->fetch('tag_stack.tpl')); - $this->assertEmpty($this->smarty->_cache[ '_tag_stack' ]); - } - /** * Test caching * diff --git a/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/PHPunitplugins/block.teststack.php b/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/PHPunitplugins/block.teststack.php deleted file mode 100644 index f1eef38d..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/PHPunitplugins/block.teststack.php +++ /dev/null @@ -1,25 +0,0 @@ -smarty->_cache['_tag_stack']; - return $stack[0][0] . '-' . $stack[1][0]; - } -} diff --git a/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.test.php b/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.test.php index 07cd9d40..5fb9ff83 100644 --- a/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.test.php +++ b/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.test.php @@ -5,8 +5,8 @@ use Smarty\Compile\Tag\Base; class smarty_compiler_test extends Base { - public function compile($args, $compiler) - { + + public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { $this->required_attributes = array('data'); $_attr = $this->getAttributes($compiler, $args); @@ -16,15 +16,3 @@ class smarty_compiler_test extends Base return ""; } } - -// compiler.testclose.php -class smarty_compiler_testclose extends Base -{ - public function compile($args, $compiler) - { - - $this->closeTag($compiler, 'test'); - - return ''; - } -} diff --git a/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.testclose.php b/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.testclose.php index 32d8b377..a18278f4 100644 --- a/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.testclose.php +++ b/tests/UnitTests/TemplateSource/TagTests/CompilerPlugin/PHPunitplugins/compiler.testclose.php @@ -5,11 +5,11 @@ use Smarty\Compile\Tag\Base; class smarty_compiler_testclose extends Base { - public function execute($args, $compiler) - { - $this->closeTag($compiler, 'test'); + public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { - return ''; - } -} + $this->closeTag($compiler, 'test'); + + return ''; + } +} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/_Issues/549/MbSplitEncodingIssue549Test.php b/tests/UnitTests/TemplateSource/_Issues/549/MbSplitEncodingIssue549Test.php index 02d09810..cadecb6f 100644 --- a/tests/UnitTests/TemplateSource/_Issues/549/MbSplitEncodingIssue549Test.php +++ b/tests/UnitTests/TemplateSource/_Issues/549/MbSplitEncodingIssue549Test.php @@ -30,13 +30,13 @@ class MbSplitEncodingIssue549Test extends PHPUnit_Smarty public function setUp(): void { - $this->charset = \Smarty::$_CHARSET; + $this->charset = \Smarty\Smarty::$_CHARSET; $this->setUpSmarty(__DIR__); } protected function tearDown(): void { - \Smarty::$_CHARSET = $this->charset ?: \Smarty::$_CHARSET; + \Smarty\Smarty::$_CHARSET = $this->charset ?: \Smarty\Smarty::$_CHARSET; $this->cleanDirs(); } @@ -65,7 +65,7 @@ class MbSplitEncodingIssue549Test extends PHPUnit_Smarty \extract($data, \EXTR_SKIP); \mb_regex_encoding($mb_regex_encoding); - \Smarty::$_CHARSET = $mb_int_encoding; + \Smarty\Smarty::$_CHARSET = $mb_int_encoding; $this->assertEquals($result, $this->smarty->fetch("string:{\"$subject\"|replace:\"$pattern\":\"$replacement\"}")); } diff --git a/tests/UnitTests/TemplateSource/_Issues/topic26878/NewlineSpacing.php b/tests/UnitTests/TemplateSource/_Issues/topic26878/NewlineSpacing.php index 6fcf32ac..f960ceb7 100644 --- a/tests/UnitTests/TemplateSource/_Issues/topic26878/NewlineSpacing.php +++ b/tests/UnitTests/TemplateSource/_Issues/topic26878/NewlineSpacing.php @@ -37,7 +37,7 @@ class NewlineSpacing extends PHPUnit_Smarty $name = empty($testName) ? $testNumber : $testName; $file = "Spacing_{$name}.tpl"; $this->makeTemplateFile($file, $code); - $this->smarty->template_dir = './templates_tmp'; + $this->smarty->setTemplateDir('./templates_tmp'); $this->smarty->assign('file', $file); $this->smarty->assign('foo', 'bar'); $this->assertEquals($result, diff --git a/tests/UnitTests/__shared/PHPunitplugins/compiler.getparamsshort.php b/tests/UnitTests/__shared/PHPunitplugins/compiler.getparamsshort.php index 210ff6ac..226edfef 100644 --- a/tests/UnitTests/__shared/PHPunitplugins/compiler.getparamsshort.php +++ b/tests/UnitTests/__shared/PHPunitplugins/compiler.getparamsshort.php @@ -41,7 +41,7 @@ class smarty_compiler_getparamsshort extends Base */ public $shorttag_order = array('s1', 's2', 's3'); - public function compile($args, $compiler) + public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { $_attr = $this->getAttributes($compiler, $args); $output = '