Symfony 2.7+资产自定义版本策略配置

时间:2015-10-03 21:15:04

标签: symfony assets symfony-2.7

我已成功设置Symfony 2.7.x安装以使用标准资产版本策略(EmptyVersionStrategy和StaticVersionStrategy),但希望实现Custom Version Strategy类似基于日期的策略或类似策略。

目前,我有

config.yml

framework:
    assets:
        version: 'v1' # or ~ for EmptyVersionStrategy

由于version值似乎实现了策略以及价值,我该如何配置自定义策略?

我已阅读Asset Blog articleless-than-complete docs

1 个答案:

答案 0 :(得分:3)

您可以轻松替换EmptyVersionStrategy,只需覆盖服务,如下所示:

应用/配置/ services.yml

services:
    assets.empty_version_strategy:
        class:      FooBundle\Twig\Extension\AssetVersionStrategy
        arguments:  ["%kernel.root_dir%"]

<强>的src / FooBundle \枝条\ AssetVersionStrategy.php

namespace FooBundle\Twig\Extension;

use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;


/**
 * AssetVersionStrategy (Twig)
 *
 * @author Your Name <you@example.org>
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class AssetVersionStrategy implements VersionStrategyInterface
{
    /**
     * @var string
     */
    private $root;

    /**
     * @param string $root
     */
    public function __construct($root)
    {
        $this->root = $root;
    }

    /**
     * Should return the version by the given path (starts from public root)
     *
     * @param string $path
     * @return string
     */
    public function getVersion($path)
    {
        return $path;
    }

    /**
     * Determines how to apply version into the given path
     *
     * @param string $path
     * @return string
     */
    public function applyVersion($path)
    {
        return sprintf('%s?v=%s', $path, $this->getVersion($path));
    }
}