Skip to content

Commit 2b96c79

Browse files
authored
Use only full path for layout (#115)
1 parent b380a45 commit 2b96c79

File tree

5 files changed

+36
-61
lines changed

5 files changed

+36
-61
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Yii View Extension Change Log
22

3-
## 6.1.2 under development
3+
## 7.0.0 under development
44

5-
- no changes in this release.
5+
- Chg #115: Only a full path can now be used as a layout (@vjik)
66

77
## 6.1.1 June 06, 2024
88

UPGRADE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Upgrading Instructions for Yii View Extension
2+
3+
This file contains the upgrade notes. These notes highlight changes that could break your
4+
application when you upgrade the package from one version to another.
5+
6+
> **Important!** The following upgrading instructions are cumulative. That is, if you want
7+
> to upgrade from version A to version C and there is version B between A and C, you need
8+
> to following the instructions for both A and B.
9+
10+
## Upgrade from 6.x
11+
12+
- Change layout value that passed to `ViewRenderer` constructor and `withLayout()` method to full path.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"yiisoft/friendly-exception": "^1.0",
3636
"yiisoft/html": "^2.5|^3.0",
3737
"yiisoft/strings": "^2.0",
38-
"yiisoft/view": "^6.0|^7.0|^8.0"
38+
"yiisoft/view": "^9.0"
3939
},
4040
"require-dev": {
4141
"httpsoft/http-message": "^1.0",

src/ViewRenderer.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use function is_array;
2828
use function is_int;
2929
use function is_string;
30-
use function pathinfo;
3130
use function preg_match;
3231
use function rtrim;
3332
use function sprintf;
@@ -61,8 +60,8 @@ final class ViewRenderer implements ViewContextInterface
6160
* @param Aliases $aliases The aliases instance.
6261
* @param WebView $view The web view instance.
6362
* @param string|null $viewPath The full path to the directory of views or its alias.
64-
* @param string|null $layout The layout name (e.g. "layout/main") to be applied to views.
65-
* If null, the layout will not be applied.
63+
* @param string|null $layout The full path to the layout file to be applied to views. If null, the layout will
64+
* not be applied.
6665
* @param array $injections The injection instances or class names.
6766
*
6867
* @psalm-param array<object|string> $injections
@@ -241,8 +240,8 @@ public function withViewPath(string $viewPath): self
241240
/**
242241
* Returns a new instance with the specified layout.
243242
*
244-
* @param string|null $layout The layout name (e.g. "layout/main") to be applied to views.
245-
* If null, the layout will not be applied.
243+
* @param string|null $layout The full path to the layout file to be applied to views. If null, the layout will
244+
* not be applied.
246245
*/
247246
public function withLayout(?string $layout): self
248247
{
@@ -331,7 +330,7 @@ private function renderProxy(
331330
return $content;
332331
}
333332

334-
$layout = $this->findLayoutFile($this->layout, $currentView);
333+
$layout = $this->aliases->get($this->layout);
335334

336335
$layoutParameters = array_filter(
337336
$injectLayoutParameters,
@@ -511,30 +510,6 @@ private function injectLinkTags(array $tags, WebView $view): void
511510
}
512511
}
513512

514-
/**
515-
* Finds a layout file based on the given file path or alias.
516-
*
517-
* @param string $file The file path or alias.
518-
*
519-
* @return string The path to the file with the file extension.
520-
*/
521-
private function findLayoutFile(string $file, WebView $view): string
522-
{
523-
$file = $this->aliases->get($file);
524-
525-
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
526-
return $file;
527-
}
528-
529-
$layoutFile = $file . '.' . $view->getDefaultExtension();
530-
531-
if ($view->getDefaultExtension() !== 'php' && !is_file($layoutFile)) {
532-
$layoutFile = $file . '.php';
533-
}
534-
535-
return $layoutFile;
536-
}
537-
538513
/**
539514
* Returns a controller name based on controller instance.
540515
*

tests/ViewRendererTest.php

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ final class ViewRendererTest extends TestCase
3939
{
4040
use TestTrait;
4141

42-
/**
43-
* @dataProvider extensionsProvider
44-
*/
45-
public function testRenderAndRenderAsString(string $extension): void
42+
public function testRenderAndRenderAsString(): void
4643
{
4744
$renderer = $this
48-
->getRenderer($extension)
49-
->withLayout('@views/with-injection/layout')
45+
->getRenderer()
46+
->withLayout('@views/with-injection/layout.php')
5047
->withControllerName('with-injection')
5148
->withInjections(new TestInjection());
5249

@@ -77,14 +74,6 @@ public function testRenderAndRenderAsString(string $extension): void
7774
);
7875
}
7976

