From 8abba7131ed4c89c1e8fc6dca0d05a4b6d0b2749 Mon Sep 17 00:00:00 2001 From: Chi-teck Date: Wed, 15 Jul 2020 06:08:04 +0000 Subject: [PATCH] Ref #46: Set up Twig environment explicitly --- src/ApplicationFactory.php | 7 +------ src/bootstrap.php | 21 +++++++++++++++++++++ tests/dcg/Helper/RendererTest.php | 4 +--- tests/dcg/TwigEnvironmentTest.php | 3 +-- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ApplicationFactory.php b/src/ApplicationFactory.php index 40c3f3f79..f3160e475 100644 --- a/src/ApplicationFactory.php +++ b/src/ApplicationFactory.php @@ -44,12 +44,7 @@ public static function create() { $helper_set = new HelperSet([ new QuestionHelper(), new Dumper(new Filesystem()), - // We cannot reference the TwigEnvironment class with use statement - // because of a PHP bug. - // @see https://bugs.php.net/bug.php?id=66773 - // @codingStandardsIgnoreStart - new Renderer(new \DrupalCodeGenerator\Twig\TwigEnvironment(new \Twig_Loader_Filesystem())), - // @codingStandardsIgnoreEnd + new Renderer(dcg_get_twig_environment(new \Twig_Loader_Filesystem())), new InputHandler(), new OutputHandler(), ]); diff --git a/src/bootstrap.php b/src/bootstrap.php index 0f03264b9..0cfb078a1 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -6,6 +6,8 @@ */ use DrupalCodeGenerator\ApplicationFactory; +use DrupalCodeGenerator\Twig\Twig1Environment; +use DrupalCodeGenerator\Twig\Twig2Environment; use Twig\Environment; /** @@ -31,6 +33,25 @@ function dcg_create_application() { return ApplicationFactory::create(); } +/** + * Creates an Twig environment. + */ +function dcg_get_twig_environment($loader) { + switch (Environment::MAJOR_VERSION) { + case 1: + $environment = new Twig1Environment($loader); + break; + + case 2: + $environment = new Twig2Environment($loader); + break; + + default: + throw new \RuntimeException('Unsupported Twig version'); + } + return $environment; +} + // Determine major Twig version. // \Twig\Environment::MAJOR_VERSION is not suitable here because of // https://github.com/twigphp/Twig/pull/2945 diff --git a/tests/dcg/Helper/RendererTest.php b/tests/dcg/Helper/RendererTest.php index a6711f046..6ce99740c 100644 --- a/tests/dcg/Helper/RendererTest.php +++ b/tests/dcg/Helper/RendererTest.php @@ -3,7 +3,6 @@ namespace DrupalCodeGenerator\Tests\Helper; use DrupalCodeGenerator\Helper\Renderer; -use DrupalCodeGenerator\Twig\TwigEnvironment; use PHPUnit\Framework\TestCase; /** @@ -15,8 +14,7 @@ class RendererTest extends TestCase { * Test callback. */ public function testRenderer() { - $twig_loader = new \Twig_Loader_Filesystem(); - $twig = new TwigEnvironment($twig_loader); + $twig = dcg_get_twig_environment(new \Twig_Loader_Filesystem()); $renderer = new Renderer($twig); static::assertEquals($renderer->getName(), 'dcg_renderer'); $renderer->addPath(__DIR__); diff --git a/tests/dcg/TwigEnvironmentTest.php b/tests/dcg/TwigEnvironmentTest.php index 80762eb20..570265d05 100644 --- a/tests/dcg/TwigEnvironmentTest.php +++ b/tests/dcg/TwigEnvironmentTest.php @@ -3,7 +3,6 @@ namespace DrupalCodeGenerator\Tests; use PHPUnit\Framework\TestCase; -use DrupalCodeGenerator\Twig\TwigEnvironment; /** * A test for Twig environment. @@ -15,7 +14,7 @@ class TwigEnvironmentTest extends TestCase { */ public function testTwigEnvironment() { $twig_loader = new \Twig_Loader_Filesystem(__DIR__); - $twig = new TwigEnvironment($twig_loader); + $twig = \dcg_get_twig_environment($twig_loader); $expected = file_get_contents(__DIR__ . '/_twig_environment_fixture.txt'); $result = $twig->render('twig-environment-template.twig', []); static::assertEquals($expected, $result);