diff options
Diffstat (limited to 'src/Renderer')
-rw-r--r-- | src/Renderer/EngineInterface.php | 21 | ||||
-rw-r--r-- | src/Renderer/HtmlEngine.php | 34 | ||||
-rw-r--r-- | src/Renderer/Renderer.php | 47 | ||||
-rw-r--r-- | src/Renderer/RendererServiceProvider.php | 36 |
4 files changed, 138 insertions, 0 deletions
diff --git a/src/Renderer/EngineInterface.php b/src/Renderer/EngineInterface.php new file mode 100644 index 00000000..ca468db5 --- /dev/null +++ b/src/Renderer/EngineInterface.php @@ -0,0 +1,21 @@ +<?php + +namespace Engelsystem\Renderer; + +interface EngineInterface +{ + /** + * Render a template + * + * @param string $path + * @param mixed[] $data + * @return string + */ + public function get($path, $data = []); + + /** + * @param string $path + * @return bool + */ + public function canRender($path); +} diff --git a/src/Renderer/HtmlEngine.php b/src/Renderer/HtmlEngine.php new file mode 100644 index 00000000..75343bbd --- /dev/null +++ b/src/Renderer/HtmlEngine.php @@ -0,0 +1,34 @@ +<?php + +namespace Engelsystem\Renderer; + +class HtmlEngine implements EngineInterface +{ + /** + * Render a template + * + * @param string $path + * @param mixed[] $data + * @return string + */ + public function get($path, $data = []) + { + $template = file_get_contents($path); + if (is_array($data)) { + foreach ($data as $name => $content) { + $template = str_replace('%' . $name . '%', $content, $template); + } + } + + return $template; + } + + /** + * @param string $path + * @return bool + */ + public function canRender($path) + { + return strpos($path, '.htm') && file_exists($path); + } +} diff --git a/src/Renderer/Renderer.php b/src/Renderer/Renderer.php new file mode 100644 index 00000000..de31ca74 --- /dev/null +++ b/src/Renderer/Renderer.php @@ -0,0 +1,47 @@ +<?php + +namespace Engelsystem\Renderer; + +use Psr\Log\LoggerAwareTrait; + +class Renderer +{ + use LoggerAwareTrait; + + /** @var EngineInterface[] */ + protected $renderer = []; + + /** + * Render a template + * + * @param string $template + * @param mixed[] $data + * @return string + */ + public function render($template, $data = []) + { + foreach ($this->renderer as $renderer) { + if (!$renderer->canRender($template)) { + continue; + } + + return $renderer->get($template, $data); + } + + if ($this->logger) { + $this->logger->error('Unable to find a renderer for template file "{file}"', ['file' => $template]); + } + + return ''; + } + + /** + * Add a new renderer engine + * + * @param EngineInterface $renderer + */ + public function addRenderer(EngineInterface $renderer) + { + $this->renderer[] = $renderer; + } +} diff --git a/src/Renderer/RendererServiceProvider.php b/src/Renderer/RendererServiceProvider.php new file mode 100644 index 00000000..3e8d69bc --- /dev/null +++ b/src/Renderer/RendererServiceProvider.php @@ -0,0 +1,36 @@ +<?php + +namespace Engelsystem\Renderer; + +use Engelsystem\Container\ServiceProvider; + +class RendererServiceProvider extends ServiceProvider +{ + public function register() + { + $this->registerRenderer(); + $this->registerHtmlEngine(); + } + + public function boot() + { + $renderer = $this->app->get('renderer'); + + foreach ($this->app->tagged('renderer.engine') as $engine) { + $renderer->addRenderer($engine); + } + } + + protected function registerRenderer() + { + $renderer = $this->app->make(Renderer::class); + $this->app->instance('renderer', $renderer); + } + + protected function registerHtmlEngine() + { + $htmlEngine = $this->app->make(HtmlEngine::class); + $this->app->instance('renderer.htmlEngine', $htmlEngine); + $this->app->tag('renderer.htmlEngine', ['renderer.engine']); + } +} |