summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Controllers/Metrics/MetricsEngine.php18
-rw-r--r--src/Renderer/Engine.php22
-rw-r--r--src/Renderer/EngineInterface.php10
-rw-r--r--src/Renderer/HtmlEngine.php8
-rw-r--r--src/Renderer/TwigEngine.php8
-rw-r--r--tests/Unit/Controllers/Metrics/MetricsEngineTest.php11
-rw-r--r--tests/Unit/Renderer/EngineTest.php25
-rw-r--r--tests/Unit/Renderer/HtmlEngineTest.php5
-rw-r--r--tests/Unit/Renderer/Stub/EngineImplementation.php32
-rw-r--r--tests/Unit/Renderer/TwigEngineTest.php12
10 files changed, 130 insertions, 21 deletions
diff --git a/src/Controllers/Metrics/MetricsEngine.php b/src/Controllers/Metrics/MetricsEngine.php
index 1e0f6957..21ae8fd0 100644
--- a/src/Controllers/Metrics/MetricsEngine.php
+++ b/src/Controllers/Metrics/MetricsEngine.php
@@ -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) { }
}
diff --git a/src/Renderer/Engine.php b/src/Renderer/Engine.php
new file mode 100644
index 00000000..60f1d686
--- /dev/null
+++ b/src/Renderer/Engine.php
@@ -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);
+ }
+}
diff --git a/src/Renderer/EngineInterface.php b/src/Renderer/EngineInterface.php
index ca468db5..3bce9c02 100644
--- a/src/Renderer/EngineInterface.php
+++ b/src/Renderer/EngineInterface.php
@@ -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);
}
diff --git a/src/Renderer/HtmlEngine.php b/src/Renderer/HtmlEngine.php
index 1feafcda..0ccffa65 100644
--- a/src/Renderer/HtmlEngine.php
+++ b/src/Renderer/HtmlEngine.php
@@ -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);
}
diff --git a/src/Renderer/TwigEngine.php b/src/Renderer/TwigEngine.php
index 55a2e299..aa51a177 100644
--- a/src/Renderer/TwigEngine.php
+++ b/src/Renderer/TwigEngine.php
@@ -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);
}
diff --git a/tests/Unit/Controllers/Metrics/MetricsEngineTest.php b/tests/Unit/Controllers/Metrics/MetricsEngineTest.php
index 38817b36..87a7dc88 100644
--- a/tests/Unit/Controllers/Metrics/MetricsEngineTest.php
+++ b/tests/Unit/Controllers/Metrics/MetricsEngineTest.php
@@ -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'));
+ }
}
diff --git a/tests/Unit/Renderer/EngineTest.php b/tests/Unit/Renderer/EngineTest.php
new file mode 100644
index 00000000..659d85c5
--- /dev/null
+++ b/tests/Unit/Renderer/EngineTest.php
@@ -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()
+ );
+ }
+}
diff --git a/tests/Unit/Renderer/HtmlEngineTest.php b/tests/Unit/Renderer/HtmlEngineTest.php
index 4a31e4bc..f76e7528 100644
--- a/tests/Unit/Renderer/HtmlEngineTest.php
+++ b/tests/Unit/Renderer/HtmlEngineTest.php
@@ -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);
}
/**
diff --git a/tests/Unit/Renderer/Stub/EngineImplementation.php b/tests/Unit/Renderer/Stub/EngineImplementation.php
new file mode 100644
index 00000000..fc436569
--- /dev/null
+++ b/tests/Unit/Renderer/Stub/EngineImplementation.php
@@ -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;
+ }
+}
diff --git a/tests/Unit/Renderer/TwigEngineTest.php b/tests/Unit/Renderer/TwigEngineTest.php
index 9d0618f1..5e5e59d9 100644
--- a/tests/Unit/Renderer/TwigEngineTest.php
+++ b/tests/Unit/Renderer/TwigEngineTest.php
@@ -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);
}