From 36beb5d8423734ecc2f30e3c0bd7888a1eef945c Mon Sep 17 00:00:00 2001 From: Rustam Date: Thu, 20 Apr 2023 11:57:23 +0500 Subject: [PATCH 1/5] Improve config, ability to get view context --- config/di-web.php | 18 +++++++++++++----- config/di.php | 18 +++++++++++++----- config/params.php | 2 ++ src/ViewInterface.php | 7 +++++++ src/ViewTrait.php | 10 +++++++++- tests/ViewTest.php | 9 +++++++++ 6 files changed, 53 insertions(+), 11 deletions(-) diff --git a/config/di-web.php b/config/di-web.php index cd1096def..a135b5d23 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -2,7 +2,9 @@ declare(strict_types=1); +use Psr\Container\ContainerInterface; use Yiisoft\Aliases\Aliases; +use Yiisoft\Definitions\Contract\ReferenceInterface; use Yiisoft\Definitions\DynamicReference; use Yiisoft\View\Theme; use Yiisoft\View\WebView; @@ -30,13 +32,19 @@ static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath']) ), ], - 'setParameters()' => [ - $params['yiisoft/view']['parameters'], - ], - 'reset' => function () use ($params) { + 'setParameters()' => ['parameters' => $params['yiisoft/view']['parameters']], + 'withRenderers()' => ['renderers' => $params['yiisoft/view']['renderers']], + 'withDefaultExtension()' => [$params['yiisoft/view']['defaultExtension']], + 'reset' => function (ContainerInterface $container) use ($params) { /** @var WebView $this */ $this->clear(); - $this->setParameters($params['yiisoft/view']['parameters']); + $parameters = $params['yiisoft/view']['parameters']; + foreach ($parameters as $name => $parameter) { + $parameters[$name] = $parameter instanceof ReferenceInterface ? + $parameter->resolve($container) : + $parameter; + } + $this->setParameters($parameters); }, ], ]; diff --git a/config/di.php b/config/di.php index b6993460f..90e6a1ae7 100644 --- a/config/di.php +++ b/config/di.php @@ -2,7 +2,9 @@ declare(strict_types=1); +use Psr\Container\ContainerInterface; use Yiisoft\Aliases\Aliases; +use Yiisoft\Definitions\Contract\ReferenceInterface; use Yiisoft\Definitions\DynamicReference; use Yiisoft\View\View; @@ -15,13 +17,19 @@ static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath']) ), ], - 'setParameters()' => [ - $params['yiisoft/view']['parameters'], - ], - 'reset' => function () use ($params) { + 'setParameters()' => ['parameters' => $params['yiisoft/view']['parameters']], + 'withRenderers()' => ['renderers' => $params['yiisoft/view']['renderers']], + 'withDefaultExtension()' => [$params['yiisoft/view']['defaultExtension']], + 'reset' => function (ContainerInterface $container) use ($params) { /** @var View $this */ $this->clear(); - $this->setParameters($params['yiisoft/view']['parameters']); + $parameters = $params['yiisoft/view']['parameters']; + foreach ($parameters as $name => $parameter) { + $parameters[$name] = $parameter instanceof ReferenceInterface ? + $parameter->resolve($container) : + $parameter; + } + $this->setParameters($parameters); }, ], ]; diff --git a/config/params.php b/config/params.php index 87d01f463..0ccf5de16 100644 --- a/config/params.php +++ b/config/params.php @@ -11,5 +11,7 @@ 'basePath' => '', 'baseUrl' => '', ], + 'renderers' => [], + 'defaultExtension' => 'php', ], ]; diff --git a/src/ViewInterface.php b/src/ViewInterface.php index 35ac79038..22bdeeca7 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -100,6 +100,13 @@ public function withLocale(string $locale): static; */ public function getBasePath(): string; + /** + * Gets the context instance, or `null` if no context has been set. + * + * @return ViewContextInterface|null The context instance, or `null` if no context has been set. + */ + public function getContext(): ?ViewContextInterface; + /** * Gets the default view file extension. * diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 8bcfeb134..6d2a5baa4 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -75,7 +75,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\ViewRenderer($environment)]); * ``` * * If no renderer is available for the given view file, the view file will be treated as a normal PHP @@ -184,6 +184,14 @@ public function getBasePath(): string return $this->basePath; } + /** + * Gets the context instance, or `null` if no context has been set. + */ + public function getContext(): ?ViewContextInterface + { + return $this->context; + } + /** * Gets the default view file extension. * diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 9834fdf73..472363ec6 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -197,6 +197,15 @@ public function testRenderWithoutFileExtension(): void ->render('file')); } + public function testContext(): void + { + $view = $this->createViewWithBasePath($this->tempDirectory); + $context = $this->createContext($this->tempDirectory); + $view = $view->withContext($context); + + $this->assertSame($context, $view->getContext()); + } + public function testLocalize(): void { $view = $this->createViewWithBasePath($this->tempDirectory); From f378b696917c2a086b634ef072ccd19b16859f88 Mon Sep 17 00:00:00 2001 From: Rustam Date: Thu, 20 Apr 2023 12:48:58 +0500 Subject: [PATCH 2/5] Add changelog --- CHANGELOG.md | 3 +++ src/ViewTrait.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32ac3cb36..a3fa60ec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ## 8.0.1 under development - Bug #224: Fix signature of `CachedContent::cache()` (@vjik) +- Bug #226: Fix `reset` config for referenced definitions (@rustamwin) +- Chg #226: Adjust config to make `View` and `WebView` more configurable (@rustamwin) +- Enh #226: Add `ViewInterface::getContext()` method (@rustamwin) ## 8.0.0 February 16, 2023 diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 6d2a5baa4..4d634f689 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -75,7 +75,7 @@ public function withBasePath(string $basePath): static * corresponding supported file extensions. * * ```php - * $view = $view->withRenderers(['twig' => new \Yiisoft\View\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 From 430d2f19af36012e068faaad990075e252fafd1d Mon Sep 17 00:00:00 2001 From: Rustam Date: Wed, 12 Jul 2023 02:06:21 +0500 Subject: [PATCH 3/5] Split `Template` into separate PR --- CHANGELOG.md | 1 - README.md | 18 +++++------ docs/basic-functionality.md | 54 +++++++++++++++---------------- docs/use-in-web-environment.md | 42 ++++++++++++------------ src/TemplateRendererInterface.php | 6 ++-- src/ViewInterface.php | 2 +- src/ViewTrait.php | 4 +-- 7 files changed, 63 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3fa60ec2..e04a4d10c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,6 @@ - Bug #224: Fix signature of `CachedContent::cache()` (@vjik) - Bug #226: Fix `reset` config for referenced definitions (@rustamwin) - Chg #226: Adjust config to make `View` and `WebView` more configurable (@rustamwin) -- Enh #226: Add `ViewInterface::getContext()` method (@rustamwin) ## 8.0.0 February 16, 2023 diff --git a/README.md b/README.md index b2be81048..2d58fb194 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,7 @@ [![type-coverage](https://shepherd.dev/github/yiisoft/view/coverage.svg)](https://shepherd.dev/github/yiisoft/view) This library provides templates rendering abstraction supporting layout-view-subview hierarchy, custom renderers with -PHP-based as default and more. It is used in [Yii Framework](https://www.yiiframework.com/) but is supposed -to be usable separately. +PHP-based as default, and more. It's used in [Yii Framework](https://www.yiiframework.com/) but is usable separately. ## Requirements @@ -36,7 +35,7 @@ composer require yiisoft/view The package provides two use cases for managing view templates: - [Basic functionality](docs/basic-functionality.md) for use in any environment. -- Advanced functionality for [use in a WEB environment](docs/use-in-web-environment.md). +- Advanced functionality for [use in a web environment](docs/use-in-web-environment.md). ### State of `View` and `WebView` services @@ -59,7 +58,7 @@ data. - JS/CSS strings, - JS/CSS files. -The state of `View` and `WebView` is not cloned when the services are cloned. So when +The state of `View` and `WebView` isn't cloned when the services are cloned. So when using `with*()`, both new and old instances are sharing the same set of stateful mutable data. It allows, for example, to get `WebView` via type-hinting in a controller and change context path: @@ -72,7 +71,7 @@ final class BlogController { } ``` -... and then register CSS in a widget: +and then register CSS in a widget: ```php final class LastPosts extends Widget @@ -144,9 +143,10 @@ $view = $view->withClearedState(); ## Extensions -- [yiisoft/yii-view](https://github.com/yiisoft/yii-view) - a wrapper that is used in - [Yii Framework]((https://www.yiiframework.com/)). Adds additional functionality for a WEB - environment and compatibility with [PSR-7](https://www.php-fig.org/psr/psr-7) interfaces. +- [yiisoft/yii-view](https://github.com/yiisoft/yii-view) - a wrapper that's used in + [Yii Framework]((https://www.yiiframework.com/)). + Adds extra functionality for a web environment and compatibility + with [PSR-7](https://www.php-fig.org/psr/psr-7) interfaces. - [yiisoft/view-twig](https://github.com/yiisoft/view-twig) - an extension that provides a view renderer that will allow you to use the [Twig](https://twig.symfony.com) view template engine, instead of the default PHP renderer. @@ -179,7 +179,7 @@ The code is statically analyzed with [Psalm](https://psalm.dev/). To run static ## License -The Yii View Rendering Library is free software. It is released under the terms of the BSD License. +The Yii View Rendering Library is free software. It's released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information. Maintained by [Yii Software](https://www.yiiframework.com/). diff --git a/docs/basic-functionality.md b/docs/basic-functionality.md index e05ab4749..495ca8fdf 100644 --- a/docs/basic-functionality.md +++ b/docs/basic-functionality.md @@ -1,9 +1,9 @@ # Basic functionality The package provides a `Yiisoft\View\View` class with basic functionality for managing views, and -a `Yiisoft\View\WebView` class with advanced functionality for use in a WEB environment. This guide applies to both +a `Yiisoft\View\WebView` class with advanced functionality for use in a web environment. This guide applies to both classes, but examples will be provided using the `Yiisoft\View\View`. For advanced examples with -`Yiisoft\View\WebView` functionality, see the "[Use in the WEB environment](use-in-web-environment.md)" guide. +`Yiisoft\View\WebView` functionality, see the "[Use in the web environment](use-in-web-environment.md)" guide. To create a `Yiisoft\View\View` class, you must specify two mandatory parameters: @@ -43,15 +43,15 @@ Posts: Within a view, you can access `$this` which refers to the `Yiisoft\View\View` managing and rendering current view template. Besides `$this`, there may be other variables in a view, such as `$posts` in the above example. -These variables represent the data that is passed as parameters when rendering the view. Note that ` Tip: The predefined variables are listed in a comment block at beginning of a view so that they -> can be recognized by IDEs. It is also a good way of documenting your views. +> Tip: The predefined variables are listed in a comment block at the beginning of a view so that they +> can be recognized by IDEs. It's also a good way of documenting your views. ## Rendering -To render the file shown above, two methods are provided: `render()` and `renderFile()`. +To render the file shown above, there are two methods: `render()` and `renderFile()`. The `renderFile()` method accepts a full absolute path of the view file to be rendered, and an array of parameters (name-value pairs) that will be available in the view template: @@ -75,9 +75,9 @@ Instead of an absolute file path, it accepts a name of a view in one of the foll - A name of a view starting with a slash (for example, `/blog/posts`). It will be prepended with the base path that was passed to `Yiisoft\View\View` constructor. For example, `/blog/posts` will be resolved into `/path/to/views/blog/posts.php`. -- A name of a view without the starting slash (e.g. `blog/posts`). The corresponding view file will be looked for +- A name of a view without the starting slash (such as `blog/posts`). The corresponding view file will be looked for in the context (instance of `Yiisoft\View\ViewContextInterface`) set via `$view->withContext()`. If the - context instance was not set, it will be looked for under the directory containing the view currently being + context instance wasn't set, it will be looked for under the directory containing the view currently being rendered. The view name may omit a file extension. In this case, `.php` will be used as the extension. @@ -119,11 +119,11 @@ and use that implementation via `$view->withRenderers()` method. ```php $view = $view->withRenderers([ 'tpl' => new MyCustomViewRenderer(), - 'twig' => new \Yiisoft\Yii\Twig\ViewRenderer($environment), + 'twig' => new \Yiisoft\View\Twig\TemplateRenderer($environment), ]); ``` -During rendering, the file extension will be analyzed and if the array key matches the file extension, +During rendering, the file extension will be analyzed, and if the array key matches the file extension, the corresponding renderer will be applied. ## Theming @@ -199,7 +199,7 @@ $view = $view->withSourceLocale('es'); $view->setLocale('fr'); ``` -In order to use multiple locales it is necessary to create subdirectories at directory level matching template files +To use multiple locales, it's necessary to create subdirectories at directory level matching template files of the view. For example, if there is a view `/path/to/views/blog/posts.php` and we translate it into Russian, create a subdirectory `ru-RU` or `ru`. In this subdirectory, create a file for the Russian locale: `/path/to/views/blog/ru/posts.php`. @@ -231,10 +231,10 @@ $view->localize($file, 'ru', 'ru'); File choice is based on the specified locale code. A file with the same name will be looked for under the subdirectory whose name is the same as the locale code. For example, given the file `/path/to/views/blog/posts.php` and the locale code `ru-RU`, the localized file will be looked -for as `/path/to/views/blog/ru-RU/posts.php`. If the file is not found, it will try a fallback -with just a language code that is `ru` i.e. `/path/to/views/blog/ru/posts.php`. +for as `/path/to/views/blog/ru-RU/posts.php`. If the file isn't found, it will try a fallback +with just a language code that's `ru` i.e. `/path/to/views/blog/ru/posts.php`. -> If the target file is not found, the original file will be returned. +> If the target file isn't found, the original file will be returned. > If the target and the source locale codes are the same, the original file will be returned. ## Sharing data among views @@ -254,7 +254,7 @@ $this->setBlock('block-id-1', '...content of block1...'); $this->setBlock('block-id-2', '...content of block2...'); ``` -Then, display the blocks if there are any, or the default content if the block is not defined: +Then, display the blocks if there are any, or the default content if the block isn't defined: ```php get(); -// If the content is not in the cache, then we will generate it and add it to the cache +// If the content isn't in the cache, then we will generate it and add it to the cache if ($content === null) { // Generating content $content = $view->render('view/name'); @@ -369,21 +369,21 @@ echo $content; ``` In addition to the content, the `Yiisoft\View\Cache\CachedContent::cache()` method -accepts three additional optional arguments: +accepts three extra optional arguments: - `$ttl (int)` - The TTL of the cached content. Default is `60`. - `$dependency (Yiisoft\Cache\Dependency\Dependency|null)` - The dependency of the cached content. Default is `null`. -- `$beta (float)` - The value for calculating the range that is used for "Probably early expiration". Default is `1.0`. +- `$beta (float)` - The value for calculating the range that's used for "Probably early expiration". Default is `1.0`. For more information about caching and cache options, see the documentation of the [yiisoft/cache package](https://github.com/yiisoft/cache). ### Dynamic Content -When caching content, you may encounter the situation where a large fragment of content is relatively +When caching content, you may meet the situation where a large fragment of content is relatively static except one or a few places. For example, a page header may display a main menu bar together with a name of the current user. Another problem is that the content being cached may contain PHP code that must -be executed for every request. Both problems can be solved by using the `Yiisoft\View\Cache\DynamicContent` class. +be executed for every request. You can solve both problems by using the `Yiisoft\View\Cache\DynamicContent` class. ```php /** @@ -409,7 +409,7 @@ $cachedContent = new CachedContent('cache-id', $cache, [$dynamicContent]); // Trying to get content from the cache $content = $cachedContent->get(); -// If the content is not in the cache, then we will generate it and add it to the cache +// If the content isn't in the cache, then we will generate it and add it to the cache if ($content === null) { // Generating content // In the view, we call `$dynamicContent->placeholder()` @@ -422,7 +422,7 @@ if ($content === null) { echo $content; ``` -A dynamic content means a fragment of output that should not be cached even if it is enclosed within a fragment cache. +A dynamic content means a fragment of output that shouldn't be cached even if it's enclosed within a fragment cache. You may call `$dynamicContent->placeholder()` within a cached fragment to insert dynamic content at the desired place of the view, like the following: @@ -445,7 +445,7 @@ Content to be cached ... ``` -For caching content fragments, it is much more convenient to use dynamic content using the +For caching content fragments, it's much more convenient to use dynamic content using the `Yiisoft\Yii\Widgets\FragmentCache` widget from the [yiisoft/yii-widgets](https://github.com/yiisoft/yii-widgets) package: @@ -475,7 +475,7 @@ FragmentCache::end(); ### Variations -Content being cached may be varied according to some parameters. For example, for a Web application supporting +Content being cached may be varied according to some parameters. For example, for a web application supporting multiple locales, the same piece of view code may generate the content in different locales. Therefore, you may want to make the cached content varied according to the current application locale. diff --git a/docs/use-in-web-environment.md b/docs/use-in-web-environment.md index 022e89aed..c2d1a0254 100644 --- a/docs/use-in-web-environment.md +++ b/docs/use-in-web-environment.md @@ -1,6 +1,6 @@ -# Use in the WEB environment +# Use in the web environment -This guide describes additional functionality of the `Yiisoft\View\WebView` class intended for use in a WEB environment. +This guide describes extra functionality of the `Yiisoft\View\WebView` class intended for use in a web environment. Please read the "[Basic Functionality](basic-functionality.md)" guide first. To create `Yiisoft\View\WebView` class, you must specify two mandatory parameters: @@ -19,7 +19,7 @@ $view = new \Yiisoft\View\WebView( ## Creating view templates In an example below, a view is a simple PHP script that outputs information about posts in a loop. -Note that for example purpose, we have greatly simplified the template code. In practice, you may +Note that for example purpose, we've greatly simplified the template code. In practice, you may want to add more content to it, such as `` tags, main menu, etc. ```php @@ -90,7 +90,7 @@ Then in the view, make sure you have the following code in the `` section: ## Rendering In addition to the `render()` and `renderFile()` methods, two `renderAjax()` and `renderAjaxString()` -methods have been added for the WEB environment to render AJAX requests. +methods have been added for the web environment to render AJAX requests. The `renderAjax()` method is like `render()` except that it will surround the view being rendered with the calls of `beginPage()`, `head()`, `beginBody()`, `endBody()` and `endPage()`. By doing so, the method can @@ -132,7 +132,7 @@ where you call `Yiisoft\View\WebView::head()` in the view template: ``` -Note that if you call `registerMeta()` or `registerMetaTag()` multiple times, it will register multiple meta tags, +Note that if you call `registerMeta()` or `registerMetaTag()` many times, it will register many meta tags, regardless whether the meta tags are the same or not. To make sure there is only a single instance of a meta tag of the same type, you can specify a key as a second parameter when calling the methods. For example, the following code registers two `description` meta tags. However, only the second one will be rendered. @@ -153,7 +153,7 @@ $view->registerMetaTag( ## Registering link tags -Like [meta tags](#registering-meta-tags), link tags are useful in many cases, such as customizing favicon, pointing to +Like [meta tags](#registering-meta-tags), link tags are useful often, such as customizing favicon, pointing to RSS feed or delegating OpenID to another server. You can work with link tags in a similar way as meta tags by using `registerLink()` or `registerLinkTag()`. For example, in a content view, you can register a link tag like follows: @@ -180,7 +180,7 @@ The code above will result in: ``` -You can use second parameter to specify position at which the link tag should be inserted in a page. +You can use the second parameter to specify the position at which the link tag should be inserted in a page. Default is `Yiisoft\View\WebView::POSITION_HEAD`. Like registering meta tags, you can specify a key to avoid creating duplicate link tags. In the `registerLink()` and `registerLinkTag()` the key is specified as a third parameter. @@ -197,7 +197,7 @@ use Yiisoft\View\WebView; $view->registerCssFile('/path/to/style.css'); // Result: -// With an URL: +// With a URL: $view->registerCssFile('https//example.com/style.css'); // Result: @@ -209,9 +209,9 @@ $view->registerCssFile('/path/to/style.css', WebView::POSITION_HEAD, [ 'media' => 'print', ]); -// With an specific of the identifying key. +// With a specific of the identifying key. $view->registerCssFile('/path/to/style.css', WebView::POSITION_HEAD, [], 'file-key'); -// If the key is not specified, the URL of the CSS file will be used instead. +// If the key isn't specified, the URL of the CSS file will be used instead. ``` The registration of the CSS code block is as follows: @@ -246,7 +246,7 @@ The code above will result in: ``` For all methods, the `POSITION_HEAD` position is used by default, and the last argument specifies the key that -identifies this block of CSS code. If the key is not specified, the `md5()` hash of the CSS code block +identifies this block of CSS code. If the key isn't specified, the `md5()` hash of the CSS code block will be used instead. ## Registering JavaScript @@ -260,7 +260,7 @@ use Yiisoft\View\WebView; $view->registerJsFile('/path/to/script.js'); // Result: -// With an URL: +// With a URL: $view->registerJsFile('https//example.com/script.js'); // Result: @@ -273,9 +273,9 @@ $view->registerJsFile('/path/to/script.js', WebView::POSITION_END, [ ]); // Result: -// With an specific of the identifying key. +// With a specific of the identifying key. $view->registerJsFile('/path/to/script.js', WebView::POSITION_END, [], 'file-key'); -// If the key is not specified, the URL of the JavaScript file will be used instead. +// If the key isn't specified, the URL of the JavaScript file will be used instead. ``` The registration of the JavaScript code block is as follows: @@ -302,7 +302,7 @@ The code above will result in: ``` For both methods, the `POSITION_END` position is used by default, and the last argument specifies the key -that identifies this block of JavaScript code. If the key is not specified, the `md5()` hash of the +that identifies this block of JavaScript code. If the key isn't specified, the `md5()` hash of the JavaScript code block will be used instead. Additionally, a separate method is provided for registering JavaScript variables: @@ -339,7 +339,7 @@ The code above will result in: ``` -The name of variable will be used as key, preventing duplicated variable names. +The name of variable will be used as a key, preventing duplicated variable names. ## Use with an asset manager @@ -361,9 +361,9 @@ $this->addJsStrings($assetManager->getJsStrings()); $this->addJsVars($assetManager->getJsVars()); ``` -These methods process the CSS and JavaScript configuration created by the asset manager and converts it into HTML code. +These methods process the CSS and JavaScript configuration created by the asset manager and convert it into HTML code. -## WebView Events +## WebView events The `Yiisoft\View\WebView` class triggers several events during the view rendering process. Events are classes: @@ -382,7 +382,7 @@ which is specified in the constructor when the `Yiisoft\View\WebView` instance i ## Security -When creating views that generate HTML pages, it is important that you properly encode and/or +When creating views that generate HTML pages, it's important that you properly encode and/or filter all the data when outputted. Otherwise, your application may be subject to [cross-site scripting](http://en.wikipedia.org/wiki/Cross-site_scripting) attacks. @@ -408,5 +408,5 @@ use Yiisoft\Html\Html; ``` If you need to securely display HTML content, you can use content filtering tools such as -[HTML Purifier](https://github.com/ezyang/htmlpurifier). A drawback is that it is not very -performant so consider caching the result. +[HTML Purifier](https://github.com/ezyang/htmlpurifier). A drawback is that it isn't very +performant, so consider caching the result. diff --git a/src/TemplateRendererInterface.php b/src/TemplateRendererInterface.php index 9b17b85b9..620604fe7 100644 --- a/src/TemplateRendererInterface.php +++ b/src/TemplateRendererInterface.php @@ -12,11 +12,11 @@ interface TemplateRendererInterface /** * Renders a template file. * - * 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. + * {@see View} and {@see WebView} invoke this method whenever it tries to render a view. + * The classes must implement this method to render the given template file. * * @param ViewInterface $view The view instance used for rendering the file. - * @param string $template The template file. + * @param string $template The template file to render. * @param array $parameters The parameters to be passed to the view file. * * @return string The rendering result. diff --git a/src/ViewInterface.php b/src/ViewInterface.php index 22bdeeca7..fa380f9af 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\ViewRenderer($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 4d634f689..039c0fc85 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -405,7 +405,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. @@ -415,7 +415,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. */ From 50ad081c58fe35c0a502dd7fd35578f9dd22aaaa Mon Sep 17 00:00:00 2001 From: Rustam Date: Wed, 12 Jul 2023 02:10:23 +0500 Subject: [PATCH 4/5] Minor fix --- src/ViewInterface.php | 7 ------- src/ViewTrait.php | 10 +--------- tests/ViewTest.php | 9 --------- 3 files changed, 1 insertion(+), 25 deletions(-) diff --git a/src/ViewInterface.php b/src/ViewInterface.php index fa380f9af..bb2c386ee 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -100,13 +100,6 @@ public function withLocale(string $locale): static; */ public function getBasePath(): string; - /** - * Gets the context instance, or `null` if no context has been set. - * - * @return ViewContextInterface|null The context instance, or `null` if no context has been set. - */ - public function getContext(): ?ViewContextInterface; - /** * Gets the default view file extension. * diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 039c0fc85..573dba27d 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -75,7 +75,7 @@ public function withBasePath(string $basePath): static * corresponding supported file extensions. * * ```php - * $view = $view->withRenderers(['twig' => new \Yiisoft\View\Twig\TemplateRenderer($environment)]); + * $view = $view->withRenderers(['twig' => new \Yiisoft\View\Twig\ViewRenderer($environment)]); * ``` * * If no renderer is available for the given view file, the view file will be treated as a normal PHP @@ -184,14 +184,6 @@ public function getBasePath(): string return $this->basePath; } - /** - * Gets the context instance, or `null` if no context has been set. - */ - public function getContext(): ?ViewContextInterface - { - return $this->context; - } - /** * Gets the default view file extension. * diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 472363ec6..9834fdf73 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -197,15 +197,6 @@ public function testRenderWithoutFileExtension(): void ->render('file')); } - public function testContext(): void - { - $view = $this->createViewWithBasePath($this->tempDirectory); - $context = $this->createContext($this->tempDirectory); - $view = $view->withContext($context); - - $this->assertSame($context, $view->getContext()); - } - public function testLocalize(): void { $view = $this->createViewWithBasePath($this->tempDirectory); From 86661a7f135b20d42f2f47b8b2be3585a7006b7f Mon Sep 17 00:00:00 2001 From: Rustam Mamadaminov Date: Wed, 12 Jul 2023 13:48:25 +0500 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 2 +- config/di-web.php | 4 ++-- config/di.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e04a4d10c..4adcaa121 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - Bug #224: Fix signature of `CachedContent::cache()` (@vjik) - Bug #226: Fix `reset` config for referenced definitions (@rustamwin) -- Chg #226: Adjust config to make `View` and `WebView` more configurable (@rustamwin) +- Enh #226: Adjust config to make `View` and `WebView` more configurable (@rustamwin) ## 8.0.0 February 16, 2023 diff --git a/config/di-web.php b/config/di-web.php index a135b5d23..0e11d9669 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -32,8 +32,8 @@ static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath']) ), ], - 'setParameters()' => ['parameters' => $params['yiisoft/view']['parameters']], - 'withRenderers()' => ['renderers' => $params['yiisoft/view']['renderers']], + 'setParameters()' => [$params['yiisoft/view']['parameters']], + 'withRenderers()' => [$params['yiisoft/view']['renderers']], 'withDefaultExtension()' => [$params['yiisoft/view']['defaultExtension']], 'reset' => function (ContainerInterface $container) use ($params) { /** @var WebView $this */ diff --git a/config/di.php b/config/di.php index 90e6a1ae7..887a17891 100644 --- a/config/di.php +++ b/config/di.php @@ -17,8 +17,8 @@ static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath']) ), ], - 'setParameters()' => ['parameters' => $params['yiisoft/view']['parameters']], - 'withRenderers()' => ['renderers' => $params['yiisoft/view']['renderers']], + 'setParameters()' => [$params['yiisoft/view']['parameters']], + 'withRenderers()' => [$params['yiisoft/view']['renderers']], 'withDefaultExtension()' => [$params['yiisoft/view']['defaultExtension']], 'reset' => function (ContainerInterface $container) use ($params) { /** @var View $this */