Renderer: Added shared data

This commit is contained in:
Igor Scheller 2018-12-03 23:39:50 +01:00
parent bcce2625a8
commit e9f157ec5c
10 changed files with 130 additions and 21 deletions

View File

@ -9,13 +9,13 @@ class MetricsEngine implements EngineInterface
/** /**
* Render metrics * Render metrics
* *
* @example $data = ['foo' => [['labels' => ['foo'=>'bar'], 'value'=>42]], 'bar'=>123]
*
* @param string $path * @param string $path
* @param mixed[] $data * @param mixed[] $data
* @return string * @return string
*
* @example $data = ['foo' => [['labels' => ['foo'=>'bar'], 'value'=>42]], 'bar'=>123]
*/ */
public function get($path, $data = []): string public function get(string $path, array $data = []): string
{ {
$return = []; $return = [];
foreach ($data as $name => $list) { foreach ($data as $name => $list) {
@ -52,7 +52,7 @@ class MetricsEngine implements EngineInterface
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
public function canRender($path): bool public function canRender(string $path): bool
{ {
return $path == '/metrics'; return $path == '/metrics';
} }
@ -60,8 +60,8 @@ class MetricsEngine implements EngineInterface
/** /**
* @param string $name * @param string $name
* @param array|mixed $row * @param array|mixed $row
* @see https://prometheus.io/docs/instrumenting/exposition_formats/
* @return string * @return string
* @see https://prometheus.io/docs/instrumenting/exposition_formats/
*/ */
protected function formatData($name, $row): string protected function formatData($name, $row): string
{ {
@ -135,4 +135,12 @@ class MetricsEngine implements EngineInterface
$value $value
); );
} }
/**
* Does nothing as shared data will onyly result in unexpected behaviour
*
* @param string|mixed[] $key
* @param mixed $value
*/
public function share($key, $value = null) { }
} }

22
src/Renderer/Engine.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Engelsystem\Renderer;
abstract class Engine implements EngineInterface
{
/** @var array */
protected $sharedData = [];
/**
* @param mixed[]|string $key
* @param null $value
*/
public function share($key, $value = null)
{
if (!is_array($key)) {
$key = [$key => $value];
}
$this->sharedData = array_replace_recursive($this->sharedData, $key);
}
}

View File

@ -11,11 +11,17 @@ interface EngineInterface
* @param mixed[] $data * @param mixed[] $data
* @return string * @return string
*/ */
public function get($path, $data = []); public function get(string $path, array $data = []): string;
/** /**
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
public function canRender($path); public function canRender(string $path): bool;
/**
* @param string|mixed[] $key
* @param mixed $value
*/
public function share($key, $value = null);
} }

View File

