Skip to content

Commit

Permalink
Allow Twig v3 (#40)
Browse files Browse the repository at this point in the history
* bump lower Twig version constraint to support namespaced classes

* update Twig classes to namespaced ones

* remove conflict from composer and allow twig3

* update to PHPUnit 5

* fix twig environment dependency in GoogleTagManagerListener

* adjust lowest supported PHP version to 5.6
  • Loading branch information
jkabat authored Mar 10, 2021
1 parent e1214d8 commit 2b2b35b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
Expand Down
7 changes: 4 additions & 3 deletions EventListener/GoogleTagManagerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Xynnn\GoogleTagManagerBundle\EventListener;

use Twig\Environment;
use Xynnn\GoogleTagManagerBundle\Twig\GoogleTagManagerExtension;

/**
Expand All @@ -12,7 +13,7 @@
class GoogleTagManagerListener
{
/**
* @var \Twig_Environment
* @var Environment
*/
private $environment;

Expand All @@ -27,11 +28,11 @@ class GoogleTagManagerListener
private $autoAppend;

/**
* @param \Twig_Environment $environment
* @param Environment $environment
* @param GoogleTagManagerExtension $extension
* @param bool $autoAppend
*/
public function __construct(\Twig_Environment $environment, GoogleTagManagerExtension $extension, $autoAppend)
public function __construct(Environment $environment, GoogleTagManagerExtension $extension, $autoAppend)
{
$this->environment = $environment;
$this->extension = $extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ protected function setUp()
{
$this->extension = new GoogleTagManagerExtension();

$twigMock = $this->getMockBuilder('\Twig\Environment')
->disableOriginalConstructor()
->getMock();

$this->container = new ContainerBuilder();
$this->container->register('twig', $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock());
$this->container->register('twig', get_class($twigMock));
$this->container->registerExtension($this->extension);
}

Expand All @@ -45,7 +49,7 @@ abstract protected function loadConfiguration(ContainerBuilder $container, $reso
public function testWithoutConfiguration()
{
// An extension is only loaded in the container if a configuration is provided for it.
// Then, we need to explicitely load it.
// Then, we need to explicitly load it.
$this->container->loadFromExtension($this->extension->getAlias());
$this->container->compile();

Expand Down
34 changes: 18 additions & 16 deletions Twig/GoogleTagManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

namespace Xynnn\GoogleTagManagerBundle\Twig;

use Twig_Extension;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Xynnn\GoogleTagManagerBundle\Helper\GoogleTagManagerHelperInterface;

/**
* Class GoogleTagManagerExtension
*
* @package Xynnn\GoogleTagManagerBundle\Extension
*/
class GoogleTagManagerExtension extends Twig_Extension
class GoogleTagManagerExtension extends AbstractExtension
{
const AREA_FULL = 'full';
const AREA_HEAD = 'head';
Expand All @@ -44,64 +46,64 @@ public function __construct(GoogleTagManagerHelperInterface $helper)
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('google_tag_manager', array($this, 'render'), array(
new TwigFunction('google_tag_manager', array($this, 'render'), array(
'is_safe' => array('html'),
'needs_environment' => true,
'deprecated' => true,
)),
new \Twig_SimpleFunction('google_tag_manager_body', array($this, 'renderBody'), array(
new TwigFunction('google_tag_manager_body', array($this, 'renderBody'), array(
'is_safe' => array('html'),
'needs_environment' => true,
)),
new \Twig_SimpleFunction('google_tag_manager_head', array($this, 'renderHead'), array(
new TwigFunction('google_tag_manager_head', array($this, 'renderHead'), array(
'is_safe' => array('html'),
'needs_environment' => true,
)),
new \Twig_SimpleFunction('google_tag_manager_body_end', array($this, 'renderBodyEnd'), array(
new TwigFunction('google_tag_manager_body_end', array($this, 'renderBodyEnd'), array(
'is_safe' => array('html'),
'needs_environment' => true,
)),
);
}

/**
* @param \Twig_Environment $twig
* @param Environment $twig
*
* @deprecated Use `renderHead` and `renderBody`
*
* @return string
*/
public function render(\Twig_Environment $twig)
public function render(Environment $twig)
{
return $this->getRenderedTemplate($twig, self::AREA_FULL);
}

/**
* @param \Twig_Environment $twig
* @param Environment $twig
*
* @return string
*/
public function renderHead(\Twig_Environment $twig)
public function renderHead(Environment $twig)
{
return $this->getRenderedTemplate($twig, self::AREA_HEAD);
}

/**
* @param \Twig_Environment $twig
* @param Environment $twig
*
* @return string
*/
public function renderBody(\Twig_Environment $twig)
public function renderBody(Environment $twig)
{
return $this->getRenderedTemplate($twig, self::AREA_BODY);
}

/**
* @param \Twig_Environment $twig
* @param Environment $twig
*
* @return string
*/
public function renderBodyEnd(\Twig_Environment $twig)
public function renderBodyEnd(Environment $twig)
{
return $this->getRenderedTemplate($twig, self::AREA_BODY_END);
}
Expand Down Expand Up @@ -134,11 +136,11 @@ private function getTemplate($area)
}

/**
* @param \Twig_Environment $twig
* @param Environment $twig
* @param $area
* @return string
*/
private function getRenderedTemplate(\Twig_Environment $twig, $area)
private function getRenderedTemplate(Environment $twig, $area)
{
if (!$this->helper->isEnabled()) {
return '';
Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4",
"php": ">=5.6",
"symfony/dependency-injection": "~2.8|~3.0|~4.0|~5.0",
"symfony/http-kernel": "~2.8|~3.0|~4.0|~5.0",
"symfony/config": "~2.8|~3.0|~4.0|~5.0",
"symfony/templating": "~2.8|~3.0|~4.0|~5.0",
"twig/twig": "~1.23|~2.0"
},
"conflict": {
"twig/twig": ">=3.0"
"twig/twig": "~1.34|~2.0|~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.3"
"phpunit/phpunit": "~5"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 2b2b35b

Please sign in to comment.