Enable versioning of assets in production
This commit is contained in:
parent
052f4dd521
commit
d192b04aef
4 changed files with 84 additions and 9 deletions
|
@ -11,7 +11,17 @@ sculpin_content_types:
|
|||
permalink: talks/:basename/
|
||||
taxonomies: [tags]
|
||||
|
||||
parameters:
|
||||
asset.manifest_dir: '%sculpin.source_dir%'
|
||||
|
||||
services:
|
||||
App\Asset\TwigExtension\EncoreExtension:
|
||||
autowire: true
|
||||
arguments:
|
||||
$manifestDir: '%asset.manifest_dir%'
|
||||
tags:
|
||||
- { name: twig.extension }
|
||||
|
||||
App\Talk\TwigExtension\TalksExtension:
|
||||
tags:
|
||||
- { name: twig.extension }
|
||||
|
|
63
app/src/Asset/TwigExtension/EncoreExtension.php
Normal file
63
app/src/Asset/TwigExtension/EncoreExtension.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Asset\TwigExtension;
|
||||
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class EncoreExtension extends AbstractExtension
|
||||
{
|
||||
/** @var string */
|
||||
private $manifestDir;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(string $manifestDir)
|
||||
{
|
||||
$this->manifestDir = $manifestDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'app.encore';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('asset', [$this, 'generateAssetPaths']),
|
||||
];
|
||||
}
|
||||
|
||||
public function generateAssetPaths(string $assetName): string
|
||||
{
|
||||
if (!$manifest = file_get_contents($this->manifestPath($assetName))) {
|
||||
return $this->defaultPath($assetName);
|
||||
}
|
||||
|
||||
return (new Collection(json_decode($manifest, true)))
|
||||
->get($assetName, $assetName);
|
||||
}
|
||||
|
||||
private function manifestPath(string $assetName): string
|
||||
{
|
||||
return preg_replace('/(?<=\/)[\w.]+$/', 'manifest.json', $this->defaultPath($assetName)) ?? $this->defaultPath($assetName);
|
||||
}
|
||||
|
||||
private function defaultPath(string $assetName): string
|
||||
{
|
||||
return "{$this->manifestDir}/{$assetName}";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue