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

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

View File

@ -2,6 +2,7 @@
use Homepage\HtmlPrettierBundle\HtmlPrettierBundle; use Homepage\HtmlPrettierBundle\HtmlPrettierBundle;
use Homepage\SiteMapBundle\SiteMapBundle; use Homepage\SiteMapBundle\SiteMapBundle;
use Homepage\TwigExtensionBundle\TwigExtensionBundle;
use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel; use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel;
class SculpinKernel extends AbstractKernel class SculpinKernel extends AbstractKernel
@ -9,6 +10,7 @@ class SculpinKernel extends AbstractKernel
protected function getAdditionalSculpinBundles(): array protected function getAdditionalSculpinBundles(): array
{ {
return [ return [
TwigExtensionBundle::class,
SiteMapBundle::class, SiteMapBundle::class,
HtmlPrettierBundle::class, HtmlPrettierBundle::class,
]; ];

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
{
}

View File

@ -3,7 +3,7 @@ layout: default
description: Личный сайт Антона Вахрушева description: Личный сайт Антона Вахрушева
--- ---
{% block css %} {% block css %}
<link rel="stylesheet" href="/static/index.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/index.css') }}">
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -1,7 +1,7 @@
{% extends 'internal.html.twig' %} {% extends 'internal.html.twig' %}
{% block css %} {% block css %}
<link rel="stylesheet" href="/static/layout_album.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/layout_album.css') }}">
{% endblock %} {% endblock %}
{% block main %} {% block main %}

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
{% include 'head.twig' %} {% include 'head.twig' %}
<link rel="stylesheet" href="/static/layout_default.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/layout_default.css') }}">
{% block css %}{% endblock %} {% block css %}{% endblock %}
</head> </head>
<body> <body>
@ -15,7 +15,7 @@
{% include 'counters.twig' %} {% include 'counters.twig' %}
<script async src="/static/layout_default.js?v={{ date().timestamp }}"></script> <script async src="{{ hashed_asset('/static/layout_default.js') }}"></script>
{% block js %}{% endblock %} {% block js %}{% endblock %}
{% include 'font-awesome.twig' %} {% include 'font-awesome.twig' %}

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
{% include 'head.twig' %} {% include 'head.twig' %}
<link rel="stylesheet" href="/static/layout_internal.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/layout_internal.css') }}">
{% block css %}{% endblock %} {% block css %}{% endblock %}
</head> </head>
<body> <body>
@ -23,7 +23,7 @@
{% include 'counters.twig' %} {% include 'counters.twig' %}
<script async src="/static/layout_internal.js?v={{ date().timestamp }}"></script> <script async src="{{ hashed_asset('/static/layout_internal.js') }}"></script>
{% block js %}{% endblock %} {% block js %}{% endblock %}
{% include 'font-awesome.twig' %} {% include 'font-awesome.twig' %}

View File

@ -5,11 +5,11 @@ description: Несколько фактов об авторе
--- ---
{% block js %} {% block js %}
<script async src="/static/about_me.js?v={{ date().timestamp }}"></script> <script async src="{{ hashed_asset('/static/about_me.js') }}"></script>
{% endblock %} {% endblock %}
{% block css %} {% block css %}
<link rel="stylesheet" href="/static/about_me.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/about_me.css') }}">
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -3,7 +3,7 @@ layout: default
description: Личный сайт Антона Вахрушева description: Личный сайт Антона Вахрушева
--- ---
{% block css %} {% block css %}
<link rel="stylesheet" href="/static/index.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/index.css') }}">
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -5,11 +5,11 @@ description: Демо-версия электронной гадалки Шен
--- ---
{% block js %} {% block js %}
<script async src="/static/predictor.js?v={{ date().timestamp }}"></script> <script async src="{{ hashed_asset('/static/predictor.js') }}"></script>
{% endblock %} {% endblock %}
{% block css %} {% block css %}
<link rel="stylesheet" href="/static/predictor.css?v={{ date().timestamp }}"> <link rel="stylesheet" href="{{ hashed_asset('/static/predictor.css') }}">
{% endblock %} {% endblock %}
{% block content %} {% block content %}