80-
public function extensionsProvider(): array
81-
{
82-
return [
83-
['php'],
84-
['tpl'],
85-
];
86-
}
87-
8877
public function testRenderWithAbsoluteLayoutPath(): void
8978
{
9079
$renderer = $this
@@ -371,7 +360,7 @@ public function testLayoutParametersInjectionsToNestedViews(): void
371360
{
372361
$renderer = $this
373362
->getRenderer()
374-
->withLayout('@views/nested-layout/layout')
363+
->withLayout('@views/nested-layout/layout.php')
375364
->withInjections(new TitleInjection());
376365

377366
$response = $renderer->render('empty');
@@ -386,7 +375,7 @@ public function testChangeInjectionsAfterCreateProxyAndBeforeRender(): void
386375
{
387376
$renderer = $this
388377
->getRenderer()
389-
->withLayout('@views/with-injection/layout')
378+
->withLayout('@views/with-injection/layout.php')
390379
->withControllerName('with-injection')
391380
->withInjections(new TestInjection());
392381

@@ -419,7 +408,7 @@ public function testPassingCommonParametersFromContentToLayout(): void
419408
$renderer = $this
420409
->getRenderer()
421410
->withViewPath('@views/passing-parameters-to-layout')
422-
->withLayout('@views/passing-parameters-to-layout/layout');
411+
->withLayout('@views/passing-parameters-to-layout/layout.php');
423412

424413
$response = $renderer->render('content', [
425414
'h1' => 'HELLO',
@@ -434,7 +423,7 @@ public function testCommonParametersOverrideLayout(): void
434423
{
435424
$renderer = $this
436425
->getRenderer()
437-
->withLayout('@views/override-layout-parameters/layout')
426+
->withLayout('@views/override-layout-parameters/layout.php')
438427
->withInjections(new CommonParametersInjection());
439428

440429
$response = $renderer->render('empty');
@@ -449,7 +438,7 @@ public function testInRenderSetParametersOverrideLayout(): void
449438
$renderer = $this
450439
->getRenderer()
451440
->withViewPath('@views/override-layout-parameters')
452-
->withLayout('@views/override-layout-parameters/layout')
441+
->withLayout('@views/override-layout-parameters/layout.php')
453442
->withInjections(new CommonParametersInjection(), new LayoutParametersInjection());
454443

455444
$response = $renderer->render('content');
@@ -463,7 +452,7 @@ public function testRenderParametersNotOverrideLayout(): void
463452
{
464453
$renderer = $this
465454
->getRenderer()
466-
->withLayout('@views/override-layout-parameters/layout')
455+
->withLayout('@views/override-layout-parameters/layout.php')
467456
->withInjections(new LayoutParametersInjection());
468457

469458
$response = $renderer->render('empty', ['seoTitle' => 'custom']);
@@ -494,7 +483,7 @@ public function testLazyLoadingInjection(): void
494483

495484
$renderer = $this
496485
->getRenderer(injectionContainer: new InjectionContainer($container))
497-
->withLayout('@views/simple/layout')
486+
->withLayout('@views/simple/layout.php')
498487
->withControllerName('simple')
499488
->withInjections(CharsetInjection::class);
500489

@@ -515,7 +504,7 @@ public function testLazyLoadingInjectionWithoutContainer(): void
515504
{
516505
$renderer = $this
517506
->getRenderer()
518-
->withLayout('@views/simple/layout')
507+
->withLayout('@views/simple/layout.php')
519508
->withControllerName('simple')
520509
->withInjections(CharsetInjection::class);
521510

@@ -528,14 +517,14 @@ public function testLayoutSpecificInjections(): void
528517
{
529518
$renderer = $this
530519
->getRenderer()
531-
->withLayout('@views/nested-layout/layout')
520+
->withLayout('@views/nested-layout/layout.php')
532521
->withInjections(
533522
new LayoutSpecificInjections(
534-
'@views/nested-layout/layout',
523+
'@views/nested-layout/layout.php',
535524
new TitleInjection(),
536525
),
537526
new LayoutSpecificInjections(
538-
'@views/layout',
527+
'@views/layout.php',
539528
new TestInjection(),
540529
),
541530
new class () implements MetaTagsInjectionInterface {
@@ -568,15 +557,14 @@ public function testImmutability(): void
568557
}
569558

570559
private function getRenderer(
571-
string $defaultExtension = 'php',
572560
?InjectionContainerInterface $injectionContainer = null,
573561
): ViewRenderer {
574562
return new ViewRenderer(
575563
new DataResponseFactory(new ResponseFactory(), new StreamFactory()),
576564
new Aliases(['@views' => $this->getViewsDir()]),
577-
(new WebView('@views', new SimpleEventDispatcher()))->withDefaultExtension($defaultExtension),
565+
new WebView('@views', new SimpleEventDispatcher()),
578566
'@views',
579-
'@views/layout',
567+
'@views/layout.php',
580568
injectionContainer: $injectionContainer
581569
);
582570
}

0 commit comments

Comments
 (0)