From 0b24d5ae6ac43137081f2c8d0b3226781703e5c2 Mon Sep 17 00:00:00 2001 From: slepic Date: Fri, 11 Dec 2020 15:24:25 +0100 Subject: [PATCH] add class DefaultDataTemplate (#2) --- .gitignore | 1 + README.md | 21 ++++++++++-- composer.json | 2 +- src/DefaultDataTemplate.php | 35 +++++++++++++++++++ src/OutputBufferTemplate.php | 9 ++++- src/TemplateInterface.php | 2 +- tests/DefaultDataTemplateTest.php | 57 +++++++++++++++++++++++++++++++ 7 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 src/DefaultDataTemplate.php create mode 100644 tests/DefaultDataTemplateTest.php diff --git a/.gitignore b/.gitignore index 0a3bec9..a513f78 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea .php_cs.cache composer.lock +.phpunit.result.cache diff --git a/README.md b/README.md index b6d4d84..fcd582b 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,6 @@ # php-template Template rendering abstraction library. Abstract your libraries from specific templating engines. -This library provides the [```TemplateInterface```](https://github.com/slepic/php-template/blob/master/src/TemplateInterface.php), which is the abstraction of any data rendering template. -And it also provides one simple implementation called [```OutputBufferTemplate```](https://github.com/slepic/php-template/blob/master/src/OutputBufferTemplate.php), which renders the data using another PHP script and PHP ob_* functions. - ## Requirements PHP 7.4 or 8 @@ -17,6 +14,19 @@ Install with composer ```composer require slepic/php-template``` +## Interfaces + +### [```TemplateInterface```](https://github.com/slepic/php-template/blob/master/src/TemplateInterface.php) +This is the abstraction of any data rendering template. + +## Classes + +### [```OutputBufferTemplate```](https://github.com/slepic/php-template/blob/master/src/OutputBufferTemplate.php) +A simple template implementation, which renders the data using another PHP script (given its filename) and PHP ob_* functions. + +### [```DefaultDataTemplate```](https://github.com/slepic/php-template/blob/master/src/DefaultDataTemplate.php) +Template decorator which allows to feed your templates with default data hidden from the template consumer. + ## Contribution If you create a library that depends on this one and you use composer, please consider the following: @@ -25,6 +35,11 @@ If you create a library that depends on this one and you use composer, please co ## Changelog +### 1.1.0 +* added new class `DefaultDataTemplate` +* `OutputBufferTemplate` now ends the output buffer if the included template throws an exception. +* `OutputBufferTemplate` now uses `include` instead of `require` to execute the template script . + ### 1.0.0 * bump PHP to ^7.4 || ^8.0 diff --git a/composer.json b/composer.json index 749554a..06593a8 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Simple PHP abstraction of template rendering.", "type": "library", "require": { - "php": "^7.4 || ^8.0" + "php": ">=7.4" }, "autoload": { "psr-4": { diff --git a/src/DefaultDataTemplate.php b/src/DefaultDataTemplate.php new file mode 100644 index 0000000..7491b6f --- /dev/null +++ b/src/DefaultDataTemplate.php @@ -0,0 +1,35 @@ + $data + */ + public function __construct(TemplateInterface $template, array $data) + { + $this->template = $template; + $this->data = $data; + } + + public function render(array $data): string + { + $allData = \array_merge($this->data, $data); + return $this->template->render($allData); + } +} diff --git a/src/OutputBufferTemplate.php b/src/OutputBufferTemplate.php index 61e6957..e9f829f 100644 --- a/src/OutputBufferTemplate.php +++ b/src/OutputBufferTemplate.php @@ -23,8 +23,15 @@ public function render(array $data): string 'Expected associative array where keys are valid variable names.' ); } + \ob_start(); - require($this->templateFile); + try { + include($this->templateFile); + } catch (\Throwable $e) { + \ob_end_clean(); + throw $e; + } + return \ob_get_clean(); } } diff --git a/src/TemplateInterface.php b/src/TemplateInterface.php index 3fc73c7..5b93fb6 100644 --- a/src/TemplateInterface.php +++ b/src/TemplateInterface.php @@ -12,7 +12,7 @@ interface TemplateInterface /** * Renders the template with given data. * - * @param array $data + * @param array $data * @return string */ public function render(array $data): string; diff --git a/tests/DefaultDataTemplateTest.php b/tests/DefaultDataTemplateTest.php new file mode 100644 index 0000000..22e71e1 --- /dev/null +++ b/tests/DefaultDataTemplateTest.php @@ -0,0 +1,57 @@ +assertInstanceOf(TemplateInterface::class, $template); + } + + /** + * @param array $defaultData + * @param array $data + * @param array $allData + * + * @dataProvider provideRenderData + */ + public function testRender(array $defaultData, array $data, array $allData): void + { + $hash = \md5((string) \time()); + $innerTemplate = self::createMock(TemplateInterface::class); + $innerTemplate->expects(self::once()) + ->method('render') + ->with($allData) + ->willReturn($hash); + $template = new DefaultDataTemplate($innerTemplate, $defaultData); + $output = $template->render($data); + self::assertSame($hash, $output); + } + + public function provideRenderData(): array + { + $hash = \md5((string) \time()); + return [ + [[], [], []], + [ + ['default' => 'value'], + ['test' => $hash], + ['test' => $hash, 'default' => 'value'] + ], + [ + ['override' => 'default'], + ['keep' => $hash, 'override' => 'overridden'], + ['keep' => $hash, 'override' => 'overridden'] + ], + ]; + } +}