Skip to content

Commit

Permalink
Fix #232: Make fallback extension configurable, deprecate `ViewInterf…
Browse files Browse the repository at this point in the history
…ace::withDefaultExtension()` and `ViewInterface::getDefaultExtension()` in favor of `withFallbackExtension()` and `getFallbackExtension()`, fix render templates that contain dots in their name
  • Loading branch information
rustamwin authored Jul 13, 2023
1 parent 6703eba commit 0086712
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- Bug #224: Fix signature of `CachedContent::cache()` (@vjik)
- Bug #226: Fix `reset` config for referenced definitions (@rustamwin)
- Enh #226: Adjust config to make `View` and `WebView` more configurable (@rustamwin)
- Enh #232: Make fallback extension configurable (@rustamwin)
- Chg #232: Deprecate `ViewInterface::withDefaultExtension()` and `ViewInterface::getDefaultExtension()` in favor of
`withFallbackExtension()` and `getFallbackExtension()` (@rustamwin)
- Bug #232: Fix render templates that contain dots in their name (@rustamwin)

## 8.0.0 February 16, 2023

Expand Down
6 changes: 5 additions & 1 deletion src/ViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
interface ViewInterface
{
public const PHP_EXTENSION = 'php';

/**
* Returns a new instance with specified base path to the view directory.
*
Expand Down Expand Up @@ -48,8 +50,9 @@ public function withSourceLocale(string $locale): static;
/**
* Returns a new instance with the specified default view file extension.
*
* @param string $defaultExtension The default view file extension. Default is "php".
* @param string $defaultExtension The default view file extension. Default is {@see 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.
*/
public function withDefaultExtension(string $defaultExtension): static;

Expand Down Expand Up @@ -104,6 +107,7 @@ 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.
*/
public function getDefaultExtension(): string;

Expand Down
41 changes: 29 additions & 12 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ trait ViewTrait
private ?ViewContextInterface $context = null;
private string $placeholderSignature;
private string $sourceLocale = 'en';
private string $defaultExtension = 'php';
private string $fallbackExtension = self::PHP_EXTENSION;

/**
* @var array A list of available renderers indexed by their corresponding
Expand Down Expand Up @@ -105,13 +105,25 @@ public function withSourceLocale(string $locale): static
/**
* Returns a new instance with the specified default view file extension.
*
* @param string $defaultExtension The default view file extension. Default is "php".
* @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
{
return $this->withFallbackExtension($defaultExtension);
}

/**
* Returns a new instance with the specified fallback view file extension.
*
* @param string $fallbackExtension The fallback view file extension. Default is {@see ViewInterface::PHP_EXTENSION}.
* This will be appended to view file names if they don't exist.
*/
public function withFallbackExtension(string $fallbackExtension): static
{
$new = clone $this;
$new->defaultExtension = $defaultExtension;
$new->fallbackExtension = $fallbackExtension;
return $new;
}

Expand Down Expand Up @@ -188,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

Check warning on line 215 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @return string The fallback view file extension. */ - public function getFallbackExtension() : string + protected function getFallbackExtension() : string { return $this->fallbackExtension; }
{
return $this->fallbackExtension;
}

/**
Expand Down Expand Up @@ -600,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 . '.php';
}

return $path;
return $file . '.' . $this->fallbackExtension;
}

/**
Expand Down
32 changes: 22 additions & 10 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,28 @@ 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');
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', $view->render('file'));
$this->assertSame('Test', $view
->withDefaultExtension('tpl')
->render('file'));
$this->assertSame('Test', $view
->withDefaultExtension('txt')
->render('file'));
$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 phpt',
$view->withDefaultExtension('twig')
->withFallbackExtension('phpt')
->render('file')
);
}

public function testLocalize(): void
Expand Down Expand Up @@ -545,6 +556,7 @@ public function testImmutability(): void
$this->assertNotSame($view, $view->withPlaceholderSalt(''));
$this->assertNotSame($view, $view->withClearedState());
$this->assertNotSame($view, $view->withLocale('es'));
$this->assertNotSame($view, $view->withFallbackExtension('tpl'));
}

private function createViewWithBasePath(string $basePath): View
Expand Down

0 comments on commit 0086712

Please sign in to comment.