-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Brent Roose <[email protected]>
- Loading branch information
1 parent
09ced7a
commit 0f47a80
Showing
7 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\View\Renderers; | ||
|
||
final readonly class TwigConfig | ||
{ | ||
/** | ||
* @see \Twig\Environment::__construct() | ||
*/ | ||
public function __construct( | ||
public array $viewPaths = [], | ||
public ?string $cachePath = null, | ||
public bool $debug = false, | ||
public string $charset = 'utf-8', | ||
public bool $strictVariables = false, | ||
public string $autoescape = 'html', | ||
public ?bool $autoReload = null, | ||
public int $optimizations = -1, | ||
) { | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'debug' => $this->debug, | ||
'charset' => $this->charset, | ||
'strict_variables' => $this->strictVariables, | ||
'autoescape' => $this->autoescape, | ||
'cache' => $this->cachePath ?: false, | ||
'auto_reload' => $this->autoReload, | ||
'optimizations' => $this->optimizations, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\View\Renderers; | ||
|
||
use Tempest\Container\Container; | ||
use Tempest\Container\DynamicInitializer; | ||
use Tempest\Container\Singleton; | ||
use Tempest\Reflection\ClassReflector; | ||
use Twig\Environment; | ||
use Twig\Loader\FilesystemLoader; | ||
|
||
final readonly class TwigInitializer implements DynamicInitializer | ||
{ | ||
public function canInitialize(ClassReflector $class): bool | ||
{ | ||
if (! class_exists(Environment::class)) { | ||
return false; | ||
} | ||
|
||
return $class->getName() === Environment::class; | ||
} | ||
|
||
#[Singleton] | ||
public function initialize(ClassReflector $class, Container $container): object | ||
{ | ||
$twigConfig = $container->get(TwigConfig::class); | ||
$twigLoader = new FilesystemLoader($twigConfig->viewPaths); | ||
|
||
return new Environment($twigLoader, $twigConfig->toArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\View\Renderers; | ||
|
||
use Tempest\View\View; | ||
use Tempest\View\ViewRenderer; | ||
use Twig\Environment; | ||
|
||
final readonly class TwigViewRenderer implements ViewRenderer | ||
{ | ||
public function __construct(private Environment $twig) | ||
{ | ||
} | ||
|
||
public function render(View|string|null $view): string | ||
{ | ||
if ($view === null) { | ||
return ''; | ||
} | ||
|
||
return trim($this->twig->render($view->getPath(), $view->getData())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Integration\View; | ||
|
||
use Tempest\View\Renderers\TwigConfig; | ||
use Tempest\View\Renderers\TwigViewRenderer; | ||
use Tempest\View\ViewConfig; | ||
use Tempest\View\ViewRenderer; | ||
use Tests\Tempest\Integration\FrameworkIntegrationTestCase; | ||
use function Tempest\view; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TwigViewRendererTest extends FrameworkIntegrationTestCase | ||
{ | ||
public function test_twig(): void | ||
{ | ||
$viewConfig = $this->container->get(ViewConfig::class); | ||
|
||
$viewConfig->rendererClass = TwigViewRenderer::class; | ||
|
||
$this->container->config(new TwigConfig( | ||
viewPaths: [__DIR__ . '/twig'], | ||
cachePath: __DIR__ . '/../../../.cache/tempest/twig/cache', | ||
)); | ||
|
||
$renderer = $this->container->get(ViewRenderer::class); | ||
|
||
$html = $renderer->render(view('index.twig', ...['foo' => 'bar'])); | ||
|
||
$this->assertStringEqualsStringIgnoringLineEndings(<<<HTML | ||
<html> | ||
<span>bar</span> | ||
</html> | ||
HTML | ||
, $html); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<html> | ||
{% block content %}{% endblock %} | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% extends "base.twig" %} | ||
|
||
{% block content %} | ||
<span>{{ foo }}</span> | ||
{% endblock %} |