-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Twig as template engine
- Loading branch information
Showing
5 changed files
with
154 additions
and
0 deletions.
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
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,61 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use Throwable; | ||
use Twig_Environment; | ||
|
||
final class Twig implements Formatter | ||
{ | ||
private const DEFAULT_ATTRIBUTE = 'template'; | ||
|
||
/** | ||
* @var Twig_Environment | ||
*/ | ||
private $environment; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $attributeName; | ||
|
||
public function __construct( | ||
Twig_Environment $environment, | ||
string $attributeName = self::DEFAULT_ATTRIBUTE | ||
) { | ||
$this->environment = $environment; | ||
$this->attributeName = $attributeName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content, array $attributes = []): string | ||
{ | ||
try { | ||
return $this->render($content, $attributes); | ||
} catch (Throwable $exception) { | ||
throw new ContentCouldNotBeFormatted( | ||
'An error occurred while formatting using twig', | ||
$exception->getCode(), | ||
$exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $content | ||
* @param mixed[] $attributes | ||
* | ||
* @throws Throwable | ||
*/ | ||
private function render($content, array $attributes = []): string | ||
{ | ||
$template = $attributes[$this->attributeName] ?? ''; | ||
|
||
return $this->environment->render($template, ['content' => $content]); | ||
} | ||
} |
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,8 @@ | ||
<section> | ||
<dl> | ||
<dt>Identifier</dt> | ||
<dd>{{ content.id }}</dd> | ||
<dt>Name</dt> | ||
<dd>{{ content.name }}</dd> | ||
</dl> | ||
</section> |
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,82 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter\Twig; | ||
use Lcobucci\ContentNegotiation\Tests\PersonDto; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig_Environment; | ||
use Twig_Loader_Filesystem; | ||
use function dirname; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\Twig | ||
*/ | ||
final class TwigTest extends TestCase | ||
{ | ||
/** | ||
* @var Twig_Environment | ||
*/ | ||
private $environment; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function configureEngine(): void | ||
{ | ||
$this->environment = new Twig_Environment( | ||
new Twig_Loader_Filesystem('templates/twig', dirname(__DIR__, 2) . '/') | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldReturnContentFormattedByPlates(): void | ||
{ | ||
$formatter = new Twig($this->environment); | ||
$content = $formatter->format(new PersonDto(1, 'Testing'), ['template' => 'person.twig']); | ||
|
||
self::assertContains('<dd>1</dd>', $content); | ||
self::assertContains('<dd>Testing</dd>', $content); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldReadTemplateNameFromCustomAttribute(): void | ||
{ | ||
$formatter = new Twig($this->environment, 'fancy!'); | ||
$content = $formatter->format(new PersonDto(1, 'Testing'), ['fancy!' => 'person.twig']); | ||
|
||
self::assertContains('<dd>1</dd>', $content); | ||
self::assertContains('<dd>Testing</dd>', $content); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldConvertAnyTwigException(): void | ||
{ | ||
$formatter = new Twig($this->environment); | ||
|
||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
$this->expectExceptionMessage('An error occurred while formatting using twig'); | ||
|
||
$formatter->format(new PersonDto(1, 'Testing'), ['template' => 'no-template-at-all']); | ||
} | ||
} |