Skip to content

Commit

Permalink
Make base path in View and WebView optional (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Mar 16, 2024
1 parent 57372ba commit ab54747
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Bug #232: Fix render templates that contain dots in their name (@rustamwin)
- New #242: Add `View::getLocale()` and `WebView::getLocale()` methods (@Tigrov)
- Enh #250: Make event dispatcher in `View` and `WebView` optional (@vjik)
- Enh #251: Make base path in `View` and `WebView` optional (@vjik)

## 8.0.0 February 16, 2023

Expand Down
4 changes: 2 additions & 2 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ final class View implements ViewInterface
private ThemeState $themeState;

/**
* @param string $basePath The full path to the base directory of views.
* @param string|null $basePath The full path to the base directory of views.
* @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance.
*/
public function __construct(string $basePath, ?EventDispatcherInterface $eventDispatcher = null)
public function __construct(?string $basePath = null, ?EventDispatcherInterface $eventDispatcher = null)
{
$this->basePath = $basePath;
$this->state = new ViewState();
Expand Down
9 changes: 7 additions & 2 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\View;

use InvalidArgumentException;
use LogicException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
use RuntimeException;
Expand Down Expand Up @@ -36,7 +37,7 @@ trait ViewTrait
{
private ?EventDispatcherInterface $eventDispatcher;

private string $basePath;
private ?string $basePath = null;
private ?ViewContextInterface $context = null;
private string $placeholderSignature;
private string $sourceLocale = 'en';
Expand Down Expand Up @@ -207,6 +208,10 @@ public function getLocale(): string
*/
public function getBasePath(): string
{
if ($this->basePath === null) {
throw new LogicException('The base path is not set.');
}

return $this->basePath;
}

Expand Down Expand Up @@ -643,7 +648,7 @@ private function findTemplateFile(string $view): string
{
if ($view !== '' && $view[0] === '/') {
// path relative to basePath e.g. "/layouts/main"
$file = $this->basePath . '/' . ltrim($view, '/');
$file = $this->getBasePath() . '/' . ltrim($view, '/');
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== null) {
// path relative to currently rendered view
$file = dirname($currentViewFile) . '/' . $view;
Expand Down
4 changes: 2 additions & 2 deletions src/WebView.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ final class WebView implements ViewInterface
private const PLACEHOLDER_BODY_END = '<![CDATA[YII-BLOCK-BODY-END-%s]]>';

/**
* @param string $basePath The full path to the base directory of views.
* @param string|null $basePath The full path to the base directory of views.
* @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance.
*/
public function __construct(string $basePath, ?EventDispatcherInterface $eventDispatcher = null)
public function __construct(?string $basePath = null, ?EventDispatcherInterface $eventDispatcher = null)
{
$this->basePath = $basePath;
$this->state = new WebViewState();
Expand Down
10 changes: 10 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Yiisoft\Files\FileHelper;
Expand Down Expand Up @@ -609,6 +610,15 @@ public function testGetLocale()
$this->assertSame('en-US', $view->getLocale());
}

public function testWithoutBasePath(): void
{
$view = new View();

$this->expectException(LogicException::class);
$this->expectExceptionMessage('The base path is not set.');
$view->getBasePath();
}

private function createViewWithBasePath(string $basePath): View
{
return new View($basePath, new SimpleEventDispatcher());
Expand Down

0 comments on commit ab54747

Please sign in to comment.