Skip to content

Commit

Permalink
Ref #46: Set up Twig environment explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-teck committed Jul 15, 2020
1 parent a1686fe commit 8abba71
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
7 changes: 1 addition & 6 deletions src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
Expand Down
21 changes: 21 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

use DrupalCodeGenerator\ApplicationFactory;
use DrupalCodeGenerator\Twig\Twig1Environment;
use DrupalCodeGenerator\Twig\Twig2Environment;
use Twig\Environment;

/**
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions tests/dcg/Helper/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace DrupalCodeGenerator\Tests\Helper;

use DrupalCodeGenerator\Helper\Renderer;
use DrupalCodeGenerator\Twig\TwigEnvironment;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -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__);
Expand Down
3 changes: 1 addition & 2 deletions tests/dcg/TwigEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace DrupalCodeGenerator\Tests;

use PHPUnit\Framework\TestCase;
use DrupalCodeGenerator\Twig\TwigEnvironment;

/**
* A test for Twig environment.
Expand All @@ -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);
Expand Down

0 comments on commit 8abba71

Please sign in to comment.