Skip to content

Commit

Permalink
Merge pull request #7 from nerds-and-company/feature/upgrading-to-twig-3
Browse files Browse the repository at this point in the history
Added Twig 3 support
  • Loading branch information
sjoulbak authored Jun 11, 2020
2 parents 758489a + c8a8b98 commit 2e2f3ab
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
language: php
php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- '7.1'
- hhvm
- nightly
- '7.2'
- '7.3'
- '7.4'
install:
- composer install
script: ./vendor/bin/phpunit
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Add the extension to the `Twig_Environment`:

```php
use NerdsAndCompany\CssToInlineStyles\Twig\InlineCssExtension;
use Twig\Environment;

$twig = new Twig_Environment(...);
$twig = new Environment(...);

$twig->addExtension(new InlineCssExtension());
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
}
],
"require": {
"php": ">=5.3.3",
"twig/twig": ">=1.0",
"php": ">=7.2.5",
"twig/twig": ">=3.0",
"tijsverkoyen/css-to-inline-styles": "^2.0"
},
"require-dev": {
Expand Down
7 changes: 4 additions & 3 deletions src/InlineCssExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace NerdsAndCompany\CssToInlineStyles\Twig;

use Twig_Extension;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

/**
Expand All @@ -14,7 +15,7 @@
*
* @see http://www.nerds.company
*/
class InlineCssExtension extends Twig_Extension implements \Twig_Extension_GlobalsInterface
class InlineCssExtension extends AbstractExtension implements GlobalsInterface
{
/**
* {@inheritdoc}
Expand All @@ -27,7 +28,7 @@ public function getTokenParsers()
/**
* {@inheritdoc}
*/
public function getGlobals()
public function getGlobals(): array
{
return ['inlinecss' => new CssToInlineStyles()];
}
Expand Down
8 changes: 4 additions & 4 deletions src/InlineCssNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace NerdsAndCompany\CssToInlineStyles\Twig;

use Twig_Compiler;
use Twig_Node;
use Twig\Compiler;
use Twig\Node\Node;

/**
* Inline css node adds coversion of css to inline style for given node.
Expand All @@ -14,12 +14,12 @@
*
* @see http://www.nerds.company
*/
class InlineCssNode extends Twig_Node
class InlineCssNode extends Node
{
/**
* {@inheritdoc}
*/
public function compile(Twig_Compiler $compiler)
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this)
->write('$cssPath = ')
Expand Down
16 changes: 8 additions & 8 deletions src/InlineCssParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace NerdsAndCompany\CssToInlineStyles\Twig;

use Twig_TokenParser;
use Twig_Token;
use Twig\TokenParser\AbstractTokenParser;
use Twig\Token;

/**
* Parses twig node between inlinecss and endinlinecss.
Expand All @@ -14,20 +14,20 @@
*
* @see http://www.nerds.company
*/
class InlineCssParser extends Twig_TokenParser
class InlineCssParser extends AbstractTokenParser
{
/**
* {@inheritdoc}
*/
public function parse(Twig_Token $token)
public function parse(Token $token)
{
$lineNo = $token->getLine();
$stream = $this->parser->getStream();
$path = $this->parser->getExpressionParser()->parseMultitargetExpression();

$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideEnd'), true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(Token::BLOCK_END_TYPE);

return new InlineCssNode(['body' => $body], ['css' => $path], $lineNo, $this->getTag());
}
Expand All @@ -43,9 +43,9 @@ public function getTag()
/**
* Node ends when endinlinecss tag has been reached.
*
* @param Twig_Token $token
* @param Token $token
*/
public function decideEnd(Twig_Token $token)
public function decideEnd(Token $token)
{
return $token->test('endinlinecss');
}
Expand Down
4 changes: 3 additions & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace NerdsAndCompany\CssToInlineStyles\Twig;

use Twig\Test\IntegrationTestCase;

/**
* Inline Css extension integration test.
*
Expand All @@ -11,7 +13,7 @@
*
* @see http://www.nerds.company
*/
class InlineCssIntegrationTest extends \Twig_Test_IntegrationTestCase
class InlineCssIntegrationTest extends IntegrationTestCase
{
/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h1 class="h1">Hello World</h1>
{% endinlinecss %}
--DATA--
return array('cssPath' => __DIR__.'/../../../../../../tests/fixtures/test.css')
return array('cssPath' => __DIR__.'/../../../../../tests/fixtures/test.css')
--EXPECT--
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><h1 class="h1" style="color: 'red'; background-color: 'blue';">Hello World</h1></body></html>
2 changes: 1 addition & 1 deletion tests/fixtures/filenotfound.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
--DATA--
return array('cssPath' => 'notfound.css')
--EXPECT--
Twig_Error: Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Given file could not be found: notfound.css") in "index.twig" at line 2 in "filenotfound.test".
Twig\Error\Error: Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("Given file could not be found: notfound.css") in "index.twig" at line 2.

0 comments on commit 2e2f3ab

Please sign in to comment.