Skip to content

Backport json manifest version strategy #4

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

Closed
68 changes: 68 additions & 0 deletions VersionStrategy/JsonManifestVersionStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Asset\VersionStrategy;

/**
* Reads the versioned path of an asset from a JSON manifest file.
*
* For example, the manifest file might look like this:
* {
* "main.js": "main.abc123.js",
* "css/styles.css": "css/styles.555abc.css"
* }
*
* You could then ask for the version of "main.js" or "css/styles.css".
*/
class JsonManifestVersionStrategy implements VersionStrategyInterface
{
private $manifestPath;
private $manifestData;

/**
* @param string $manifestPath Absolute path to the manifest file
*/
public function __construct(string $manifestPath)
{
$this->manifestPath = $manifestPath;
}

/**
* With a manifest, we don't really know or care about what
* the version is. Instead, this returns the path to the
* versioned file.
*/
public function getVersion(string $path)
{
return $this->applyVersion($path);
}

public function applyVersion(string $path)
{
return $this->getManifestPath($path) ?: $path;
}

private function getManifestPath(string $path): ?string
{
if (null === $this->manifestData) {
if (!is_file($this->manifestPath)) {
throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath));
}

$this->manifestData = json_decode(file_get_contents($this->manifestPath), true);
if (0 < json_last_error()) {
throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": '.json_last_error_msg(), $this->manifestPath));
}
}

return isset($this->manifestData[$path]) ? $this->manifestData[$path] : null;
}
}