Created
October 26, 2017 22:47
-
-
Save tayhimself/0bd6360b145654133f3337d82b97714a to your computer and use it in GitHub Desktop.
VersionStrategy for using assets versioned with gulp-rev in Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework: | |
assets: | |
version_strategy: app.assets.gulp_rev_version_strategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Twig; | |
use Exception; | |
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; | |
class GulpRevVersionStrategy implements VersionStrategyInterface | |
{ | |
private $manifestFilename; | |
private $manifestDir; | |
private $paths = []; | |
/** | |
* VersionStrategy constructor. | |
* | |
* @param string $manifestDir | |
* @param string $manifestFilename | |
*/ | |
public function __construct($manifestDir, $manifestFilename = 'rev-manifest.json') | |
{ | |
$this->manifestFilename = $manifestFilename; | |
$this->manifestDir = $manifestDir; | |
} | |
public function getVersion($path) | |
{ | |
if (file_exists($path)) { | |
return null; | |
} | |
$path = pathinfo($this->getAssetVersion($path)); | |
$filenameParts = explode('-', $path['filename']); | |
// With gulp rev, the version is at the end of the filename so it will be the last item of the array | |
return $filenameParts[count($filenameParts) - 1]; | |
} | |
public function applyVersion($path) | |
{ | |
return $this->getAssetVersion($path); | |
} | |
private function getAssetVersion($path) | |
{ | |
// The twig extension is a singleton so we store the loaded content into a property to read it only once | |
// @see https://knpuniversity.com/screencast/gulp/version-cache-busting#comment-2884388919 | |
if (count($this->paths) === 0) { | |
$manifestPath = $this->manifestDir .'/'. $this->manifestFilename;$manifestPath = $this->manifestDir .'/'. $this->manifestFilename; | |
if (!is_file($manifestPath)) { | |
throw new Exception( | |
sprintf( | |
'Manifest file "%s" not found in path "%s". You can generate this file running gulp', | |
$this->manifestFilename, $this->manifestDir | |
) | |
); | |
} | |
$this->paths = json_decode(file_get_contents($manifestPath), true); | |
} | |
// If a file exists, it doesn't have a version so we ignore it | |
$fileExists = file_exists($path); | |
$hasVersion = isset($this->paths[$path]); | |
if (!$fileExists && !$hasVersion) { | |
throw new Exception(sprintf('The file "%s" does not exist and there is no version file for it', $path)); | |
} | |
return $hasVersion ? $this->paths[$path] : $path; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
app.assets.gulp_rev_version_strategy: | |
class: AppBundle\Twig\GulpRevVersionStrategy | |
arguments: ["%kernel.root_dir%/Resources/assets"] | |
public: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment