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 1 commit
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
2 changes: 2 additions & 0 deletions 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
20 changes: 17 additions & 3 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
private ?ViewContextInterface $context = null;
private string $placeholderSignature;
private string $sourceLocale = 'en';
private string $defaultExtension = 'php';
private string $defaultExtension = self::PHP_EXTENSION;
private string $fallbackExtension = self::PHP_EXTENSION;

/**
* @var array A list of available renderers indexed by their corresponding
Expand Down Expand Up @@ -105,7 +106,7 @@
/**
* 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.
*/
public function withDefaultExtension(string $defaultExtension): static
Expand All @@ -115,6 +116,19 @@
return $new;
}

/**
* 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->fallbackExtension = $fallbackExtension;
return $new;
}

/**
* Returns a new instance with the specified view context instance.
*
Expand Down Expand Up @@ -430,14 +444,14 @@
}

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

Check warning on line 447 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 447 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,
];

if ($this->beforeRender($viewFile, $parameters)) {
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
$renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer();

Check warning on line 454 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ $this->viewFiles[] = ['resolved' => $viewFile, 'requested' => $requestedFile]; if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); - $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer(); + $renderer = new PhpTemplateRenderer() ?? $this->renderers[$ext]; $output = $renderer->render($this, $viewFile, $parameters); $output = $this->afterRender($viewFile, $parameters, $output); }

Check warning on line 454 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ $this->viewFiles[] = ['resolved' => $viewFile, 'requested' => $requestedFile]; if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); - $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer(); + $renderer = new PhpTemplateRenderer() ?? $this->renderers[$ext]; $output = $renderer->render($this, $viewFile, $parameters); $output = $this->afterRender($viewFile, $parameters, $output); }
$output = $renderer->render($this, $viewFile, $parameters);
$output = $this->afterRender($viewFile, $parameters, $output);
}
Expand Down Expand Up @@ -607,7 +621,7 @@
$path = $file . '.' . $this->defaultExtension;

if ($this->defaultExtension !== 'php' && !is_file($path)) {
$path = $file . '.php';
$path = $file . '.' . $this->fallbackExtension;
}

return $path;
Expand Down
5 changes: 5 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public function testRenderWithoutFileExtension(): void
$this->assertSame('Test', $view
->withDefaultExtension('txt')
->render('file'));
$this->assertSame(
'Test',
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
$view->withDefaultExtension('phpt')->withFallbackExtension('tpl')->render('file')
);
}

public function testLocalize(): void
Expand Down Expand Up @@ -545,6 +549,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