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

标签 symfony

这个:

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

会产生:
parameters.yml
   my_key: todays_timestamp_9845712365

运行后 composer install
这可能还是任何解决方法?我想要实现的是,每次运行时 composer install , my_key将具有独特的值(value)。

更新

配置文件
doctrine_cache:
    providers:
        my_memcached_cache:
            namespace: %cache_namespace%

参数.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_通过附加一个唯一的后缀来随时更改。

最佳答案

由于您的容器参数通常仅在缓存预热时创建,您可以在 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 的配置。随着您的时间戳缓存 key 更新为“现在”,您就不需要 Hook Composer 更新或类似的东西。

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

关于symfony - 在 symfony 中运行 composer install 后动态覆盖 parameters.yml 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859005/

相关文章:

php - Symfony 和 Doctrine - 查询缺少标识符 id

javascript - Symfony2 从 javascript 中提取文本进行翻译

php - Twig - 在选择选项中添加翻译

php - Symfony2 - Doctrine-extension - 用户可登录

php - 如何强制用户注销symfony2

php - Symfony 2 - 使用子实体创建表单并上传文件

php - 升级到 Symfony 2.8 后出现 MappingException

php - 错误 : Cannot use object of type Symfony\Component\HttpFoundation\Request as array upgrading to symfony 3. 0

php - 尝试清除 Symfony 4 应用程序缓存时出现 "Cannot autowire service"异常

php - Symfony 2/Doctrine : How to lower the num of queries without losing the benefit of ORM?