Add default views path to ease renderers instanciation.
This commit is contained in:
parent
596af225e5
commit
dbf10374cb
4 changed files with 88 additions and 3 deletions
12
src/Exceptions/Renderer/ViewNotFoundException.php
Normal file
12
src/Exceptions/Renderer/ViewNotFoundException.php
Normal 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
|
||||
{
|
||||
}
|
34
src/Http.php
34
src/Http.php
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue