Renderer: Added shared data

main
Igor Scheller 6 years ago
parent bcce2625a8
commit e9f157ec5c

@ -9,13 +9,13 @@ class MetricsEngine implements EngineInterface
/**
* Render metrics
*
* @example $data = ['foo' => [['labels' => ['foo'=>'bar'], 'value'=>42]], 'bar'=>123]
*
* @param string $path
* @param mixed[] $data
* @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 = [];
foreach ($data as $name => $list) {
@ -52,7 +52,7 @@ class MetricsEngine implements EngineInterface
* @param string $path
* @return bool
*/
public function canRender($path): bool
public function canRender(string $path): bool
{
return $path == '/metrics';
}
@ -60,8 +60,8 @@ class MetricsEngine implements EngineInterface
/**
* @param string $name
* @param array|mixed $row
* @see https://prometheus.io/docs/instrumenting/exposition_formats/
* @return string
* @see https://prometheus.io/docs/instrumenting/exposition_formats/
*/
protected function formatData($name, $row): string
{
@ -135,4 +135,12 @@ class MetricsEngine implements EngineInterface
$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) { }
}

@ -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);
}
}

@ -11,11 +11,17 @@ interface EngineInterface
* @param mixed[] $data
* @return string
*/
public function get($path, $data = []);
public function get(string $path, array $data = []): string;
/**
* @param string $path
* @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);
}

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

@ -7,7 +7,7 @@ use Twig_Error_Loader as LoaderError;
use Twig_Error_Runtime as RuntimeError;
use Twig_Error_Syntax as SyntaxError;
class TwigEngine implements EngineInterface
class TwigEngine extends Engine
{
/** @var Twig */
protected $twig;
@ -25,8 +25,10 @@ class TwigEngine implements EngineInterface
* @return string
* @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);
}
@ -34,7 +36,7 @@ class TwigEngine implements EngineInterface
* @param string $path
* @return bool
*/
public function canRender($path)
public function canRender(string $path): bool
{
return $this->twig->getLoader()->exists($path);
}

@ -66,4 +66,15 @@ class MetricsEngineTest extends TestCase
$this->assertFalse($engine->canRender('/metrics.foo'));
$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'));
}
}

@ -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()
);
}
}

@ -16,11 +16,12 @@ class HtmlEngineTest extends TestCase
public function testGet()
{
$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']);
$this->assertEquals('<div>Lorem ipsum dolor sit</div>', $data);
$this->assertEquals('<div>Lorem ipsum dolor sit is a tester</div>', $data);
}
/**

@ -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;
}
}

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

Loading…
Cancel
Save