Skip to content

Commit

Permalink
Fix #233: Support multiple fallback extensions (#234)
Browse files Browse the repository at this point in the history
* Support multiple fallback extensions

* Update src/ViewTrait.php

* Update ViewTrait.php

* Adjust changelog

* Update src/ViewTrait.php

Co-authored-by: Sergei Predvoditelev <[email protected]>

---------

Co-authored-by: Alexander Makarov <[email protected]>
Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2023
1 parent 0086712 commit 38e7351
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
- 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)
- Enh #232, #233: Make fallback extension configurable & support multiple fallbacks (@rustamwin)
- Chg #232: Deprecate `ViewInterface::withDefaultExtension()` and `ViewInterface::getDefaultExtension()` in favor of
`withFallbackExtension()` and `getFallbackExtension()` (@rustamwin)
`withFallbackExtension()` and `getFallbackExtensions()` (@rustamwin)
- Bug #232: Fix render templates that contain dots in their name (@rustamwin)

## 8.0.0 February 16, 2023
Expand Down
28 changes: 19 additions & 9 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ trait ViewTrait
private ?ViewContextInterface $context = null;
private string $placeholderSignature;
private string $sourceLocale = 'en';
private string $fallbackExtension = self::PHP_EXTENSION;
/**
* @var string[]
*/
private array $fallbackExtensions = [self::PHP_EXTENSION];

/**
* @var array A list of available renderers indexed by their corresponding
Expand Down Expand Up @@ -120,10 +123,10 @@ public function withDefaultExtension(string $defaultExtension): static
* @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
public function withFallbackExtension(string $fallbackExtension, string ...$otherFallbacks): static
{
$new = clone $this;
$new->fallbackExtension = $fallbackExtension;
$new->fallbackExtensions = [$fallbackExtension, ...array_values($otherFallbacks)];

Check warning on line 129 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ public function withFallbackExtension(string $fallbackExtension, string ...$otherFallbacks) : static { $new = clone $this; - $new->fallbackExtensions = [$fallbackExtension, ...array_values($otherFallbacks)]; + $new->fallbackExtensions = [$fallbackExtension, ...$otherFallbacks]; return $new; } /**
return $new;
}

Expand Down Expand Up @@ -200,21 +203,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.
* @deprecated Since 8.0.1 and will be removed in the next major version. Use {@see getFallbackExtensions()} instead.
*/
public function getDefaultExtension(): string
{
return $this->getFallbackExtension();
return $this->getFallbackExtensions()[0];
}

/**
* Gets the fallback view file extension.
*
* @return string The fallback view file extension.
* @return string[] The fallback view file extension.
*/
public function getFallbackExtension(): string
public function getFallbackExtensions(): array

Check warning on line 218 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 getFallbackExtensions() : array + protected function getFallbackExtensions() : array { return $this->fallbackExtensions; }
{
return $this->fallbackExtension;
return $this->fallbackExtensions;
}

/**
Expand Down Expand Up @@ -627,7 +630,14 @@ private function findTemplateFile(string $view): string
return $file;
}

return $file . '.' . $this->fallbackExtension;
foreach ($this->fallbackExtensions as $fallbackExtension) {
$fileWithFallbackExtension = $file . '.' . $fallbackExtension;
if (is_file($fileWithFallbackExtension)) {
return $fileWithFallbackExtension;
}
}

return $file . '.' . $this->fallbackExtensions[0];
}

/**
Expand Down
65 changes: 46 additions & 19 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,32 +179,59 @@ public function testFlushViewFilesOnChangeContext(): void
$this->assertSame('42', $view->render('/change-context'));
}

public function testRenderWithoutFileExtension(): void
public function renderFilesWithExtensionProvider(): array
{
return [
[
'file',
'php',
'php',
['php'],
],
[
'file',
'tpl',
'tpl',
['tpl'],
],
[
'file',
'phpt',
'phpt',
['phpt'],
],
[
'file.txt',
'twig',
'twig',
['txt', 'twig'],
],
[
'file',
'smarty',
'smarty',
['smarty', 'twig'],
],
];
}

/**
* @dataProvider renderFilesWithExtensionProvider
*/
public function testRenderWithoutFileExtension(string $filename, string $extension, string $defaultExtension, array $fallbackExtensions): void
{
$view = $this
->createViewWithBasePath($this->tempDirectory)
->withContext($this->createContext($this->tempDirectory));
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');
file_put_contents("$this->tempDirectory/$filename.$extension", 'Test ' . $extension);

$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')
'Test ' . $extension,
$view->withDefaultExtension($defaultExtension)->render($filename) // BC test
);
$this->assertSame(
'Test phpt',
$view->withDefaultExtension('twig')
->withFallbackExtension('phpt')
->render('file')
'Test ' . $extension,
$view->withFallbackExtension(...$fallbackExtensions)->render($filename)
);
}

Expand Down Expand Up @@ -372,7 +399,7 @@ public function testAddToParameter(): void
$this->assertSame(['a'], $view->getParameter('test'));
}

public function testAddToParameterWithVaridicValues(): void
public function testAddToParameterWithVariadicValues(): void
{
$view = TestHelper::createView();

Expand Down

0 comments on commit 38e7351

Please sign in to comment.