Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
twig 2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
MatTheCat committed Jan 6, 2017
1 parent 31da4bf commit 991befa
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"twig/twig": "~1.12"
"twig/twig": "~2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2"
Expand Down
5 changes: 0 additions & 5 deletions lib/Twig/Extension/WhitespaceCollapser.php
Expand Up @@ -33,9 +33,4 @@ public function getDefault()
{
return $this->default;
}

public function getName()
{
return 'whitespace_collapser';
}
}
2 changes: 1 addition & 1 deletion lib/Twig/Node/WhitespaceCollapse.php
Expand Up @@ -4,7 +4,7 @@

class WhitespaceCollapse extends \Twig_Node
{
public function __construct($value, \Twig_NodeInterface $body, $lineno, $tag = 'whitespacecollapse')
public function __construct($value, \Twig_Node $body, $lineno, $tag = 'whitespacecollapse')
{
parent::__construct(array('body' => $body), array('value' => $value), $lineno, $tag);
}
Expand Down
13 changes: 7 additions & 6 deletions lib/Twig/NodeVisitor/WhitespaceCollapser.php
Expand Up @@ -2,7 +2,8 @@

namespace MatTheCat\Twig\NodeVisitor;

use MatTheCat\Twig\Node\WhitespaceCollapse;
use MatTheCat\Twig\Extension\WhitespaceCollapser as WhitespaceCollapseExtension;
use MatTheCat\Twig\Node\WhitespaceCollapse as WhitespaceCollapseNode;

class WhitespaceCollapser extends \Twig_BaseNodeVisitor
{
Expand All @@ -17,16 +18,16 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
$this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ?
$this->blocks[$node->getAttribute('name')] :
$this->needCollapsing();
} elseif ($node instanceof WhitespaceCollapse) {
} elseif ($node instanceof WhitespaceCollapseNode) {
$this->statusStack[] = $node->getAttribute('value');
} elseif ($env->hasExtension('whitespace_collapser')) {
} elseif ($env->hasExtension(WhitespaceCollapseExtension::class)) {
/** @var \MatTheCat\Twig\Extension\WhitespaceCollapser $extension */
$extension = $env->getExtension('whitespace_collapser');
$extension = $env->getExtension(WhitespaceCollapseExtension::class);
$extensionDefault = $extension->getDefault();

if ($node instanceof \Twig_Node_Module) {
if (is_array($extensionDefault)) {
$filename = $node->getAttribute('filename');
$filename = $node->getTemplateName();
if (substr($filename, -5) === '.twig') {
$filename = substr($filename, 0, -5);
}
Expand All @@ -46,7 +47,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)

protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
{
if ($node instanceof WhitespaceCollapse || $node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_AutoEscape) {
if ($node instanceof WhitespaceCollapseNode || $node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_AutoEscape) {
array_pop($this->statusStack);
} elseif ($node instanceof \Twig_Node_BlockReference) {
$this->blocks[$node->getAttribute('name')] = $this->needCollapsing();
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/TokenParser/WhitespaceCollapse.php
Expand Up @@ -16,7 +16,7 @@ public function parse(\Twig_Token $token)
} else {
$expr = $this->parser->getExpressionParser()->parseExpression();
if (!$expr instanceof \Twig_Node_Expression_Constant) {
throw new \Twig_Error_Syntax('State must be a Boolean.', $stream->getCurrent()->getLine(), $stream->getFilename());
throw new \Twig_Error_Syntax('State must be a Boolean.', $stream->getCurrent()->getLine(), $stream->getSourceContext()->getPath());
}
$value = $expr->getAttribute('value');
}
Expand Down

0 comments on commit 991befa

Please sign in to comment.