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

Ben*_*der 4 symfony

这个:

parameters_dev.yml.dist
   my_key: todays_timestamp_{$timestamp}
Run Code Online (Sandbox Code Playgroud)

会产生:

parameters.yml
   my_key: todays_timestamp_9845712365
Run Code Online (Sandbox Code Playgroud)

跑完之后 composer install

这可能还是有任何解决方法吗?我想要实现的是,每当我跑步时composer install,my_key都会有一个独特的价值.


UPDATE

config.yml

doctrine_cache:
    providers:
        my_memcached_cache:
            namespace: %cache_namespace%
Run Code Online (Sandbox Code Playgroud)

parameters.yml

parameters:
    cache_namespace: todays_timestamp_
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

但是我希望todays_timestamp_随时添加一个独特的后缀来改变它.

qoo*_*mao 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()
                        ),
                     ),
                ),
            )
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方式每次编译容器时,都应该重新构建配置,doctrine_cache并将时间戳缓存键更新为"现在",阻止您需要挂钩到编辑器更新或类似的东西.

有关这些PrependExtension东西的更多信息,请查看文档.