Для ресурсов добавлены версии по содержимому
This commit is contained in:
@ -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');
|
||||
}
|
||||
}
|
8
bundle/TwigExtensionBundle/Resources/config/services.yml
Normal file
8
bundle/TwigExtensionBundle/Resources/config/services.yml
Normal file
@ -0,0 +1,8 @@
|
||||
services:
|
||||
homepage.twig_extension:
|
||||
class: \Homepage\TwigExtensionBundle\TwigExtension
|
||||
arguments:
|
||||
- '%sculpin.output_dir%'
|
||||
public: yes
|
||||
tags:
|
||||
- { name: twig.extension }
|
42
bundle/TwigExtensionBundle/TwigExtension.php
Normal file
42
bundle/TwigExtensionBundle/TwigExtension.php
Normal 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;
|
||||
}
|
||||
}
|
9
bundle/TwigExtensionBundle/TwigExtensionBundle.php
Normal file
9
bundle/TwigExtensionBundle/TwigExtensionBundle.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Homepage\TwigExtensionBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class TwigExtensionBundle extends Bundle
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user