Для ресурсов добавлены версии по содержимому

This commit is contained in:
2019-01-06 22:11:25 +03:00
parent f131f53ea7
commit d1b9124405
12 changed files with 92 additions and 11 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace Homepage\TwigExtensionBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class TwigExtensionExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

View File

@ -0,0 +1,8 @@
services:
homepage.twig_extension:
class: \Homepage\TwigExtensionBundle\TwigExtension
arguments:
- '%sculpin.output_dir%'
public: yes
tags:
- { name: twig.extension }

View File

@ -0,0 +1,42 @@
<?php
namespace Homepage\TwigExtensionBundle;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class TwigExtension extends AbstractExtension
{
private $outputDir;
public function __construct(string $outputDir)
{
$this->outputDir = $outputDir;
}
public function getFunctions(): array
{
return [
new TwigFunction('hashed_asset', [$this, 'createHashedFileLink']),
];
}
public function createHashedFileLink(string $path): string
{
$fullPath = $this->join($this->outputDir, $path);
$realPath = realpath($fullPath);
if (!file_exists($realPath)) {
return sprintf('%s?v=%s', $path, time());
}
$hash = md5_file($realPath);
return sprintf('%s?v=%s', $path, $hash);
}
private function join(string $base, string $path): string
{
return $path ? rtrim($base, '/').'/'.ltrim($path, '/') : $base;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Homepage\TwigExtensionBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class TwigExtensionBundle extends Bundle
{
}