From 6832a027d0c64c092da1dd54fb203e48f1b17c88 Mon Sep 17 00:00:00 2001 From: Rustam Date: Tue, 6 Jun 2023 10:53:34 +0500 Subject: [PATCH 1/6] Add `Template` class that holds information to rendering --- src/PhpTemplateRenderer.php | 4 +-- src/Template.php | 54 +++++++++++++++++++++++++++++++ src/TemplateRendererInterface.php | 5 +-- src/ViewInterface.php | 2 +- src/ViewTrait.php | 4 ++- tests/PhpTemplateRendererTest.php | 4 ++- tests/TemplateTest.php | 28 ++++++++++++++++ 7 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 src/Template.php create mode 100644 tests/TemplateTest.php diff --git a/src/PhpTemplateRenderer.php b/src/PhpTemplateRenderer.php index 16786add1..40857cad2 100644 --- a/src/PhpTemplateRenderer.php +++ b/src/PhpTemplateRenderer.php @@ -19,7 +19,7 @@ */ final class PhpTemplateRenderer implements TemplateRendererInterface { - public function render(ViewInterface $view, string $template, array $parameters): string + public function render(Template $template): string { $renderer = function (): void { /** @psalm-suppress MixedArgument */ @@ -33,7 +33,7 @@ public function render(ViewInterface $view, string $template, array $parameters) ob_implicit_flush(false); try { /** @psalm-suppress PossiblyInvalidFunctionCall,PossiblyNullFunctionCall */ - $renderer->bindTo($view)($template, $parameters); + $renderer->bindTo($template->getView())($template->getTemplate(), $template->getParameters()); return ob_get_clean(); } catch (Throwable $e) { while (ob_get_level() > $obInitialLevel) { diff --git a/src/Template.php b/src/Template.php new file mode 100644 index 000000000..71883253f --- /dev/null +++ b/src/Template.php @@ -0,0 +1,54 @@ +template; + } + + /** + * @return array + */ + public function getParameters(): array + { + return $this->parameters; + } + + /** + * @return ViewInterface + */ + public function getView(): ViewInterface + { + return $this->view; + } + + /** + * @return ViewContextInterface|null + */ + public function getViewContext(): ?ViewContextInterface + { + return $this->viewContext; + } +} diff --git a/src/TemplateRendererInterface.php b/src/TemplateRendererInterface.php index 9b17b85b9..735717435 100644 --- a/src/TemplateRendererInterface.php +++ b/src/TemplateRendererInterface.php @@ -15,11 +15,8 @@ interface TemplateRendererInterface * This method is invoked by {@see View} and {@see WebView} whenever it tries to render a view. * The classes must implement this method to render the given view file. * - * @param ViewInterface $view The view instance used for rendering the file. - * @param string $template The template file. - * @param array $parameters The parameters to be passed to the view file. * * @return string The rendering result. */ - public function render(ViewInterface $view, string $template, array $parameters): string; + public function render(Template $template): string; } diff --git a/src/ViewInterface.php b/src/ViewInterface.php index 35ac79038..8977118fc 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -28,7 +28,7 @@ public function withBasePath(string $basePath): static; * corresponding supported file extensions. * * ```php - * $view = $view->withRenderers(['twig' => new \Yiisoft\Yii\Twig\ViewRenderer($environment)]); + * $view = $view->withRenderers(['twig' => new \Yiisoft\View\Twig\TemplateRenderer($environment)]); * ``` * * If no renderer is available for the given view file, the view file will be treated as a normal PHP diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 8bcfeb134..1aae5d041 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -438,7 +438,9 @@ public function renderFile(string $viewFile, array $parameters = []): string if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer(); - $output = $renderer->render($this, $viewFile, $parameters); + $output = $renderer->render( + new Template(template: $viewFile, parameters: $parameters, view: $this, viewContext: $this->context) + ); $output = $this->afterRender($viewFile, $parameters, $output); } diff --git a/tests/PhpTemplateRendererTest.php b/tests/PhpTemplateRendererTest.php index 0f764c229..e66c8a8f9 100644 --- a/tests/PhpTemplateRendererTest.php +++ b/tests/PhpTemplateRendererTest.php @@ -5,8 +5,10 @@ namespace Yiisoft\View\Tests; use LogicException; +use phpseclib3\Crypt\DH\Parameters; use PHPUnit\Framework\TestCase; use Yiisoft\View\PhpTemplateRenderer; +use Yiisoft\View\Template; use Yiisoft\View\Tests\TestSupport\TestHelper; final class PhpTemplateRendererTest extends TestCase @@ -20,7 +22,7 @@ public function testExceptionDuringRendering(): void $obInitialLevel = ob_get_level(); try { - $renderer->render($view, __DIR__ . '/public/view/error.php', []); + $renderer->render(new Template(template: __DIR__ . '/public/view/error.php', parameters: [], view: $view)); } catch (LogicException) { } diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php new file mode 100644 index 000000000..e51f46100 --- /dev/null +++ b/tests/TemplateTest.php @@ -0,0 +1,28 @@ + 'bar'], + $view = TestHelper::createView(), + new ViewContext(__DIR__) + ); + + $this->assertSame($file, $template->getTemplate()); + $this->assertSame(['foo' => 'bar'], $template->getParameters()); + $this->assertSame($view, $template->getView()); + $this->assertSame(__DIR__, $template->getViewContext()->getViewPath()); + } +} From f7ec5668eb38180b5e7a1a373d3a6a63b5ab9bbb Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 6 Jun 2023 05:53:54 +0000 Subject: [PATCH 2/6] Apply fixes from StyleCI --- src/TemplateRendererInterface.php | 1 - tests/PhpTemplateRendererTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/TemplateRendererInterface.php b/src/TemplateRendererInterface.php index 735717435..2f6f9f977 100644 --- a/src/TemplateRendererInterface.php +++ b/src/TemplateRendererInterface.php @@ -15,7 +15,6 @@ interface TemplateRendererInterface * This method is invoked by {@see View} and {@see WebView} whenever it tries to render a view. * The classes must implement this method to render the given view file. * - * * @return string The rendering result. */ public function render(Template $template): string; diff --git a/tests/PhpTemplateRendererTest.php b/tests/PhpTemplateRendererTest.php index e66c8a8f9..f50f5a3ff 100644 --- a/tests/PhpTemplateRendererTest.php +++ b/tests/PhpTemplateRendererTest.php @@ -5,7 +5,6 @@ namespace Yiisoft\View\Tests; use LogicException; -use phpseclib3\Crypt\DH\Parameters; use PHPUnit\Framework\TestCase; use Yiisoft\View\PhpTemplateRenderer; use Yiisoft\View\Template; From 52857b467673432bd6f49a725ab6ce18633487c4 Mon Sep 17 00:00:00 2001 From: rector-bot Date: Tue, 6 Jun 2023 05:55:12 +0000 Subject: [PATCH 3/6] [ci-review] Apply changes from Rector action. --- src/Template.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Template.php b/src/Template.php index 71883253f..e93482ee7 100644 --- a/src/Template.php +++ b/src/Template.php @@ -20,33 +20,21 @@ public function __construct( ) { } - /** - * @return string - */ public function getTemplate(): string { return $this->template; } - /** - * @return array - */ public function getParameters(): array { return $this->parameters; } - /** - * @return ViewInterface - */ public function getView(): ViewInterface { return $this->view; } - /** - * @return ViewContextInterface|null - */ public function getViewContext(): ?ViewContextInterface { return $this->viewContext; From 190313a9de13c226e53cb3014d73584460b4845d Mon Sep 17 00:00:00 2001 From: Rustam Date: Wed, 12 Jul 2023 01:36:27 +0500 Subject: [PATCH 4/6] Adjustments --- src/PhpTemplateRenderer.php | 2 +- src/Template.php | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/PhpTemplateRenderer.php b/src/PhpTemplateRenderer.php index 40857cad2..08cfb2e8c 100644 --- a/src/PhpTemplateRenderer.php +++ b/src/PhpTemplateRenderer.php @@ -33,7 +33,7 @@ public function render(Template $template): string ob_implicit_flush(false); try { /** @psalm-suppress PossiblyInvalidFunctionCall,PossiblyNullFunctionCall */ - $renderer->bindTo($template->getView())($template->getTemplate(), $template->getParameters()); + $renderer->bindTo($template->getView())($template->getPath(), $template->getParameters()); return ob_get_clean(); } catch (Throwable $e) { while (ob_get_level() > $obInitialLevel) { diff --git a/src/Template.php b/src/Template.php index e93482ee7..c249d2d92 100644 --- a/src/Template.php +++ b/src/Template.php @@ -4,16 +4,19 @@ namespace Yiisoft\View; +/** + * The template holds the information needed to render a view. + */ final class Template { /** - * @param string $template The template file. - * @param array $parameters The parameters to be passed to the view file. + * @param string $path The full absolute path of the view template file. + * @param array $parameters The parameters to pass to the template. * @param ViewInterface $view The view instance used for rendering the file. * @param ViewContextInterface|null $viewContext The context instance of the view. */ public function __construct( - private string $template, + private string $path, private array $parameters, private ViewInterface $view, private ?ViewContextInterface $viewContext = null @@ -22,7 +25,7 @@ public function __construct( public function getTemplate(): string { - return $this->template; + return $this->path; } public function getParameters(): array From 178dfa50ec762a869e1373e984545343e7deaf65 Mon Sep 17 00:00:00 2001 From: Rustam Date: Wed, 12 Jul 2023 01:42:32 +0500 Subject: [PATCH 5/6] Adjustments --- src/ViewTrait.php | 6 +++--- tests/PhpTemplateRendererTest.php | 2 +- tests/TemplateTest.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 1aae5d041..10ac72cfd 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -397,7 +397,7 @@ public function render(string $view, array $parameters = []): string * Renders a view file. * * If the theme was set {@see setTheme()}, it will try to render the themed version of the view file - * as long as it is available. + * as long as it's available. * * If the renderer was set {@see withRenderers()}, the method will use it to render the view file. Otherwise, * it will simply include the view file as a normal PHP file, capture its output and return it as a string. @@ -407,7 +407,7 @@ public function render(string $view, array $parameters = []): string * file. * * @throws Throwable - * @throws ViewNotFoundException If the view file does not exist + * @throws ViewNotFoundException If the view file doesn't exist * * @return string The rendering result. */ @@ -439,7 +439,7 @@ public function renderFile(string $viewFile, array $parameters = []): string $ext = pathinfo($viewFile, PATHINFO_EXTENSION); $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer(); $output = $renderer->render( - new Template(template: $viewFile, parameters: $parameters, view: $this, viewContext: $this->context) + new Template(path: $viewFile, parameters: $parameters, view: $this, viewContext: $this->context) ); $output = $this->afterRender($viewFile, $parameters, $output); } diff --git a/tests/PhpTemplateRendererTest.php b/tests/PhpTemplateRendererTest.php index f50f5a3ff..48ad23224 100644 --- a/tests/PhpTemplateRendererTest.php +++ b/tests/PhpTemplateRendererTest.php @@ -21,7 +21,7 @@ public function testExceptionDuringRendering(): void $obInitialLevel = ob_get_level(); try { - $renderer->render(new Template(template: __DIR__ . '/public/view/error.php', parameters: [], view: $view)); + $renderer->render(new Template(path: __DIR__ . '/public/view/error.php', parameters: [], view: $view)); } catch (LogicException) { } diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index e51f46100..b6cb29d8f 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -20,7 +20,7 @@ public function testTemplate(): void new ViewContext(__DIR__) ); - $this->assertSame($file, $template->getTemplate()); + $this->assertSame($file, $template->getPath()); $this->assertSame(['foo' => 'bar'], $template->getParameters()); $this->assertSame($view, $template->getView()); $this->assertSame(__DIR__, $template->getViewContext()->getViewPath()); From 84223fd837bdf1812c7c9cd370e12bf939b6cee5 Mon Sep 17 00:00:00 2001 From: Rustam Date: Wed, 12 Jul 2023 01:44:56 +0500 Subject: [PATCH 6/6] Fix template --- src/Template.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Template.php b/src/Template.php index c249d2d92..dc9778bc1 100644 --- a/src/Template.php +++ b/src/Template.php @@ -23,21 +23,41 @@ public function __construct( ) { } - public function getTemplate(): string + /** + * Get the full absolute path of the view template file. + * + * @return string The full absolute path of the view template file. + */ + public function getPath(): string { return $this->path; } + /** + * Get the parameters to pass to the template. + * + * @return array The parameters to pass to the template. + */ public function getParameters(): array { return $this->parameters; } + /** + * Get the view instance used for rendering the file. + * + * @return ViewInterface The view instance used for rendering the file. + */ public function getView(): ViewInterface { return $this->view; } + /** + * Get the context instance of the view. + * + * @return ViewContextInterface|null The context instance of the view. + */ public function getViewContext(): ?ViewContextInterface { return $this->viewContext;