Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make viewPath in ViewRenderer constructor optional #99

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
private DataResponseFactoryInterface $responseFactory,
private Aliases $aliases,
private WebView $view,
string $viewPath,
?string $viewPath = null,
private ?string $layout = null,
private array $injections = []
) {
$this->viewPath = rtrim($viewPath, '/');
$this->setViewPath($viewPath);
}

/**
Expand All @@ -75,6 +76,10 @@
*/
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 : '');
}

Expand Down Expand Up @@ -218,7 +223,7 @@
public function withViewPath(string $viewPath): self
{
$new = clone $this;
$new->viewPath = rtrim($viewPath, '/');
$new->setViewPath($viewPath);
return $new;
}

Expand All @@ -243,7 +248,7 @@
public function withAddedInjections(object ...$injections): self
{
$new = clone $this;
$new->injections = array_merge($this->injections, $injections);

Check warning on line 251 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ public function withAddedInjections(object ...$injections) : self { $new = clone $this; - $new->injections = array_merge($this->injections, $injections); + $new->injections = $this->injections; return $new; } /**

Check warning on line 251 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ public function withAddedInjections(object ...$injections) : self { $new = clone $this; - $new->injections = array_merge($this->injections, $injections); + $new->injections = $injections; return $new; } /**

Check warning on line 251 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ public function withAddedInjections(object ...$injections) : self { $new = clone $this; - $new->injections = array_merge($this->injections, $injections); + $new->injections = $this->injections; return $new; } /**

Check warning on line 251 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ public function withAddedInjections(object ...$injections) : self { $new = clone $this; - $new->injections = array_merge($this->injections, $injections); + $new->injections = $injections; return $new; } /**
return $new;
}

Expand All @@ -266,7 +271,7 @@
*/
public function withLocale(string $locale): self
{
$new = clone $this;

Check warning on line 274 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withLocale(string $locale) : self { - $new = clone $this; + $new = $this; $new->locale = $locale; return $new; }

Check warning on line 274 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withLocale(string $locale) : self { - $new = clone $this; + $new = $this; $new->locale = $locale; return $new; }
$new->locale = $locale;
return $new;
}
Expand Down Expand Up @@ -345,10 +350,10 @@
$parameters = [];
foreach ($this->injections as $injection) {
if ($injection instanceof CommonParametersInjectionInterface) {
$parameters = array_merge($parameters, $injection->getCommonParameters());

Check warning on line 353 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $parameters = []; foreach ($this->injections as $injection) { if ($injection instanceof CommonParametersInjectionInterface) { - $parameters = array_merge($parameters, $injection->getCommonParameters()); + $parameters = $injection->getCommonParameters(); } } return $parameters;

Check warning on line 353 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $parameters = []; foreach ($this->injections as $injection) { if ($injection instanceof CommonParametersInjectionInterface) { - $parameters = array_merge($parameters, $injection->getCommonParameters()); + $parameters = $injection->getCommonParameters(); } } return $parameters;
}
}
return $parameters;

Check warning on line 356 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ $parameters = array_merge($parameters, $injection->getCommonParameters()); } } - return $parameters; + return count($parameters) > 1 ? array_slice($parameters, 0, 1, true) : $parameters; } /** * Gets the merged injection layout parameters.

Check warning on line 356 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ $parameters = array_merge($parameters, $injection->getCommonParameters()); } } - return $parameters; + return count($parameters) > 1 ? array_slice($parameters, 0, 1, true) : $parameters; } /** * Gets the merged injection layout parameters.
}

/**
Expand All @@ -363,7 +368,7 @@
$parameters = [];
foreach ($this->injections as $injection) {
if ($injection instanceof LayoutParametersInjectionInterface) {
$parameters = array_merge($parameters, $injection->getLayoutParameters());

Check warning on line 371 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $parameters = []; foreach ($this->injections as $injection) { if ($injection instanceof LayoutParametersInjectionInterface) { - $parameters = array_merge($parameters, $injection->getLayoutParameters()); + $parameters = $injection->getLayoutParameters(); } } return $parameters;

Check warning on line 371 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $parameters = []; foreach ($this->injections as $injection) { if ($injection instanceof LayoutParametersInjectionInterface) { - $parameters = array_merge($parameters, $injection->getLayoutParameters()); + $parameters = $injection->getLayoutParameters(); } } return $parameters;
}
}
return $parameters;
Expand Down Expand Up @@ -553,4 +558,9 @@
{
return get_debug_type($value);
}

private function setViewPath(?string $path): void
{
$this->viewPath = $path === null ? null : rtrim($path, '/');
}
}
14 changes: 14 additions & 0 deletions tests/ViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use LogicException;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use RuntimeException;
Expand Down Expand Up @@ -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();
Expand Down
Loading