Add default views path to ease renderers instanciation.

This commit is contained in:
Madeorsk 2024-11-08 18:39:42 +01:00
parent 596af225e5
commit dbf10374cb
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
4 changed files with 88 additions and 3 deletions

View file

@ -0,0 +1,12 @@
<?php
namespace Nest\Http\Exceptions\Renderer;
use Nest\Http\Exceptions\NotFoundException;
/**
* Exception thrown when the given view could not be found.
*/
class ViewNotFoundException extends NotFoundException
{
}

View file

@ -3,12 +3,14 @@
namespace Nest\Http;
use Nest\Application;
use Nest\Http\Exceptions\Renderer\ViewNotFoundException;
use Nyholm\Psr7\ServerRequest;
use Override;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function Nest\Utils\path_join;
/**
* HTTP service manager.
@ -38,6 +40,38 @@ class Http
return $this->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.

View file

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

View file

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