summaryrefslogtreecommitdiff
path: root/src/Renderer
diff options
context:
space:
mode:
Diffstat (limited to 'src/Renderer')
-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/Twig/Extensions/Translation.php2
-rw-r--r--src/Renderer/TwigEngine.php8
5 files changed, 41 insertions, 9 deletions
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/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php
index 41619c19..3e6f30b4 100644
--- a/src/Renderer/Twig/Extensions/Translation.php
+++ b/src/Renderer/Twig/Extensions/Translation.php
@@ -2,7 +2,7 @@
namespace Engelsystem\Renderer\Twig\Extensions;
-use Engelsystem\Helpers\Translator;
+use Engelsystem\Helpers\Translation\Translator;
use Twig_Extension as TwigExtension;
use Twig_Extensions_TokenParser_Trans as TranslationTokenParser;
use Twig_Filter as TwigFilter;
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);
}