From adce5b4b9824c8f22928f792f6e3f8997d39c10b Mon Sep 17 00:00:00 2001 From: Rustam Date: Thu, 13 Jul 2023 11:15:32 +0500 Subject: [PATCH] Fix bug & add changelog --- CHANGELOG.md | 4 ++++ src/ViewInterface.php | 18 ++++++++++++++++++ src/ViewTrait.php | 29 ++++++++++++++++------------- tests/ViewTest.php | 34 ++++++++++++++++++++-------------- 4 files changed, 58 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32ac3cb36..bc6eff55a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## 8.0.1 under development - Bug #224: Fix signature of `CachedContent::cache()` (@vjik) +- Enh #232: Make fallback extension configurable (@rustamwin) +- Chg #232: Deprecate `ViewInterface::withDefaultExtension()` and `ViewInterface::getDefaultExtension()` in favor of + `ViewInterface::withFallbackExtension()` and `ViewInterface::getFallbackExtension()` (@rustamwin) +- Bug #232: Fix render templates that contain dots in their name (@rustamwin) ## 8.0.0 February 16, 2023 diff --git a/src/ViewInterface.php b/src/ViewInterface.php index f3feeed0a..0740cd795 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -52,9 +52,18 @@ public function withSourceLocale(string $locale): static; * * @param string $defaultExtension The default view file extension. Default is "php". * This will be appended to view file names if they don't have file extensions. + * @deprecated Since 8.0.1 and will be removed in the next major version. Use {@see withFallbackExtension()} instead. */ public function withDefaultExtension(string $defaultExtension): static; + /** + * Returns a new instance with the specified fallback view file extension. + * + * @param string $fallbackExtension The fallback view file extension. Default is {@see PHP_EXTENSION}. + * This will be appended to view file names if they don't exist. + */ + public function withFallbackExtension(string $fallbackExtension): static; + /** * Returns a new instance with the specified view context instance. * @@ -106,9 +115,18 @@ public function getBasePath(): string; * Gets the default view file extension. * * @return string The default view file extension. + * @deprecated Since 8.0.1 and will be removed in the next major version. Use {@see withFallbackExtension()} instead. */ public function getDefaultExtension(): string; + + /** + * Gets the fallback view file extension. + * + * @return string The fallback view file extension. + */ + public function getFallbackExtension(): string; + /** * Gets the theme instance, or `null` if no theme has been set. * diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 8942de128..1924a1b5f 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -39,7 +39,6 @@ trait ViewTrait private ?ViewContextInterface $context = null; private string $placeholderSignature; private string $sourceLocale = 'en'; - private string $defaultExtension = self::PHP_EXTENSION; private string $fallbackExtension = self::PHP_EXTENSION; /** @@ -108,12 +107,11 @@ public function withSourceLocale(string $locale): static * * @param string $defaultExtension The default view file extension. Default is {@see ViewInterface::PHP_EXTENSION}. * This will be appended to view file names if they don't have file extensions. + * @deprecated Since 8.0.1 and will be removed in the next major version. Use {@see withFallbackExtension()} instead. */ public function withDefaultExtension(string $defaultExtension): static { - $new = clone $this; - $new->defaultExtension = $defaultExtension; - return $new; + return $this->withFallbackExtension($defaultExtension); } /** @@ -202,10 +200,21 @@ public function getBasePath(): string * Gets the default view file extension. * * @return string The default view file extension. + * @deprecated Since 8.0.1 and will be removed in the next major version. Use {@see getFallbackExtension()} instead. */ public function getDefaultExtension(): string { - return $this->defaultExtension; + return $this->getFallbackExtension(); + } + + /** + * Gets the fallback view file extension. + * + * @return string The fallback view file extension. + */ + public function getFallbackExtension(): string + { + return $this->fallbackExtension; } /** @@ -614,17 +623,11 @@ private function findTemplateFile(string $view): string throw new RuntimeException("Unable to resolve view file for view \"$view\": no active view context."); } - if (pathinfo($file, PATHINFO_EXTENSION) !== '') { + if (pathinfo($file, PATHINFO_EXTENSION) !== '' && is_file($file)) { return $file; } - $path = $file . '.' . $this->defaultExtension; - - if ($this->defaultExtension !== 'php' && !is_file($path)) { - $path = $file . '.' . $this->fallbackExtension; - } - - return $path; + return $file . '.' . $this->fallbackExtension; } /** diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 48da1fe14..c70dc94da 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -19,7 +19,6 @@ use Yiisoft\View\Theme; use Yiisoft\View\View; use Yiisoft\View\ViewContextInterface; - use function crc32; use function dechex; use function file_put_contents; @@ -184,20 +183,27 @@ public function testRenderWithoutFileExtension(): void $view = $this ->createViewWithBasePath($this->tempDirectory) ->withContext($this->createContext($this->tempDirectory)); - file_put_contents("$this->tempDirectory/file.php", 'Test'); - file_put_contents("$this->tempDirectory/file.tpl", 'Test'); - file_put_contents("$this->tempDirectory/file.txt.php", 'Test'); - - $this->assertSame('Test', $view->render('file')); - $this->assertSame('Test', $view - ->withDefaultExtension('tpl') - ->render('file')); - $this->assertSame('Test', $view - ->withDefaultExtension('txt') - ->render('file')); + file_put_contents("$this->tempDirectory/file.php", 'Test php'); + file_put_contents("$this->tempDirectory/file.tpl", 'Test tpl'); + file_put_contents("$this->tempDirectory/file.phpt", 'Test phpt'); + file_put_contents("$this->tempDirectory/file.txt.twig", 'Test txt'); + + $this->assertSame('Test php', $view->render('file')); + $this->assertSame( + 'Test tpl', + $view->withDefaultExtension('tpl') + ->render('file') + ); + $this->assertSame( + 'Test txt', + $view->withDefaultExtension('twig') + ->render('file.txt') + ); $this->assertSame( - 'Test', - $view->withDefaultExtension('phpt')->withFallbackExtension('tpl')->render('file') + 'Test phpt', + $view->withDefaultExtension('twig') + ->withFallbackExtension('phpt') + ->render('file') ); }