Compare commits

..

1 Commits

Author SHA1 Message Date
Simon Wisselink 23b3c21d73 rebase of j-applese3d:smarty5 2024-03-18 16:00:35 +01:00
18 changed files with 1495 additions and 1372 deletions
-13
View File
@@ -6,19 +6,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [5.0.2] - 2024-03-28
- Fix Smarty::assign() not returning $this when called with an array as first parameter [#972](https://github.com/smarty-php/smarty/pull/972)
## [5.0.1] - 2024-03-27
- Fix error in Smarty\Smarty::compileAllTemplates() by including missing FilesystemIterator class [#966](https://github.com/smarty-php/smarty/issues/966)
## [5.0.0] - 2024-03-25
- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952)
- Removed publicly accessible `$tpl->_var_stack` variable
### Fixed
- Too many shorthand attributes error when using a modifier as a function with more than 3 parameters in an expression [#949](https://github.com/smarty-php/smarty/issues/949)
+2
View File
@@ -0,0 +1,2 @@
- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952)
- Removed publicly accessible `$tpl->_var_stack` variable
-1
View File
@@ -1 +0,0 @@
- Fix warning when calling hasVariable for an undefined variable [#977](https://github.com/smarty-php/smarty/issues/977)
+1 -1
View File
@@ -15,7 +15,7 @@ php utilities/update-smarty-version-number.php $1
git add changelog CHANGELOG.md src/Smarty.php
git commit -m "version bump"
git checkout support/5
git checkout master
git pull
git merge --no-ff "release/$1"
git branch -d "release/$1"
+2 -2
View File
@@ -109,7 +109,7 @@ class Data
foreach ($tpl_var as $_key => $_val) {
$this->assign($_key, $_val, $nocache, $scope);
}
return $this;
return;
}
switch ($scope ?? $this->getDefaultScope()) {
case self::SCOPE_GLOBAL:
@@ -290,7 +290,7 @@ class Data
* @return bool
*/
public function hasVariable($varName): bool {
return !($this->getVariable($varName, true, false) instanceof UndefinedVariable);
return !($this->getVariable($varName) instanceof UndefinedVariable);
}
/**
+29 -23
View File
@@ -131,6 +131,7 @@ class TemplateLexer
'OPENB' => '[',
'CLOSEB' => ']',
'PTR' => '->',
'NSPTR' => '?->',
'APTR' => '=>',
'EQUAL' => '=',
'NUMBER' => 'number',
@@ -567,7 +568,7 @@ class TemplateLexer
public function yylex3()
{
if (!isset($this->yy_global_pattern3)) {
$this->yy_global_pattern3 = $this->replace("/\G(\\s*SMARTYrdel)|\G((SMARTYldel)SMARTYal)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even|div)\\s+by\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G([!]\\s*|not\\s+)|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G(array\\s*[(]\\s*)|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|][@]?)|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS");
$this->yy_global_pattern3 = $this->replace("/\G(\\s*SMARTYrdel)|\G((SMARTYldel)SMARTYal)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even|div)\\s+by\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G([!]\\s*|not\\s+)|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[?][-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G(array\\s*[(]\\s*)|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|][@]?)|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS");
}
if (!isset($this->dataLength)) {
$this->dataLength = strlen($this->data);
@@ -737,44 +738,49 @@ class TemplateLexer
public function yy_r3_33()
{
$this->token = \Smarty\Parser\TemplateParser::TP_APTR;
$this->token = \Smarty\Parser\TemplateParser::TP_NSPTR;
}
public function yy_r3_34()
{
$this->token = \Smarty\Parser\TemplateParser::TP_EQUAL;
$this->token = \Smarty\Parser\TemplateParser::TP_APTR;
}
public function yy_r3_35()
{
$this->token = \Smarty\Parser\TemplateParser::TP_EQUAL;
}
public function yy_r3_36()
{
$this->token = \Smarty\Parser\TemplateParser::TP_INCDEC;
}
public function yy_r3_37()
public function yy_r3_38()
{
$this->token = \Smarty\Parser\TemplateParser::TP_UNIMATH;
}
public function yy_r3_39()
public function yy_r3_40()
{
$this->token = \Smarty\Parser\TemplateParser::TP_MATH;
}
public function yy_r3_41()
public function yy_r3_42()
{
$this->token = \Smarty\Parser\TemplateParser::TP_AT;
}
public function yy_r3_42()
public function yy_r3_43()
{
$this->token = \Smarty\Parser\TemplateParser::TP_ARRAYOPEN;
}
public function yy_r3_43()
public function yy_r3_44()
{
$this->token = \Smarty\Parser\TemplateParser::TP_HATCH;
}
public function yy_r3_44()
public function yy_r3_45()
{
// resolve conflicts with shorttag and right_delimiter starting with '='
@@ -786,73 +792,73 @@ class TemplateLexer
$this->token = \Smarty\Parser\TemplateParser::TP_ATTR;
}
}
public function yy_r3_45()
public function yy_r3_46()
{
$this->token = \Smarty\Parser\TemplateParser::TP_NAMESPACE;
}
public function yy_r3_48()
public function yy_r3_49()
{
$this->token = \Smarty\Parser\TemplateParser::TP_ID;
}
public function yy_r3_49()
public function yy_r3_50()
{
$this->token = \Smarty\Parser\TemplateParser::TP_INTEGER;
}
public function yy_r3_50()
public function yy_r3_51()
{
$this->token = \Smarty\Parser\TemplateParser::TP_BACKTICK;
$this->yypopstate();
}
public function yy_r3_51()
public function yy_r3_52()
{
$this->token = \Smarty\Parser\TemplateParser::TP_VERT;
}
public function yy_r3_52()
public function yy_r3_53()
{
$this->token = \Smarty\Parser\TemplateParser::TP_DOT;
}
public function yy_r3_53()
public function yy_r3_54()
{
$this->token = \Smarty\Parser\TemplateParser::TP_COMMA;
}
public function yy_r3_54()
public function yy_r3_55()
{
$this->token = \Smarty\Parser\TemplateParser::TP_SEMICOLON;
}
public function yy_r3_55()
public function yy_r3_56()
{
$this->token = \Smarty\Parser\TemplateParser::TP_DOUBLECOLON;
}
public function yy_r3_56()
public function yy_r3_57()
{
$this->token = \Smarty\Parser\TemplateParser::TP_COLON;
}
public function yy_r3_57()
public function yy_r3_58()
{
$this->token = \Smarty\Parser\TemplateParser::TP_QMARK;
}
public function yy_r3_58()
public function yy_r3_59()
{
$this->token = \Smarty\Parser\TemplateParser::TP_HEX;
}
public function yy_r3_59()
public function yy_r3_60()
{
$this->token = \Smarty\Parser\TemplateParser::TP_SPACE;
}
public function yy_r3_60()
public function yy_r3_61()
{
$this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
+5
View File
@@ -131,6 +131,7 @@ class TemplateLexer
'OPENB' => '[',
'CLOSEB' => ']',
'PTR' => '->',
'NSPTR' => '?->',
'APTR' => '=>',
'EQUAL' => '=',
'NUMBER' => 'number',
@@ -310,6 +311,7 @@ class TemplateLexer
equal = ~\s*[=]\s*~
space = ~\s+~
ptr = ~\s*[-][>]\s*~
nsptr = ~\s*[?][-][>]\s*~
aptr = ~\s*[=][>]\s*~
singlequotestring = ~'[^'\\]*(?:\\.[^'\\]*)*'~
backtick = ~[`]~
@@ -514,6 +516,9 @@ class TemplateLexer
ptr {
$this->token = \Smarty\Parser\TemplateParser::TP_PTR;
}
nsptr {
$this->token = \Smarty\Parser\TemplateParser::TP_NSPTR;
}
aptr {
$this->token = \Smarty\Parser\TemplateParser::TP_APTR;
}
+1355 -1271
View File
File diff suppressed because it is too large Load Diff
+44
View File
@@ -432,6 +432,16 @@ tag(res) ::= LDEL ID(i) PTR ID(me) modifierlist(l) attributes(a). {
res = $this->compiler->compileTag(i,a,array('modifierlist'=>l, 'object_method'=>me));
}
// registered ns object tag
tag(res) ::= LDEL ID(i) NSPTR ID(m) attributes(a). {
res = $this->compiler->compileTag(i,a,array('object_method'=>m));
}
// registered ns object tag with modifiers
tag(res) ::= LDEL ID(i) NSPTR ID(me) modifierlist(l) attributes(a). {
res = $this->compiler->compileTag(i,a,array('modifierlist'=>l, 'object_method'=>me));
}
// {if}, {elseif} and {while} tag
tag(res) ::= LDELIF(i) expr(ie). {
$tag = trim(substr(i,$this->compiler->getLdelLength()));
@@ -1056,6 +1066,40 @@ objectelement(res)::= PTR method(f). {
res = '->'.f;
}
// variable
objectelement(res)::= NSPTR ID(i) arrayindex(a). {
if ($this->security && substr(i,0,1) === '_') {
$this->compiler->trigger_template_error (self::ERR1);
}
res = '?->'.i.a;
}
objectelement(res)::= NSPTR varvar(v) arrayindex(a). {
if ($this->security) {
$this->compiler->trigger_template_error (self::ERR2);
}
res = '?->{'.$this->compiler->compileVariable(v).a.'}';
}
objectelement(res)::= NSPTR LDEL expr(e) RDEL arrayindex(a). {
if ($this->security) {
$this->compiler->trigger_template_error (self::ERR2);
}
res = '?->{'.e.a.'}';
}
objectelement(res)::= NSPTR ID(ii) LDEL expr(e) RDEL arrayindex(a). {
if ($this->security) {
$this->compiler->trigger_template_error (self::ERR2);
}
res = '?->{\''.ii.'\'.'.e.a.'}';
}
// method
objectelement(res)::= NSPTR method(f). {
res = '?->'.f;
}
//
// function
+13 -15
View File
@@ -2,7 +2,6 @@
namespace Smarty;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Smarty\Cacheresource\File;
@@ -13,12 +12,11 @@ use Smarty\Extension\CoreExtension;
use Smarty\Extension\DefaultExtension;
use Smarty\Extension\ExtensionInterface;
use Smarty\Filter\Output\TrimWhitespace;
use Smarty\Runtime\CaptureRuntime;
use Smarty\Runtime\DefaultPluginHandlerRuntime;
use Smarty\Runtime\ForeachRuntime;
use Smarty\Runtime\InheritanceRuntime;
use Smarty\Runtime\TplFunctionRuntime;
use Smarty\Resource\BasePlugin;
use Smarty\Smarty\Runtime\CaptureRuntime;
use Smarty\Smarty\Runtime\ForeachRuntime;
use Smarty\Smarty\Runtime\InheritanceRuntime;
use Smarty\Smarty\Runtime\TplFunctionRuntime;
/**
* Project: Smarty: the PHP compiling template engine
@@ -55,7 +53,7 @@ class Smarty extends \Smarty\TemplateBase {
/**
* smarty version
*/
const SMARTY_VERSION = '5.0.2';
const SMARTY_VERSION = '5.0.0-rc3';
/**
* define caching modes
@@ -1757,15 +1755,15 @@ class Smarty extends \Smarty\TemplateBase {
// Lazy load runtimes when/if needed
switch ($type) {
case 'Capture':
return $this->runtimes[$type] = new CaptureRuntime();
return $this->runtimes[$type] = new \Smarty\Runtime\CaptureRuntime();
case 'Foreach':
return $this->runtimes[$type] = new ForeachRuntime();
return $this->runtimes[$type] = new \Smarty\Runtime\ForeachRuntime();
case 'Inheritance':
return $this->runtimes[$type] = new InheritanceRuntime();
return $this->runtimes[$type] = new \Smarty\Runtime\InheritanceRuntime();
case 'TplFunction':
return $this->runtimes[$type] = new TplFunctionRuntime();
return $this->runtimes[$type] = new \Smarty\Runtime\TplFunctionRuntime();
case 'DefaultPluginHandler':
return $this->runtimes[$type] = new DefaultPluginHandlerRuntime(
return $this->runtimes[$type] = new \Smarty\Runtime\DefaultPluginHandlerRuntime(
$this->getDefaultPluginHandlerFunc()
);
}
@@ -2054,7 +2052,7 @@ class Smarty extends \Smarty\TemplateBase {
* @param array|string $modifiers modifier or list of modifiers
* to add
*
* @return Smarty
* @return \Smarty|Template
* @api Smarty::addDefaultModifiers()
*
*/
@@ -2133,7 +2131,7 @@ class Smarty extends \Smarty\TemplateBase {
* @throws \Smarty\Exception
*/
public function display($template = null, $cache_id = null, $compile_id = null) {
$this->returnOrCreateTemplate($template, $cache_id, $compile_id)->display();
return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->display();
}
/**
+2 -2
View File
@@ -377,9 +377,9 @@ abstract class TemplateBase extends Data {
* Registers a resource to fetch a template
*
* @param string $name name of resource type
* @param \Smarty\Resource\BasePlugin $resource_handler instance of Smarty\Resource\BasePlugin
* @param Smarty\Resource\Base $resource_handler instance of Smarty\Resource\Base
*
* @return \Smarty\Smarty|\Smarty\Template
* @return \Smarty|\Smarty\Template
* @link https://www.smarty.net/docs/en/api.register.resource.tpl
*
* @api Smarty::registerResource()
+1 -1
View File
@@ -66,7 +66,7 @@ function smarty_make_timestamp($string)
|| (interface_exists('DateTimeInterface', false) && $string instanceof DateTimeInterface)
) {
return (int)$string->format('U'); // PHP 5.2 BC
} elseif (strlen($string) === 14 && ctype_digit((string)$string)) {
} elseif (strlen($string) === 14 && ctype_digit($string)) {
// it is mysql timestamp format of YYYYMMDDHHMMSS?
return mktime(
substr($string, 8, 2),
@@ -42,15 +42,4 @@ class AssignTest extends PHPUnit_Smarty
$this->smarty->assign(array('foo' => 'bar', 'foo2' => 'bar2'));
$this->assertEquals('bar bar2', $this->smarty->fetch('eval:{$foo} {$foo2}'));
}
/**
* Test that assign returns this.
*/
public function testAssignReturnsThis()
{
$this->assertEquals(
'data',
$this->smarty->assign(['dummy' => 'data'])->fetch('eval:{$dummy}')
);
}
}
@@ -0,0 +1,2 @@
# Ignore anything in here, but keep this directory
*
@@ -0,0 +1,2 @@
# Ignore anything in here, but keep this directory
*
@@ -1,32 +0,0 @@
<?php
/**
* Tests the ::hasVariable method
*/
class HasVariableTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testInit()
{
$this->cleanDirs();
}
public function testSimpleTrue()
{
$this->smarty->assign('foo', 'bar');
$this->assertTrue($this->smarty->hasVariable('foo'));
}
public function testSimpleFalse()
{
$this->smarty->assign('foo', 'bar');
$this->assertFalse($this->smarty->hasVariable('foox'));
}
}
@@ -93,6 +93,33 @@ class ObjectVariableTest extends PHPUnit_Smarty
$tpl->assign('object', $object);
$this->assertEquals('hello world', $this->smarty->fetch($tpl));
}
public function testNullSafeOperatorSimple()
{
$object = new VariableObject;
$tpl = $this->smarty->createTemplate('string:{$object?->hello}');
$tpl->assign('object', $object);
$this->assertEquals('hello_world', $this->smarty->fetch($tpl));
$object = null;
$tpl = $this->smarty->createTemplate('string:{$object?->hello}');
$tpl->assign('object', $object);
$this->assertEquals((string)null, $this->smarty->fetch($tpl));
}
public function testNullSafeOperatorChaining()
{
$object = new VariableObject;
$tpl = $this->smarty->createTemplate('string:{$object?->returnSelf()?->myhello()}');
$tpl->assign('object', $object);
$this->assertEquals('hello world', $this->smarty->fetch($tpl));
$tpl = $this->smarty->createTemplate('string:{$object?->returnNull()?->myhello()}');
$tpl->assign('object', $object);
$this->assertEquals((string)null, $this->smarty->fetch($tpl));
}
}
Class VariableObject
@@ -103,4 +130,14 @@ Class VariableObject
{
return 'hello world';
}
public function returnSelf()
{
return $this;
}
public function returnNull()
{
return null;
}
}