From dbf10374cb7c2469381947c20a15225305efd6a3 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Fri, 8 Nov 2024 18:39:42 +0100 Subject: [PATCH] Add default views path to ease renderers instanciation. --- .../Renderer/ViewNotFoundException.php | 12 +++++++ src/Http.php | 34 +++++++++++++++++++ src/HttpConfiguration.php | 26 ++++++++++++++ src/Renderer/PhpRenderer.php | 19 +++++++++-- 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/Exceptions/Renderer/ViewNotFoundException.php diff --git a/src/Exceptions/Renderer/ViewNotFoundException.php b/src/Exceptions/Renderer/ViewNotFoundException.php new file mode 100644 index 0000000..26bc9bf --- /dev/null +++ b/src/Exceptions/Renderer/ViewNotFoundException.php @@ -0,0 +1,12 @@ +configuration->getDebug(); } + /** + * Get default views path, if there is one. + * @return string|null Default views path. + */ + public function getDefaultViewsPath(): ?string + { + return $this->configuration->getViewsPath(); + } + + /** + * Resolve the given view name to an absolute path to the actual view file. + * @param string $viewName Name of the view to resolve. + * @return string Absolute path to the view file. + * @throws ViewNotFoundException + */ + public function resolveView(string $viewName): string + { + if (file_exists($viewName)) + // If the view name is already a valid file, just return it. + return $viewName; + else + { // Try to build a view absolute path with the given view name. + $viewPath = path_join($this->getDefaultViewsPath(), "$viewName.php"); + if (file_exists($viewPath)) return $viewPath; + $viewPath = path_join($this->getDefaultViewsPath(), "$viewName"); + if (file_exists($viewPath)) return $viewPath; + + // No view path found, throw an exception. + throw new ViewNotFoundException(); + } + } + /** * Parse the current server request from global context. * @return ServerRequestInterface Parsed server request. diff --git a/src/HttpConfiguration.php b/src/HttpConfiguration.php index a602a27..77a7cd5 100644 --- a/src/HttpConfiguration.php +++ b/src/HttpConfiguration.php @@ -29,6 +29,12 @@ class HttpConfiguration extends ServiceConfiguration */ private string $exceptionRenderer = ExceptionRenderer::class; + /** + * Default views directory path, if there is one. + * @var string|null + */ + private ?string $viewsPath = null; + /** * The main request handler. * @var RequestHandlerInterface @@ -71,6 +77,17 @@ class HttpConfiguration extends ServiceConfiguration return $this; } + /** + * Set default views path. + * @param string|null $viewsPath Path to views directory. NULL to remove default paths directory. + * @return $this + */ + public function viewsPath(?string $viewsPath): static + { + $this->viewsPath = $viewsPath; + return $this; + } + /** * Set HTTP request handler. * @param (callable(ServerRequestInterface): ResponseInterface)|RequestHandlerInterface $requestHandler @@ -171,6 +188,15 @@ class HttpConfiguration extends ServiceConfiguration return $this->exceptionRenderer; } + /** + * Get default views path. + * @return string|null Default views path, if there is one. + */ + public function getViewsPath(): ?string + { + return $this->viewsPath; + } + /** * Get configured request handler. * @return RequestHandlerInterface diff --git a/src/Renderer/PhpRenderer.php b/src/Renderer/PhpRenderer.php index abccaba..d9a8a13 100644 --- a/src/Renderer/PhpRenderer.php +++ b/src/Renderer/PhpRenderer.php @@ -2,16 +2,29 @@ namespace Nest\Http\Renderer; +use Nest\Application; +use Nest\Http\Exceptions\Renderer\ViewNotFoundException; + /** * Plain PHP body renderer. */ class PhpRenderer implements Renderer { /** - * @param string $viewPath Path of the view to use when rendering. + * Resolved view path. + * @var string */ - public function __construct(protected string $viewPath) - {} + protected string $viewPath; + + /** + * @param string $viewName Name of the view to use when rendering. + * @throws ViewNotFoundException + */ + public function __construct(protected string $viewName) + { + // Try to resolve view path. + $this->viewPath = Application::get()->http()->resolveView($viewName); + } /** * @inheritDoc