ParameterHandler icon indicating copy to clipboard operation
ParameterHandler copied to clipboard

Update parameters based on vcs-status

Open rvanlaak opened this issue 11 years ago • 8 comments

I'm using Assetic in combination with compass and uglifycss to manage my assets. One of the configuration parameters of Assetic is assets_version in order to handle cache busting.

It would be nice if the ParameterHandler could update a parameter based on the current revision of a version control system (like svn or git). That would result in cache busting based on the code version, and would not bust the cache in case the code hasn't been updated but just some composer packages are updated.

rvanlaak avatar Nov 12 '13 08:11 rvanlaak

@Rvanlaak Dealing with the same problem I came up with the following solution:

app/config/config.yml:

imports:
    - { resource: assets_version.php }
...
framework:
    ...
    templating:
        engines: ['twig']
        packages:
            style:
                version_format: %%s?v=%%s

app/config/assets_version.php

$container->loadFromExtension(
    'framework',
    array(
        'templating' => array(
            'packages' => array(
                'style' => array(
                    'version' => substr(
                        file_exists(__DIR__.'/../../web/css/style.css')
                            ? sha1_file(__DIR__.'/../../web/css/style.css')
                            : md5(time())
                        ,
                        0,
                        6
                    )
                ),
                ...
            )
        ),
    )
);

This way the assets version only changes if there are actual changes in the file.

hacfi avatar Nov 18 '13 02:11 hacfi

very nice @hacfi this is the central css file that gets finally generated from all others right?

cordoval avatar Nov 18 '13 14:11 cordoval

@Rvanlaak ParameterHandler would not be able to get the asset version properly IMO.

a far better way to deal with it is to use the cache busting feature in Assetic. See https://github.com/symfony/AsseticBundle/pull/240 for the PR adding it in the bundle.

stof avatar Nov 18 '13 14:11 stof

@cordoval Yes, in my template I got

  {% stylesheets '@ProjectAppBundle/Resources/less/make.less' filter='lessphp,?yui_css' output="css/style.css" package="style" -%}
    <link rel="stylesheet" href="{{ asset_url }}" />
  {% endstylesheets %}

I actually have more than this one css file but the main styling is that one big file. Instead of adding all the css/less files in my twig template I prefer doing this in the make.less.

hacfi avatar Nov 18 '13 15:11 hacfi

you can use env var to achive this one:

ASSET_VERSION=`git log -1 --pretty=%h` composer install --no-interaction

and of course, edit the composer.json as decribe in the README.

lyrixx avatar Jan 07 '14 20:01 lyrixx

Both using an env var and editing the composer.json do not seem appropriate to me, because then I'm unable to access it in for example my Twig templates.

I solved this by a basic parameter in config.yml, which I also put in the Twig globals config section. Hereby I can use it in my entire application. And experience of the last weeks told me, sometimes the asset_version does not has to be incremented based on the revision number, because no css/jss/twig-files were committed and no cache busting was needed.

rvanlaak avatar Jan 07 '14 21:01 rvanlaak

not sure if it could help you but i have a working easy example : https://github.com/pborreli/composer-service/pull/51#issuecomment-31610405

pborreli avatar Jan 07 '14 22:01 pborreli

@Rvanlaak Making the variable available in Twig is about using the parameter. It is not related to the way you define it.

and then, your original report was describing assets_version as an AsseticBundle config, which is wrong. It is a FrameworkBundle config. AsseticBundle indeed uses it by default. And it is now able to do proper cache busting as the PR has been merged. But the asset version can still be useful for other assets (images for instance).

stof avatar Jan 08 '14 11:01 stof