Skip to content

Commit

Permalink
Update for Craft 4 (Twig 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
marionnewlevant committed May 20, 2022
1 parent 7c47292 commit 66e9dcf
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 57 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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' }}
Expand All @@ -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 #}
Expand All @@ -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`.
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -14,7 +17,7 @@
}
],
"require": {
"craftcms/cms": "^3.1.29"
"craftcms/cms": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Array_Test.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace marionnewlevant\twigperversion\twigextensions;

class Array_Test extends \Twig_Node_Expression_Test
class Array_Test extends \Twig\Node\Expression\TestExpression
{
public function compile(\Twig_Compiler $compiler)
public function compile(\Twig\Compiler $compiler): void
{
$compiler->raw('is_array(')->subcompile($this->getNode('node'))->raw(')');
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Break_Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions src/twigextensions/Break_TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Continue_Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/twigextensions/Continue_TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Expression_Binary_Equivalent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('===');
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Expression_Binary_NotEquivalent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('!==');
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Expression_Binary_Spaceship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<=>');
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Numeric_Test.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace marionnewlevant\twigperversion\twigextensions;

class Numeric_Test extends \Twig_Node_Expression_Test
class Numeric_Test extends \Twig\Node\Expression\TestExpression
{
public function compile(\Twig_Compiler $compiler)
public function compile(\Twig\Compiler $compiler): void
{
$compiler->raw('is_numeric(')->subcompile($this->getNode('node'))->raw(')');
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/Return_Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/twigextensions/Return_TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/twigextensions/String_Test.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace marionnewlevant\twigperversion\twigextensions;

class String_Test extends \Twig_Node_Expression_Test
class String_Test extends \Twig\Node\Expression\TestExpression
{
public function compile(\Twig_Compiler $compiler)
public function compile(\Twig\Compiler $compiler): void
{
$compiler->raw('is_string(')->subcompile($this->getNode('node'))->raw(')');
}
Expand Down
24 changes: 12 additions & 12 deletions src/twigextensions/TwigPerversionTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @package TwigPerversion
* @since 1.0.0
*/
class TwigPerversionTwigExtension extends \Twig_Extension
class TwigPerversionTwigExtension extends \Twig\Extension\AbstractExtension
{
// Public Methods
// =========================================================================
Expand Down Expand Up @@ -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);
Expand All @@ -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']),
];
}

Expand All @@ -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,
],
],
];
Expand Down
16 changes: 8 additions & 8 deletions src/twigextensions/While_TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,40 @@
* @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());
}

/**
* 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']);
}
Expand Down

0 comments on commit 66e9dcf

Please sign in to comment.