Skip to content

Commit

Permalink
Make viewPath in ViewRenderer constructor optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Mar 15, 2024
1 parent 33c85e0 commit 0f8aa16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 15 additions & 5 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\View;

use LogicException;
use RuntimeException;
use Throwable;
use Yiisoft\Aliases\Aliases;
Expand Down Expand Up @@ -42,15 +43,15 @@
*/
final class ViewRenderer implements ViewContextInterface
{
private string $viewPath;
private ?string $viewPath = null;
private ?string $name = null;
private ?string $locale = null;

/**
* @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.
Expand All @@ -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);
}

/**
Expand All @@ -75,6 +76,10 @@ public function __construct(
*/
public function getViewPath(): string
{
if ($this->viewPath === null) {
throw new LogicException('The view path is not set.');

Check warning on line 80 in src/ViewRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/ViewRenderer.php#L80

Added line #L80 was not covered by tests
}

return $this->aliases->get($this->viewPath) . ($this->name ? '/' . $this->name : '');
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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, '/');
}
}

0 comments on commit 0f8aa16

Please sign in to comment.