在symfony中运行composer install后动态覆盖parameters.yml值

时间:2015-08-06 14:52:06

标签: symfony

此:

parameters_dev.yml.dist
   my_key: todays_timestamp_{$timestamp}

将生成:

parameters.yml
   my_key: todays_timestamp_9845712365
运行composer install

这可能还是有任何解决方法吗?我想要实现的目标是,每次运行composer install时,my_key都会有唯一的价值。

更新

config.yml

doctrine_cache:
    providers:
        my_memcached_cache:
            namespace: %cache_namespace%

parameters.yml

parameters:
    cache_namespace: todays_timestamp_

Memcache统计数据将始终如下:

Server 127.0.0.1:11211
stats cachedump 12 100

Server 127.0.0.1:11211
ITEM todays_timestamp_[My\Bundle\Acc][1] [1807 b; 1438597305 s]
ITEM todays_timestamp_[My\Bundle\Mer][1] [1707 b; 1438597305 s]
.....
.....
END

但我希望todays_timestamp_能够随时附加一个唯一的后缀来更改。{/ p>

2 个答案:

答案 0 :(得分:6)

由于您的容器参数通常仅在缓存预热时创建,因此您可以在每个部署的XxxExtension中创建参数。从这里开始,您可以使用prepend doctrine_cache配置namespace Acme\HelloBundle\DependencyInjection; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; // implement the PrependExtensionInterface so it gets picked up by the DI class AcmeHelloExtension extends Extension implements PrependExtensionInterface { // Add your usual extension stuff // add the prepend method to prepend your config onto the doctrine_cache config public function prepend(ContainerBuilder $container) { // check for the presence of the doctrine_cache bundle and throw an exception if (!$container->hasExtension('doctrine_cache')) { throw new \Exception('DoctrineCacheBundle must be registered in kernel'); } // add your config with your timestamp to the doctrine_cache config $container->prependExtensionConfig( 'doctrine_cache', array( 'providers' => array( 'my_memcached_cache' => array( 'namespace' => sprintf( // your cache key with timestamp 'memcache_timestamp_%s', \DateTime::getTimestamp() ), ), ), ) ); } } 而不需要反过来。

在您的扩展程序中,您可以执行以下操作...

doctrine_cache

这种方式每次编译容器时都应重建id movie v1 v2 v3 v4 v5 v6 1 LTR comedy highbudget action comedy jj horror 2 MI newmovie fiction scifi funny xx jhee 的配置,并将时间戳缓存键更新为" now"停止你需要挂钩作曲家更新或类似的东西。

有关PrependExtension stuff check out the docs的更多信息。

答案 1 :(得分:4)

I do a similar think for the asset version management. Practically, I suggest you the following approach:

<?php
// app/config/cache_namespace_version.php
        $container->loadFromExtension('doctrine_cache', array(
                'providers' => array(
                    'my_memcached_cache' => array(
                        'namespace' => exec('git rev-parse --short HEAD'),  // Or the current timestamp
                    ),
                )));

And import in the config_prod.ml

# app/config/config.yml

imports:
    - { resource: config.yml }
    - { resource: cache_namespace_version.php }

You can use the current timestamp instead of the git hash commit, depends if you want to change the key every cache clear or only if the code change.

Check that I have injected the correct doctrine_cache key into the container. Inspired by this article

Hope this help