Skip to content

Commit

Permalink
JsonManifestStrategy, support for FormatExtensionResolver (#1588)
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterSkepp authored and dbu committed Jun 19, 2024
1 parent 9a4d51d commit d10b3c6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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.

<<<<<<< HEAD
# 2.x

## unreleased
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

0 comments on commit d10b3c6

Please sign in to comment.