Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve config #226

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
- Enh #226: Add `ViewInterface::getContext()` method (@rustamwin)

## 8.0.0 February 16, 2023

Expand Down
18 changes: 13 additions & 5 deletions config/di-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']],
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
'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);
},
],
];
18 changes: 13 additions & 5 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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']],
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
'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);
},
],
];
2 changes: 2 additions & 0 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
'basePath' => '',
'baseUrl' => '',
],
'renderers' => [],
'defaultExtension' => 'php',
],
];
7 changes: 7 additions & 0 deletions src/ViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Gets the default view file extension.
*
Expand Down
10 changes: 9 additions & 1 deletion src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\TemplateRenderer($environment)]);
* ```
*
* If no renderer is available for the given view file, the view file will be treated as a normal PHP
Expand Down Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down