Skip to content

Commit 55ef662

Browse files
authored
Allow to pass null to ViewInterface methods withBasePath() and withContext() (#276)
1 parent 02448f8 commit 55ef662

7 files changed

Lines changed: 58 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 11.0.2 under development
44

5-
- no changes in this release.
5+
- Chg #276: Allow to pass `null` to `ViewInterface` methods `withBasePath()` and `withContext()` (@vjik)
66

77
## 11.0.1 October 08, 2024
88

src/ViewInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ interface ViewInterface
1919
/**
2020
* Returns a new instance with specified base path to the view directory.
2121
*
22-
* @param string $basePath The base path to the view directory.
22+
* @param string|null $basePath The base path to the view directory.
2323
*/
24-
public function withBasePath(string $basePath): static;
24+
public function withBasePath(string|null $basePath): static;
2525

2626
/**
2727
* Returns a new instance with the specified renderers.
@@ -50,9 +50,9 @@ public function withSourceLocale(string $locale): static;
5050
/**
5151
* Returns a new instance with the specified view context instance.
5252
*
53-
* @param ViewContextInterface $context The context under which the {@see render()} method is being invoked.
53+
* @param ViewContextInterface|null $context The context under which the {@see render()} method is being invoked.
5454
*/
55-
public function withContext(ViewContextInterface $context): static;
55+
public function withContext(ViewContextInterface|null $context): static;
5656

5757
/**
5858
* Returns a new instance with the specified view context path.

src/ViewTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ trait ViewTrait
6262
/**
6363
* Returns a new instance with specified base path to the view directory.
6464
*
65-
* @param string $basePath The base path to the view directory.
65+
* @param string|null $basePath The base path to the view directory.
6666
*/
67-
public function withBasePath(string $basePath): static
67+
public function withBasePath(string|null $basePath): static
6868
{
6969
$new = clone $this;
7070
$new->basePath = $basePath;
@@ -121,9 +121,9 @@ public function withFallbackExtension(string $fallbackExtension, string ...$othe
121121
/**
122122
* Returns a new instance with the specified view context instance.
123123
*
124-
* @param ViewContextInterface $context The context under which the {@see render()} method is being invoked.
124+
* @param ViewContextInterface|null $context The context under which the {@see render()} method is being invoked.
125125
*/
126-
public function withContext(ViewContextInterface $context): static
126+
public function withContext(ViewContextInterface|null $context): static
127127
{
128128
$new = clone $this;
129129
$new->context = $context;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\View\Tests\View\ResetContext;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\View\View;
9+
use Yiisoft\View\ViewContext;
10+
11+
final class ResetContextTest extends TestCase
12+
{
13+
public function testBase(): void
14+
{
15+
$baseView = (new View(__DIR__ . '/views-base'))
16+
->withContext(new ViewContext(__DIR__ . '/views-context'));
17+
18+
$view = $baseView->withContext(null);
19+
20+
$baseContent = $baseView->render('test');
21+
$content = $view->render('test');
22+
23+
$this->assertSame('View Context', $baseContent);
24+
$this->assertSame('View Base', $content);
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
echo 'View Base';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
echo 'View Context';

tests/ViewTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,19 @@ public function testWithoutBasePath(): void
614614
$view->getBasePath();
615615
}
616616

617+
public function testResetBasePath(): void
618+
{
619+
$baseView = new View(__DIR__);
620+
621+
$view = $baseView->withBasePath(null);
622+
623+
$this->assertSame(__DIR__, $baseView->getBasePath());
624+
625+
$this->expectException(LogicException::class);
626+
$this->expectExceptionMessage('The base path is not set.');
627+
$view->getBasePath();
628+
}
629+
617630
private function createViewWithBasePath(string $basePath): View
618631
{
619632
return new View($basePath, new SimpleEventDispatcher());

0 commit comments

Comments
 (0)