From 66e9dcfb4fe67cdcfa9e4e683e8f957c6aa720dc Mon Sep 17 00:00:00 2001 From: Marion Newlevant Date: Thu, 19 May 2022 22:33:54 -0700 Subject: [PATCH] Update for Craft 4 (Twig 3) --- README.md | 11 +++++---- composer.json | 9 ++++--- src/twigextensions/Array_Test.php | 4 ++-- src/twigextensions/Break_Node.php | 4 ++-- src/twigextensions/Break_TokenParser.php | 7 +++--- src/twigextensions/Continue_Node.php | 4 ++-- src/twigextensions/Continue_TokenParser.php | 6 ++--- .../Expression_Binary_Equivalent.php | 4 ++-- .../Expression_Binary_NotEquivalent.php | 4 ++-- .../Expression_Binary_Spaceship.php | 4 ++-- src/twigextensions/Numeric_Test.php | 4 ++-- src/twigextensions/Return_Node.php | 4 ++-- src/twigextensions/Return_TokenParser.php | 8 +++---- src/twigextensions/String_Test.php | 4 ++-- .../TwigPerversionTwigExtension.php | 24 +++++++++---------- src/twigextensions/While_TokenParser.php | 16 ++++++------- 16 files changed, 60 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 7d31d01..819895d 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ or A macro with a `{% return %}` tag will return whatever the return value is (which can be a complex expression). Any other output generated by the macro will be discarded. ### Operators + - **===** and **!==** Compares two values for equivalence, using php's `===` and `!==` operators. @@ -94,11 +95,11 @@ or Compares two values using php's [`<=>` (spaceship)](http://php.net/manual/en/migration70.new-features.php#migration70.new-features.spaceship-op) operator. Returns `-1`, `0`, or `1` when the first argument is respectively less than, equal to, or greater than the second. ### Tests + - **Numeric** Test whether given value is numeric (behaviour like PHP 7 `is_numeric`). (Note that as of PHP 7, hexadecimal strings are not considered numeric) - ```Twig {{ 12 is numeric ? 'Yes' : 'No' }} @@ -114,19 +115,19 @@ or - **String** Test whether given value is a string. - + ```Twig {{ '12' is string ? 'Yes' : 'No' }} {# Yes #} {{ 12 is string ? 'Yes' : 'No' }} {# No #} -{``` +``` - **Array** Test whether given value is an array. - + ```Twig { [] is array ? 'Yes' : 'No' }} {# Yes #} @@ -135,8 +136,8 @@ or {# No #} ``` - ### Filters + - **array_splice** Remove a portion of an array and replace it with something else. Uses the PHP [array_splice](http://php.net/manual/en/function.array-splice.php) function. Note that unlike the php function, this filter returns the modified array rather than the extracted elements. The original array is unchanged. Since the implementation requires copying the array, this will be less efficient than the raw php function. The **array_splice** filter is passed an `offset`, an optional `length`, and an optional `replacement`. diff --git a/composer.json b/composer.json index 6e873d2..42cc829 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,13 @@ { "name": "marionnewlevant/twig-perversion", "description": "Making twig do things it really shouldn't", - "version": "2.2.0", + "version": "3.0.0", "type": "craft-plugin", "keywords": [ - "craft", "craftcms", "plugin", "twigperversion" + "craft", + "craftcms", + "plugin", + "twigperversion" ], "license": "MIT", "authors": [ @@ -14,7 +17,7 @@ } ], "require": { - "craftcms/cms": "^3.1.29" + "craftcms/cms": "^4.0" }, "autoload": { "psr-4": { diff --git a/src/twigextensions/Array_Test.php b/src/twigextensions/Array_Test.php index da4d1fe..2292712 100644 --- a/src/twigextensions/Array_Test.php +++ b/src/twigextensions/Array_Test.php @@ -1,9 +1,9 @@ raw('is_array(')->subcompile($this->getNode('node'))->raw(')'); } diff --git a/src/twigextensions/Break_Node.php b/src/twigextensions/Break_Node.php index 0ec7d3b..4225c0c 100644 --- a/src/twigextensions/Break_Node.php +++ b/src/twigextensions/Break_Node.php @@ -11,12 +11,12 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Break_Node extends \Twig_Node +class Break_Node extends \Twig\Node\Node { /** * Compiles a Break_Node into PHP. */ - public function compile(\Twig_Compiler $compiler) + public function compile(\Twig\Compiler $compiler): void { $compiler ->addDebugInfo($this) diff --git a/src/twigextensions/Break_TokenParser.php b/src/twigextensions/Break_TokenParser.php index 3da740e..fa42e77 100644 --- a/src/twigextensions/Break_TokenParser.php +++ b/src/twigextensions/Break_TokenParser.php @@ -13,12 +13,11 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Break_TokenParser extends \Twig_TokenParser +class Break_TokenParser extends \Twig\TokenParser\AbstractTokenParser { - - public function parse(\Twig_Token $token) + public function parse(\Twig\Token $token) { - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + $this->parser->getStream()->expect(\Twig\Token::BLOCK_END_TYPE); return new Break_Node(array(), array(), $token->getLine(), $this->getTag()); } diff --git a/src/twigextensions/Continue_Node.php b/src/twigextensions/Continue_Node.php index 8e606fe..1f4cca9 100644 --- a/src/twigextensions/Continue_Node.php +++ b/src/twigextensions/Continue_Node.php @@ -11,12 +11,12 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Continue_Node extends \Twig_Node +class Continue_Node extends \Twig\Node\Node { /** * Compiles a Continue_Node into PHP. */ - public function compile(\Twig_Compiler $compiler) + public function compile(\Twig\Compiler $compiler) { $compiler ->addDebugInfo($this) diff --git a/src/twigextensions/Continue_TokenParser.php b/src/twigextensions/Continue_TokenParser.php index f9da4e4..aa4dfba 100644 --- a/src/twigextensions/Continue_TokenParser.php +++ b/src/twigextensions/Continue_TokenParser.php @@ -13,12 +13,12 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Continue_TokenParser extends \Twig_TokenParser +class Continue_TokenParser extends \Twig\TokenParser\AbstractTokenParser { - public function parse(\Twig_Token $token) + public function parse(\Twig\Token $token) { - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + $this->parser->getStream()->expect(\Twig\Token::BLOCK_END_TYPE); return new Continue_Node(array(), array(), $token->getLine(), $this->getTag()); } diff --git a/src/twigextensions/Expression_Binary_Equivalent.php b/src/twigextensions/Expression_Binary_Equivalent.php index fbc8c6e..a8ae77a 100644 --- a/src/twigextensions/Expression_Binary_Equivalent.php +++ b/src/twigextensions/Expression_Binary_Equivalent.php @@ -2,9 +2,9 @@ namespace marionnewlevant\twigperversion\twigextensions; -class Expression_Binary_Equivalent extends \Twig_Node_Expression_Binary +class Expression_Binary_Equivalent extends \Twig\Node\Expression\Binary\AbstractBinary { - public function operator(\Twig_Compiler $compiler) + public function operator(\Twig\Compiler $compiler): \Twig\Compiler { return $compiler->raw('==='); } diff --git a/src/twigextensions/Expression_Binary_NotEquivalent.php b/src/twigextensions/Expression_Binary_NotEquivalent.php index a519b1a..b48cd82 100644 --- a/src/twigextensions/Expression_Binary_NotEquivalent.php +++ b/src/twigextensions/Expression_Binary_NotEquivalent.php @@ -2,9 +2,9 @@ namespace marionnewlevant\twigperversion\twigextensions; -class Expression_Binary_NotEquivalent extends \Twig_Node_Expression_Binary +class Expression_Binary_NotEquivalent extends \Twig\Node\Expression\Binary\AbstractBinary { - public function operator(\Twig_Compiler $compiler) + public function operator(\Twig\Compiler $compiler): \Twig\Compiler { return $compiler->raw('!=='); } diff --git a/src/twigextensions/Expression_Binary_Spaceship.php b/src/twigextensions/Expression_Binary_Spaceship.php index 4577c04..e2fb20a 100644 --- a/src/twigextensions/Expression_Binary_Spaceship.php +++ b/src/twigextensions/Expression_Binary_Spaceship.php @@ -2,9 +2,9 @@ namespace marionnewlevant\twigperversion\twigextensions; -class Expression_Binary_Spaceship extends \Twig_Node_Expression_Binary +class Expression_Binary_Spaceship extends \Twig\Node\Expression\Binary\AbstractBinary { - public function operator(\Twig_Compiler $compiler) + public function operator(\Twig\Compiler $compiler): \Twig\Compiler { return $compiler->raw('<=>'); } diff --git a/src/twigextensions/Numeric_Test.php b/src/twigextensions/Numeric_Test.php index 765b930..fd492b1 100644 --- a/src/twigextensions/Numeric_Test.php +++ b/src/twigextensions/Numeric_Test.php @@ -1,9 +1,9 @@ raw('is_numeric(')->subcompile($this->getNode('node'))->raw(')'); } diff --git a/src/twigextensions/Return_Node.php b/src/twigextensions/Return_Node.php index ecc95b4..5475848 100644 --- a/src/twigextensions/Return_Node.php +++ b/src/twigextensions/Return_Node.php @@ -11,13 +11,13 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Return_Node extends \Twig_Node +class Return_Node extends \Twig\Node\Node { /** * Compiles a Return_Node into PHP. */ - public function compile(\Twig_Compiler $compiler) + public function compile(\Twig\Compiler $compiler) { $compiler ->addDebugInfo($this) diff --git a/src/twigextensions/Return_TokenParser.php b/src/twigextensions/Return_TokenParser.php index ab34cdf..2e268d5 100644 --- a/src/twigextensions/Return_TokenParser.php +++ b/src/twigextensions/Return_TokenParser.php @@ -12,20 +12,20 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Return_TokenParser extends \Twig_TokenParser +class Return_TokenParser extends \Twig\TokenParser\AbstractTokenParser { - public function parse(\Twig_Token $token) + public function parse(\Twig\Token $token) { $stream = $this->parser->getStream(); // entire stream of tokens $nodes = array(); - if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) + if (!$stream->test(\Twig\Token::BLOCK_END_TYPE)) { $nodes['expr'] = $this->parser->getExpressionParser()->parseExpression(); } - $stream->expect(\Twig_Token::BLOCK_END_TYPE); + $stream->expect(\Twig\Token::BLOCK_END_TYPE); return new Return_Node($nodes, array(), $token->getLine(), $this->getTag()); } diff --git a/src/twigextensions/String_Test.php b/src/twigextensions/String_Test.php index b495d12..f8134a3 100644 --- a/src/twigextensions/String_Test.php +++ b/src/twigextensions/String_Test.php @@ -1,9 +1,9 @@ raw('is_string(')->subcompile($this->getNode('node'))->raw(')'); } diff --git a/src/twigextensions/TwigPerversionTwigExtension.php b/src/twigextensions/TwigPerversionTwigExtension.php index 9c7a072..d073cb2 100644 --- a/src/twigextensions/TwigPerversionTwigExtension.php +++ b/src/twigextensions/TwigPerversionTwigExtension.php @@ -16,7 +16,7 @@ * @package TwigPerversion * @since 1.0.0 */ -class TwigPerversionTwigExtension extends \Twig_Extension +class TwigPerversionTwigExtension extends \Twig\Extension\AbstractExtension { // Public Methods // ========================================================================= @@ -44,16 +44,16 @@ public function getTokenParsers() public function getTests() { return [ - new \Twig_Test('numeric', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\Numeric_Test']), - new \Twig_Test('string', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\String_Test']), - new \Twig_Test('array', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\Array_Test']), + new \Twig\TwigTest('numeric', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\Numeric_Test']), + new \Twig\TwigTest('string', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\String_Test']), + new \Twig\TwigTest('array', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\Array_Test']), ]; } public function getFilters() { return [ - new \Twig_Filter('array_splice', function(array $input, int $offset, int $length = null, $replacement = null) { + new \Twig\TwigFilter('array_splice', function(array $input, int $offset, int $length = null, $replacement = null) { if (is_null($length)) { $length = count($input); @@ -66,10 +66,10 @@ public function getFilters() return $input; }), - new \Twig_SimpleFilter('string', [$this, 'string']), - new \Twig_SimpleFilter('float', [$this, 'float']), - new \Twig_SimpleFilter('int', [$this, 'int']), - new \Twig_SimpleFilter('bool', [$this, 'bool']), + new \Twig\TwigFilter('string', [$this, 'string']), + new \Twig\TwigFilter('float', [$this, 'float']), + new \Twig\TwigFilter('int', [$this, 'int']), + new \Twig\TwigFilter('bool', [$this, 'bool']), ]; } @@ -81,17 +81,17 @@ public function getOperators() '===' => [ 'precedence' => 20, 'class' => '\marionnewlevant\twigperversion\twigextensions\Expression_Binary_Equivalent', - 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT, + 'associativity' => \Twig\ExpressionParser::OPERATOR_LEFT, ], '!==' => [ 'precedence' => 20, 'class' => '\marionnewlevant\twigperversion\twigextensions\Expression_Binary_NotEquivalent', - 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT, + 'associativity' => \Twig\ExpressionParser::OPERATOR_LEFT, ], '<=>' => [ 'precedence' => 20, 'class' => '\marionnewlevant\twigperversion\twigextensions\Expression_Binary_Spaceship', - 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT, + 'associativity' => \Twig\ExpressionParser::OPERATOR_LEFT, ], ], ]; diff --git a/src/twigextensions/While_TokenParser.php b/src/twigextensions/While_TokenParser.php index 3ff0048..c75bff5 100644 --- a/src/twigextensions/While_TokenParser.php +++ b/src/twigextensions/While_TokenParser.php @@ -20,28 +20,28 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion * */ -class While_TokenParser extends \Twig_TokenParser +class While_TokenParser extends \Twig\TokenParser\AbstractTokenParser { /** * Parses a token and returns a node. * - * @param Twig_Token $token A Twig_Token instance + * @param \Twig\Token $token A \Twig\Token instance * - * @return Twig_NodeInterface A Twig_NodeInterface instance + * @return \Twig\NodeInterface A \Twig\NodeInterface instance */ - public function parse(\Twig_Token $token) + public function parse(\Twig\Token $token) { $lineno = $token->getLine(); /** @var Parser $parser */ $parser = $this->parser; $stream = $parser->getStream(); $condition = $parser->getExpressionParser()->parseExpression(); - $stream->expect(\Twig_Token::BLOCK_END_TYPE); + $stream->expect(\Twig\Token::BLOCK_END_TYPE); $body = $parser->subparse([$this, 'decideWhileEnd']); $stream->next(); - $stream->expect(\Twig_Token::BLOCK_END_TYPE); + $stream->expect(\Twig\Token::BLOCK_END_TYPE); return new While_Node($condition, $body, $lineno, $this->getTag()); } @@ -49,11 +49,11 @@ public function parse(\Twig_Token $token) /** * Block end * - * @param Twig_Token $token + * @param \Twig\Token $token * * @return bool */ - public function decideWhileEnd(\Twig_Token $token) + public function decideWhileEnd(\Twig\Token $token) { return $token->test(['endwhile']); }