diff --git a/app/SculpinKernel.php b/app/SculpinKernel.php index 4755b50..f4dcde7 100644 --- a/app/SculpinKernel.php +++ b/app/SculpinKernel.php @@ -2,6 +2,7 @@ use Homepage\HtmlPrettierBundle\HtmlPrettierBundle; use Homepage\SiteMapBundle\SiteMapBundle; +use Homepage\TwigExtensionBundle\TwigExtensionBundle; use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel; class SculpinKernel extends AbstractKernel @@ -9,6 +10,7 @@ class SculpinKernel extends AbstractKernel protected function getAdditionalSculpinBundles(): array { return [ + TwigExtensionBundle::class, SiteMapBundle::class, HtmlPrettierBundle::class, ]; diff --git a/bundle/TwigExtensionBundle/DependencyInjection/TwigExtensionExtension.php b/bundle/TwigExtensionBundle/DependencyInjection/TwigExtensionExtension.php new file mode 100644 index 0000000..3c7f17b --- /dev/null +++ b/bundle/TwigExtensionBundle/DependencyInjection/TwigExtensionExtension.php @@ -0,0 +1,20 @@ +load('services.yml'); + } +} diff --git a/bundle/TwigExtensionBundle/Resources/config/services.yml b/bundle/TwigExtensionBundle/Resources/config/services.yml new file mode 100644 index 0000000..f68ed62 --- /dev/null +++ b/bundle/TwigExtensionBundle/Resources/config/services.yml @@ -0,0 +1,8 @@ +services: + homepage.twig_extension: + class: \Homepage\TwigExtensionBundle\TwigExtension + arguments: + - '%sculpin.output_dir%' + public: yes + tags: + - { name: twig.extension } diff --git a/bundle/TwigExtensionBundle/TwigExtension.php b/bundle/TwigExtensionBundle/TwigExtension.php new file mode 100644 index 0000000..2739aac --- /dev/null +++ b/bundle/TwigExtensionBundle/TwigExtension.php @@ -0,0 +1,42 @@ +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; + } +} diff --git a/bundle/TwigExtensionBundle/TwigExtensionBundle.php b/bundle/TwigExtensionBundle/TwigExtensionBundle.php new file mode 100644 index 0000000..cd4152c --- /dev/null +++ b/bundle/TwigExtensionBundle/TwigExtensionBundle.php @@ -0,0 +1,9 @@ + + {% endblock %} {% block content %} diff --git a/source/_layouts/album.html.twig b/source/_layouts/album.html.twig index ba7f8f6..d044718 100644 --- a/source/_layouts/album.html.twig +++ b/source/_layouts/album.html.twig @@ -1,7 +1,7 @@ {% extends 'internal.html.twig' %} {% block css %} - + {% endblock %} {% block main %} diff --git a/source/_layouts/default.html.twig b/source/_layouts/default.html.twig index e0d1374..d863786 100644 --- a/source/_layouts/default.html.twig +++ b/source/_layouts/default.html.twig @@ -2,7 +2,7 @@
{% include 'head.twig' %} - + {% block css %}{% endblock %} @@ -15,7 +15,7 @@ {% include 'counters.twig' %} - + {% block js %}{% endblock %} {% include 'font-awesome.twig' %} diff --git a/source/_layouts/internal.html.twig b/source/_layouts/internal.html.twig index a435eb0..de0438d 100644 --- a/source/_layouts/internal.html.twig +++ b/source/_layouts/internal.html.twig @@ -2,7 +2,7 @@ {% include 'head.twig' %} - + {% block css %}{% endblock %} @@ -23,7 +23,7 @@ {% include 'counters.twig' %} - + {% block js %}{% endblock %} {% include 'font-awesome.twig' %} diff --git a/source/about/me.html.twig b/source/about/me.html.twig index f9c3c5f..880b0d1 100644 --- a/source/about/me.html.twig +++ b/source/about/me.html.twig @@ -5,11 +5,11 @@ description: Несколько фактов об авторе --- {% block js %} - + {% endblock %} {% block css %} - + {% endblock %} {% block content %} diff --git a/source/index.html.twig b/source/index.html.twig index 68ceb92..1e08479 100644 --- a/source/index.html.twig +++ b/source/index.html.twig @@ -3,7 +3,7 @@ layout: default description: Личный сайт Антона Вахрушева --- {% block css %} - + {% endblock %} {% block content %} diff --git a/source/projects/predictor/index.html.twig b/source/projects/predictor/index.html.twig index c0c8030..b891d75 100644 --- a/source/projects/predictor/index.html.twig +++ b/source/projects/predictor/index.html.twig @@ -5,11 +5,11 @@ description: Демо-версия электронной гадалки Шен --- {% block js %} - + {% endblock %} {% block css %} - + {% endblock %} {% block content %}