generated from yii-tools/template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ViewTemplateRenderer::class
. (#25)
- Loading branch information
1 parent
8f043a7
commit d9aa65e
Showing
8 changed files
with
231 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\Service; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Yiisoft\Aliases\Aliases; | ||
use Yiisoft\DataResponse\DataResponseFactoryInterface; | ||
use Yiisoft\View\ViewContextInterface; | ||
use Yiisoft\View\WebView; | ||
|
||
final class ViewTemplateRenderer implements ViewContextInterface | ||
{ | ||
private string $layout = '@layout/main.php'; | ||
private array $layoutParameters = []; | ||
private string $viewPath = '@views'; | ||
|
||
public function __construct( | ||
private readonly Aliases $aliases, | ||
private readonly DataResponseFactoryInterface $dataResponse, | ||
private readonly WebView $webView, | ||
) { | ||
} | ||
|
||
public function getViewPath(): string | ||
{ | ||
return $this->findAliases($this->viewPath); | ||
} | ||
|
||
public function render(string $view, array $viewParameters = [], array $layoutParameters = []): ResponseInterface | ||
{ | ||
$content = $this->renderView($view, $viewParameters); | ||
|
||
$parameters = ['content' => $content, ...$layoutParameters, ...$this->layoutParameters]; | ||
|
||
$layout = $this->findAliases($this->layout); | ||
|
||
$content = $this->webView->renderFile($layout, $parameters); | ||
|
||
return $this->dataResponse->createResponse($content); | ||
} | ||
|
||
public function withLayoutParameters(array $parameters): self | ||
{ | ||
$new = clone $this; | ||
$new->layoutParameters = $parameters; | ||
|
||
return $new; | ||
} | ||
|
||
public function withViewPath(string $path): self | ||
{ | ||
$new = clone $this; | ||
$new->viewPath = $path; | ||
|
||
return $new; | ||
} | ||
|
||
private function findAliases(string $aliases): string | ||
{ | ||
if (substr($aliases, 0, 1) === '@') { | ||
return $this->aliases->get($aliases); | ||
} | ||
|
||
return $aliases; | ||
} | ||
|
||
private function renderView(string $view, array $viewParameters): string | ||
{ | ||
return $this->webView->withContext($this)->render($view, $viewParameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\Service\Tests\ViewTemplateRenderer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yii\Service\Tests\Support\TestTrait; | ||
|
||
final class ImmutabilityTest extends TestCase | ||
{ | ||
use TestTrait; | ||
|
||
public function testImmutability(): void | ||
{ | ||
$viewTemplateRenderer = $this->viewTemplateRenderer; | ||
|
||
$this->assertNotSame($viewTemplateRenderer, $viewTemplateRenderer->withLayoutParameters([])); | ||
$this->assertNotSame($viewTemplateRenderer, $viewTemplateRenderer->withViewPath('')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\Service\Tests\ViewTemplateRenderer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yii\Service\Tests\Support\TestTrait; | ||
use Yii\Support\Assert; | ||
use Yiisoft\DataResponse\DataResponse; | ||
|
||
final class Test extends TestCase | ||
{ | ||
use TestTrait; | ||
|
||
public function testRender(): void | ||
{ | ||
$this->aliases->set('@layout', dirname(__DIR__) . '/data/resources/view'); | ||
|
||
/** @var DataResponse $dataResponse */ | ||
$dataResponse = $this->viewTemplateRenderer | ||
->withViewPath(dirname(__DIR__) . '/data/resources/view') | ||
->render('view', ['title' => 'View'], ['title' => 'Layout']); | ||
|
||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Layout</title> | ||
</head> | ||
<body> | ||
<h1>View</h1> | ||
</body> | ||
</html> | ||
HTML, | ||
$dataResponse->getData(), | ||
); | ||
} | ||
|
||
public function testWithLayoutParameters(): void | ||
{ | ||
$this->aliases->set('@layout', dirname(__DIR__) . '/data/resources/view'); | ||
|
||
/** @var DataResponse $dataResponse */ | ||
$dataResponse = $this->viewTemplateRenderer | ||
->withViewPath(dirname(__DIR__) . '/data/resources/view') | ||
->withLayoutParameters(['csrfToken' => 'test']) | ||
->render('view', ['title' => 'View'], ['title' => 'Layout']); | ||
|
||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="csrf-token" content="test"> | ||
<title>Layout</title> | ||
</head> | ||
<body> | ||
<h1>View</h1> | ||
</body> | ||
</html> | ||
HTML, | ||
$dataResponse->getData(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use HttpSoft\Message\StreamFactory; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
|
||
return [ | ||
StreamFactoryInterface::class => StreamFactory::class, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* @var $this \Yiisoft\View\WebView | ||
* @var $content string | ||
* @var string|null $csrfToken | ||
*/ | ||
$csrfToken = $csrfToken ?? null; | ||
?> | ||
<?php $this->beginPage(); ?> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<?php if ($csrfToken !== null): ?> | ||
<meta name="csrf-token" content="<?= $csrfToken ?>"> | ||
<?php endif ?> | ||
<title><?= $title ?></title> | ||
</head> | ||
<body> | ||
<?php $this->beginBody(); ?> | ||
|
||
<?= $content ?> | ||
|
||
<?php $this->endBody(); ?> | ||
</body> | ||
</html> | ||
<?php $this->endPage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
/** @var string $title */ | ||
?> | ||
<h1><?= $title ?></h1> |