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

JsonManifestStrategy, support for FormatExtensionResolver #1588

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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This file contains a complete enumeration of all [pull requests](https://github.
for a given releases. Unreleased, upcoming changes will be updated here periodically; reference the next release on our
[milestones](https://github.com/liip/LiipImagineBundle/milestones) page for the latest changes.

## [2.12.0](https://github.com/liip/LiipImagineBundle/tree/2.12.0)
## [2.13.0](https://github.com/liip/LiipImagineBundle/tree/2.13.0)

- Support JsonManifestVersionStrategy that was added in Symfony 6.

Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/asset-versioning.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ setting for ``framework.assets.version``. It strips the version from the file
name and appends it to the resulting image URL so that the file is found and
cache busting is used.

Since LiipImagineBundle version 2.12, we integrate with the configuration
Since LiipImagineBundle version 2.13, we integrate with the configuration
setting for ``framework.assets.json_manifest_path``. The manifest file is used
to lookup the location of the actual file, and append the versioning string to
the resulting image URL so that cache busting is used.
Expand Down
52 changes: 51 additions & 1 deletion Templating/LazyFilterRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,63 @@ private function appendAssetVersion(string $resolvedPath, string $path): string
return $resolvedPath.$separator.$this->assetVersion;
}



if (\array_key_exists($path, $this->jsonManifest)) {

$prefixedSlash = '/' !== mb_substr($path, 0, 1) && '/' === mb_substr($this->jsonManifest[$path], 0, 1);
$versionedPath = $prefixedSlash ? mb_substr($this->jsonManifest[$path], 1) : $this->jsonManifest[$path];

$resolvedPath = str_replace($path, $versionedPath, $resolvedPath);
$originalExt = pathinfo($path, PATHINFO_EXTENSION);
$resolvedExt = pathinfo($resolvedPath, PATHINFO_EXTENSION);

if ($originalExt !== $resolvedExt) {
$path = str_replace('.'.$originalExt, '.'.$resolvedExt, $path);
$versionedPath = str_replace('.'.$originalExt, '.'.$resolvedExt, $versionedPath);
}

$versioning = $this->captureVersion(pathinfo($path, PATHINFO_BASENAME), pathinfo($versionedPath, PATHINFO_BASENAME));
$resolvedFilename = pathinfo($resolvedPath, PATHINFO_BASENAME);
$resolvedDir = pathinfo($resolvedPath, PATHINFO_DIRNAME);
$resolvedPath = $resolvedDir.'/'.$this->insertVersion($resolvedFilename, $versioning['version'], $versioning['position']);
}

return $resolvedPath;
}

/**
* Capture the versioning string from the versioned filename
*/
private function captureVersion(string $originalFilename, string $versionedFilename): array
{
$originalLength = strlen($originalFilename);
$versionedLength = strlen($versionedFilename);

for ($i = 0; $i < $originalLength && $i < $versionedLength; $i++) {
if ($originalFilename[$i] !== $versionedFilename[$i]) {
break;
}
}

$version = substr($versionedFilename, $i, $versionedLength - $originalLength);

return ['version' => $version, 'position' => $i];
}

/**
* Insert the version string into our resolved filename
*/
private function insertVersion(string $resolvedFilename, string $version, int $position): string
{
if ($position < 0 || $position > strlen($resolvedFilename)) {
return $resolvedFilename;
}

$firstPart = substr($resolvedFilename, 0, $position);
$secondPart = substr($resolvedFilename, $position);

$versionedFilename = $firstPart . $version . $secondPart;

return $versionedFilename;
}
}
33 changes: 33 additions & 0 deletions Tests/Templating/LazyFilterRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ public function testJsonManifestVersionHandling(string $sourcePath, string $vers
$this->assertSame($expectedPath, $actualPath);
}

/**
* @dataProvider provideJsonManifestSwapExt
*/
public function testJsonManifestVersionHandlingWithExtensionSwapping(string $sourcePath, string $versionedPath, $originalExt, $newExt): void
{
$this->runtime = new LazyFilterRuntime($this->manager, null, self::JSON_MANIFEST);

$cachePath = 'image/cache/'.self::FILTER.'/'.('/' === (mb_substr($sourcePath, 0, 1)) ? mb_substr($sourcePath, 1) : $sourcePath);
$cachePath = str_replace('.'.$originalExt, '.'.$newExt, $cachePath);
$expectedPath = 'image/cache/'.self::FILTER.'/'.('/' === (mb_substr($versionedPath, 0, 1)) ? mb_substr($versionedPath, 1) : $versionedPath);
$expectedPath = str_replace('.'.$originalExt, '.'.$newExt, $expectedPath);

$this->manager
->expects($this->once())
->method('getBrowserPath')
->with($sourcePath, self::FILTER)
->willReturn($cachePath);

$actualPath = $this->runtime->filter($versionedPath, self::FILTER);

$this->assertSame($expectedPath, $actualPath);
}

public function provideJsonManifest(): array
{
return [
Expand All @@ -138,6 +161,16 @@ public function provideJsonManifest(): array
];
}

public function provideJsonManifestSwapExt(): array
{
return [
'query parameter, no slash' => [array_keys(self::JSON_MANIFEST)[0], array_values(self::JSON_MANIFEST)[0], 'png', 'webp'],
'in filename, no slash' => [array_keys(self::JSON_MANIFEST)[1], array_values(self::JSON_MANIFEST)[1], 'png', 'webp'],
'query parameter, slash' => [array_keys(self::JSON_MANIFEST)[2], array_values(self::JSON_MANIFEST)[2], 'png', 'webp'],
'in filename, slash' => [array_keys(self::JSON_MANIFEST)[3], array_values(self::JSON_MANIFEST)[3], 'png', 'webp'],
];
}

public function testInvokeFilterCacheMethod(): void
{
$expectedInputPath = 'thePathToTheImage';
Expand Down
Loading