From 0f8aa1627b3b9c32c67483d66ec248aab8ec856e Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 15 Mar 2024 11:41:32 +0300 Subject: [PATCH 1/2] Make `viewPath` in `ViewRenderer` constructor optional --- CHANGELOG.md | 1 + src/ViewRenderer.php | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 546588f..27b5acf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Enh #79: Add debug collector for yiisoft/yii-debug (@xepozz) - Bug #82: Fixed find for layout file due to compatibility with `yiisoft/view` (@rustamwin) +- Enh #99: Make `viewPath` in `ViewRenderer` constructor optional (@vjik) ## 6.0.0 February 16, 2023 diff --git a/src/ViewRenderer.php b/src/ViewRenderer.php index cec4c66..96d8463 100644 --- a/src/ViewRenderer.php +++ b/src/ViewRenderer.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\View; +use LogicException; use RuntimeException; use Throwable; use Yiisoft\Aliases\Aliases; @@ -42,7 +43,7 @@ */ final class ViewRenderer implements ViewContextInterface { - private string $viewPath; + private ?string $viewPath = null; private ?string $name = null; private ?string $locale = null; @@ -50,7 +51,7 @@ final class ViewRenderer implements ViewContextInterface * @param DataResponseFactoryInterface $responseFactory The data response factory instance. * @param Aliases $aliases The aliases instance. * @param WebView $view The web view instance. - * @param string $viewPath The full path to the directory of views or its alias. + * @param string|null $viewPath The full path to the directory of views or its alias. * @param string|null $layout The layout name (e.g. "layout/main") to be applied to views. * If null, the layout will not be applied. * @param object[] $injections The injection instances. @@ -59,11 +60,11 @@ public function __construct( private DataResponseFactoryInterface $responseFactory, private Aliases $aliases, private WebView $view, - string $viewPath, + ?string $viewPath, private ?string $layout = null, private array $injections = [] ) { - $this->viewPath = rtrim($viewPath, '/'); + $this->setViewPath($viewPath); } /** @@ -75,6 +76,10 @@ public function __construct( */ public function getViewPath(): string { + if ($this->viewPath === null) { + throw new LogicException('The view path is not set.'); + } + return $this->aliases->get($this->viewPath) . ($this->name ? '/' . $this->name : ''); } @@ -218,7 +223,7 @@ public function withControllerName(string $name): self public function withViewPath(string $viewPath): self { $new = clone $this; - $new->viewPath = rtrim($viewPath, '/'); + $new->setViewPath($viewPath); return $new; } @@ -553,4 +558,9 @@ private function getType(mixed $value): string { return get_debug_type($value); } + + private function setViewPath(?string $path): void + { + $this->viewPath = $path === null ? null : rtrim($path, '/'); + } } From 8619ffcabd30b315e3866161fcd3ec0b85603293 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 15 Mar 2024 11:53:20 +0300 Subject: [PATCH 2/2] fix + test --- src/ViewRenderer.php | 2 +- tests/ViewRendererTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ViewRenderer.php b/src/ViewRenderer.php index 96d8463..92565d8 100644 --- a/src/ViewRenderer.php +++ b/src/ViewRenderer.php @@ -60,7 +60,7 @@ public function __construct( private DataResponseFactoryInterface $responseFactory, private Aliases $aliases, private WebView $view, - ?string $viewPath, + ?string $viewPath = null, private ?string $layout = null, private array $injections = [] ) { diff --git a/tests/ViewRendererTest.php b/tests/ViewRendererTest.php index 01763d2..9624498 100644 --- a/tests/ViewRendererTest.php +++ b/tests/ViewRendererTest.php @@ -6,6 +6,7 @@ use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\StreamFactory; +use LogicException; use PHPUnit\Framework\TestCase; use ReflectionObject; use RuntimeException; @@ -466,6 +467,19 @@ public function testRenderParametersNotOverrideLayout(): void $this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody()); } + public function testWithoutViewPath(): void + { + $viewRenderer = new ViewRenderer( + new DataResponseFactory(new ResponseFactory(), new StreamFactory()), + new Aliases(), + new WebView(__DIR__, new SimpleEventDispatcher()), + ); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('The view path is not set.'); + $viewRenderer->getViewPath(); + } + public function testImmutability(): void { $original = $this->getRenderer();