Skip to content

Commit

Permalink
Fix bug & add changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Jul 13, 2023
1 parent af46cd1 commit adce5b4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions src/ViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 16 additions & 13 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
34 changes: 20 additions & 14 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
);
}

Expand Down

0 comments on commit adce5b4

Please sign in to comment.