@ -2,7 +2,7 @@
namespace Engelsystem\Renderer; namespace Engelsystem\Renderer;
class HtmlEngine implements EngineInterface class HtmlEngine extends Engine
{ {
/** /**
* Render a template * Render a template
@ -11,9 +11,11 @@ class HtmlEngine implements EngineInterface
* @param mixed[] $data * @param mixed[] $data
* @return string * @return string
*/ */
public function get($path, $data = []) public function get(string $path, array $data = []): string
{ {
$data = array_replace_recursive($this->sharedData, $data);
$template = file_get_contents($path); $template = file_get_contents($path);
if (is_array($data)) { if (is_array($data)) {
foreach ($data as $name => $content) { foreach ($data as $name => $content) {
$template = str_replace('%' . $name . '%', $content, $template); $template = str_replace('%' . $name . '%', $content, $template);
@ -27,7 +29,7 @@ class HtmlEngine implements EngineInterface
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
public function canRender($path) public function canRender(string $path): bool
{ {
return mb_strpos($path, '.htm') !== false && file_exists($path); return mb_strpos($path, '.htm') !== false && file_exists($path);
} }

View File

@ -7,7 +7,7 @@ use Twig_Error_Loader as LoaderError;
use Twig_Error_Runtime as RuntimeError; use Twig_Error_Runtime as RuntimeError;
use Twig_Error_Syntax as SyntaxError; use Twig_Error_Syntax as SyntaxError;
class TwigEngine implements EngineInterface class TwigEngine extends Engine
{ {
/** @var Twig */ /** @var Twig */
protected $twig; protected $twig;
@ -25,8 +25,10 @@ class TwigEngine implements EngineInterface
* @return string * @return string
* @throws LoaderError|RuntimeError|SyntaxError * @throws LoaderError|RuntimeError|SyntaxError
*/ */
public function get($path, $data = []) public function get(string $path, array $data = []): string
{ {
$data = array_replace_recursive($this->sharedData, $data);
return $this->twig->render($path, $data); return $this->twig->render($path, $data);
} }
@ -34,7 +36,7 @@ class TwigEngine implements EngineInterface
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
public function canRender($path) public function canRender(string $path): bool
{ {
return $this->twig->getLoader()->exists($path); return $this->twig->getLoader()->exists($path);
} }

View File

@ -66,4 +66,15 @@ class MetricsEngineTest extends TestCase
$this->assertFalse($engine->canRender('/metrics.foo')); $this->assertFalse($engine->canRender('/metrics.foo'));
$this->assertTrue($engine->canRender('/metrics')); $this->assertTrue($engine->canRender('/metrics'));
} }
/**
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::share
*/
public function testShare()
{
$engine = new MetricsEngine();
$engine->share('foo', 42);
$this->assertEquals('', $engine->get('/metrics'));
}
} }

View File

@ -0,0 +1,25 @@
<?php
namespace Engelsystem\Test\Unit\Renderer;
use Engelsystem\Test\Unit\Renderer\Stub\EngineImplementation;
use PHPUnit\Framework\TestCase;
class EngineTest extends TestCase
{
/**
* @covers \Engelsystem\Renderer\Engine::share
*/
public function testShare()
{
$engine = new EngineImplementation();
$engine->share(['foo' => ['bar' => 'baz', 'lorem' => 'ipsum']]);
$engine->share(['foo' => ['lorem' => 'dolor']]);
$engine->share('key', 'value');
$this->assertEquals(
['foo' => ['bar' => 'baz', 'lorem' => 'dolor'], 'key' => 'value'],
$engine->getSharedData()
);
}
}

View File

@ -16,11 +16,12 @@ class HtmlEngineTest extends TestCase
public function testGet() public function testGet()
{ {
$engine = new HtmlEngine(); $engine = new HtmlEngine();
$engine->share('shared_data', 'tester');
$file = $this->createTempFile('<div>%main_content%</div>'); $file = $this->createTempFile('<div>%main_content% is a %shared_data%</div>');
$data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']); $data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']);
$this->assertEquals('<div>Lorem ipsum dolor sit</div>', $data); $this->assertEquals('<div>Lorem ipsum dolor sit is a tester</div>', $data);
} }
/** /**

View File

@ -0,0 +1,32 @@
<?php
namespace Engelsystem\Test\Unit\Renderer\Stub;
use Engelsystem\Renderer\Engine;
class EngineImplementation extends Engine
{
/**
* @inheritdoc
*/
public function get(string $path, array $data = []): string
{
return '';
}
/**
* @inheritdoc
*/
public function canRender(string $path): bool
{
return true;
}
/**
* @return array
*/
public function getSharedData(): array
{
return $this->sharedData;
}
}

View File

@ -20,16 +20,16 @@ class TwigEngineTest extends TestCase
$twig = $this->createMock(Twig::class); $twig = $this->createMock(Twig::class);
$path = 'foo.twig'; $path = 'foo.twig';
$data = ['lorem' => 'ipsum'];
$twig->expects($this->once()) $twig->expects($this->once())
->method('render') ->method('render')
->with($path, $data) ->with($path, ['lorem' => 'ipsum', 'shared' => 'data'])
->willReturn('LoremIpsum!'); ->willReturn('LoremIpsum data!');
$engine = new TwigEngine($twig); $engine = new TwigEngine($twig);
$return = $engine->get($path, $data); $engine->share('shared', 'data');
$this->assertEquals('LoremIpsum!', $return);
$return = $engine->get($path, ['lorem' => 'ipsum']);
$this->assertEquals('LoremIpsum data!', $return);
} }