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

Make fallback extension configurable #232

Merged
merged 5 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
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 @@
/**
* 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 @@
* 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; }

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 @@ -430,7 +453,7 @@
}

$output = '';
$this->viewFiles[] = [

Check warning on line 456 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ throw new ViewNotFoundException("The view file \"{$viewFile}\" does not exist."); } $output = ''; - $this->viewFiles[] = ['resolved' => $viewFile, 'requested' => $requestedFile]; + $this->viewFiles[] = ['requested' => $requestedFile]; if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer();

Check warning on line 456 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ throw new ViewNotFoundException("The view file \"{$viewFile}\" does not exist."); } $output = ''; - $this->viewFiles[] = ['resolved' => $viewFile, 'requested' => $requestedFile]; + $this->viewFiles[] = ['requested' => $requestedFile]; if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer();
'resolved' => $viewFile,
'requested' => $requestedFile,
];
Expand Down Expand Up @@ -600,17 +623,11 @@
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
Loading