Skip to content

Commit 8abba71

Browse files
committed
Ref #46: Set up Twig environment explicitly
1 parent a1686fe commit 8abba71

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/ApplicationFactory.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ public static function create() {
4444
$helper_set = new HelperSet([
4545
new QuestionHelper(),
4646
new Dumper(new Filesystem()),
47-
// We cannot reference the TwigEnvironment class with use statement
48-
// because of a PHP bug.
49-
// @see https://bugs.php.net/bug.php?id=66773
50-
// @codingStandardsIgnoreStart
51-
new Renderer(new \DrupalCodeGenerator\Twig\TwigEnvironment(new \Twig_Loader_Filesystem())),
52-
// @codingStandardsIgnoreEnd
47+
new Renderer(dcg_get_twig_environment(new \Twig_Loader_Filesystem())),
5348
new InputHandler(),
5449
new OutputHandler(),
5550
]);

src/bootstrap.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77

88
use DrupalCodeGenerator\ApplicationFactory;
9+
use DrupalCodeGenerator\Twig\Twig1Environment;
10+
use DrupalCodeGenerator\Twig\Twig2Environment;
911
use Twig\Environment;
1012

1113
/**
@@ -31,6 +33,25 @@ function dcg_create_application() {
3133
return ApplicationFactory::create();
3234
}
3335

36+
/**
37+
* Creates an Twig environment.
38+
*/
39+
function dcg_get_twig_environment($loader) {
40+
switch (Environment::MAJOR_VERSION) {
41+
case 1:
42+
$environment = new Twig1Environment($loader);
43+
break;
44+
45+
case 2:
46+
$environment = new Twig2Environment($loader);
47+
break;
48+
49+
default:
50+
throw new \RuntimeException('Unsupported Twig version');
51+
}
52+
return $environment;
53+
}
54+
3455
// Determine major Twig version.
3556
// \Twig\Environment::MAJOR_VERSION is not suitable here because of
3657
// https://github.com/twigphp/Twig/pull/2945

tests/dcg/Helper/RendererTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace DrupalCodeGenerator\Tests\Helper;
44

55
use DrupalCodeGenerator\Helper\Renderer;
6-
use DrupalCodeGenerator\Twig\TwigEnvironment;
76
use PHPUnit\Framework\TestCase;
87

98
/**
@@ -15,8 +14,7 @@ class RendererTest extends TestCase {
1514
* Test callback.
1615
*/
1716
public function testRenderer() {
18-
$twig_loader = new \Twig_Loader_Filesystem();
19-
$twig = new TwigEnvironment($twig_loader);
17+
$twig = dcg_get_twig_environment(new \Twig_Loader_Filesystem());
2018
$renderer = new Renderer($twig);
2119
static::assertEquals($renderer->getName(), 'dcg_renderer');
2220
$renderer->addPath(__DIR__);

tests/dcg/TwigEnvironmentTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace DrupalCodeGenerator\Tests;
44

55
use PHPUnit\Framework\TestCase;
6-
use DrupalCodeGenerator\Twig\TwigEnvironment;
76

87
/**
98
* A test for Twig environment.
@@ -15,7 +14,7 @@ class TwigEnvironmentTest extends TestCase {
1514
*/
1615
public function testTwigEnvironment() {
1716
$twig_loader = new \Twig_Loader_Filesystem(__DIR__);
18-
$twig = new TwigEnvironment($twig_loader);
17+
$twig = \dcg_get_twig_environment($twig_loader);
1918
$expected = file_get_contents(__DIR__ . '/_twig_environment_fixture.txt');
2019
$result = $twig->render('twig-environment-template.twig', []);
2120
static::assertEquals($expected, $result);

0 commit comments

Comments
 (0)