Skip to content

Commit

Permalink
Merge pull request #2 from Larium/feature/default-twig-env
Browse files Browse the repository at this point in the history
Allow adding default twig env instance
  • Loading branch information
akDeveloper authored Aug 22, 2023
2 parents 578b5aa + 92a8638 commit 3dd7de4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
30 changes: 23 additions & 7 deletions src/TwigTemplate.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Larium\Bridge\Template;

use Larium\Bridge\Template\Cache\Cache;
use Larium\Bridge\Template\Filter\Filter;
use Twig\Cache\CacheInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
Expand All @@ -22,12 +23,9 @@ class TwigTemplate implements Template
*/
private $fileSystem;

public function __construct(string $path)
public function __construct(string $path, ?Environment $twig = null)
{
$this->fileSystem = new FilesystemLoader($path);
$this->engine = new Environment(
$this->fileSystem
);
$twig === null ? $this->setUpNew($path) : $this->setUpExisting($path, $twig);
}

public function render(string $template, array $params = []): string
Expand All @@ -49,11 +47,29 @@ public function disableCache(): void

public function setCache(Cache $cache): void
{
$this->engine->setCache($cache);
/** @var CacheInterface $engineCache */
$engineCache = $cache->getCacheImplementation();
$this->engine->setCache($engineCache);
}

public function addPath(string $path): void
{
$this->fileSystem->addPath($path);
}

private function setUpExisting(string $path, Environment $twig): void
{
$this->engine = $twig;
$loader = $this->engine->getLoader();
if ($loader instanceof FilesystemLoader) {
$loader->addPath($path);
$this->fileSystem = $loader;
}
}

private function setUpNew(string $path): void
{
$this->fileSystem = new FilesystemLoader($path);
$this->engine = new Environment($this->fileSystem);
}
}
33 changes: 23 additions & 10 deletions tests/TwigTemplateTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Larium\Bridge\Template;

Expand Down Expand Up @@ -32,32 +32,45 @@ public function testShouldTwigBridgePath(): void
{
$engine = $this->getEngine();

$this->assertInstanceOf(FilesystemLoader::class, $engine->getLoader());
$paths = $engine->getLoader()->getPaths();
/** @var FilesystemLoader $loader */
$loader = $engine->getLoader();
self::assertInstanceOf(FilesystemLoader::class, $loader);
$paths = $loader->getPaths();
$path = reset($paths);

$this->assertEquals($this->templatePath, $path);
self::assertEquals($this->templatePath, $path);
}

public function testShouldAddPath(): void
{
try {
$this->template->render('test.html.twig', ['testVariable' => 'Hello World!']);
} catch (\Twig\Error\LoaderError $e) {
$this->assertInstanceOf(\Twig\Error\LoaderError::class, $e);
self::assertInstanceOf(\Twig\Error\LoaderError::class, $e);
}

$this->template->addPath(__DIR__ . '/templates/other');
$result = $this->template->render('test.html.twig', ['testVariable' => 'Hello World!']);

$this->assertEquals('<div>Hello World!</div>', $result);
self::assertEquals('<div>Hello World!</div>', $result);
}

public function testTwigBridgeShouldRender(): void
{
$content = $this->template->render('block.html.twig');

$this->assertNotNull($content);
self::assertNotNull($content);
}

public function testTwigBridgeShouldRenderWithPredefinedEngine(): void
{
$env = new Environment(
new FilesystemLoader([$this->templatePath])
);
$template = new TwigTemplate($this->templatePath, $env);
$content = $template->render('block.html.twig');

self::assertNotNull($content);
}

public function testTwigBridgeShouldAddFilters(): void
Expand All @@ -69,10 +82,10 @@ public function testTwigBridgeShouldAddFilters(): void

$twigFilter = $engine->getFilter('uppercase');

$this->assertInstanceOf(TwigFilter::class, $twigFilter);
self::assertInstanceOf(TwigFilter::class, $twigFilter);

$content = $this->template->render('uppercase-block.html.twig');
$this->assertNotNull($content);
self::assertNotNull($content);
$expected = <<<CONTENT
<html>
<head>
Expand All @@ -84,7 +97,7 @@ public function testTwigBridgeShouldAddFilters(): void
</html>
CONTENT;
$this->assertEquals($expected, $content);
self::assertEquals($expected, $content);
}

private function getEngine(): Environment
Expand Down

0 comments on commit 3dd7de4

Please sign in to comment.