2015-10-03 68 views
1

我已成功設置我的Symfony 2.7.x安裝以使用標準資產版本策略(EmptyVersionStrategy和StaticVersionStrategy),但希望實施Custom Version Strategy,如基於日期的策略或類似方法。Symfony 2.7+資產定製版本策略配置

目前,我有

config.yml

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

由於version價值似乎執行該戰略,也值,我該如何配置自定義策略?

我已閱讀Asset Blog articleless-than-complete docs

+0

我找到了[這個symfony ticket](https://github.com/symfony/sym fony/issues/15554),它基本上概述了同樣的問題,但目前還沒有提供解決方案。 – craigh

回答

1

您可以輕鬆更換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 <[email protected]> 
* @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)); 
    } 
}