Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding parameters.yml values dynamically after running composer install in symfony

Tags:

symfony

This:

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

would generate:

parameters.yml
   my_key: todays_timestamp_9845712365

after running composer install

Is this possible yet or any workaround? What I'm trying to achieve is, everytime when I run composer install, my_key will have a unique value.


UPDATE

config.yml

doctrine_cache:
    providers:
        my_memcached_cache:
            namespace: %cache_namespace%

parameters.yml

parameters:
    cache_namespace: todays_timestamp_

Memcache stats will always be like this:

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

but I want todays_timestamp_ to be changed all the time with attaching a unique suffix to it.

like image 461
BentCoder Avatar asked Dec 24 '22 16:12

BentCoder


2 Answers

As your container parameters are only generally created on cache warmup you can create the parameter in your XxxExtension on each deploy. From here you can use then prepend your configuration for doctrine_cache rather than needing to do it the other way around.

In your extension you can do the following...

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()
                        ),
                     ),
                ),
            )
        );
    }
}

This way each time the container is compiled it should rebuild the config for doctrine_cache with your timestamped cache key updated to "now" stopping you needing to hook into composer updates or anything like that.

For more info on the PrependExtension stuff check out the docs.

like image 103
qooplmao Avatar answered Dec 27 '22 20:12

qooplmao


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

like image 31
Matteo Avatar answered Dec 27 '22 21:12

Matteo