8000 [2.7][Asset] Framework config to support custom version strategy · Issue #15554 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.7][Asset] Framework config to support custom version strategy #15554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xavierbriand opened this issue Aug 14, 2015 · 8 comments
Closed

[2.7][Asset] Framework config to support custom version strategy #15554

xavierbriand opened this issue Aug 14, 2015 · 8 comments
Labels

Comments

@xavierbriand
Copy link
Contributor

The Framework bundle only implements two asset version strategies: empty and static (defaults provided by the Asset component).

If I want to use a custom strategy for my package (eg. to add the locale its path) without overriding the defaults, the recommended solution is to:

  • create a new service (my strategy)
  • use a compiler pass to extract the version and version format parameters from the default static strategy my package is compiled with
  • update the definition of my service with previously extracted parameters

If I have two packages with the same custom strategy but different versions, I need to duplicate my service declaration and update my compiler. And I cannot create services on-demand during the compiler pass. I could also create my own package configuration for my bundle that would override or extend the default assets.packages service.

It's neither convenient nor elegant.

I propose to add an optional parameter to set custom strategies:

#app/config/config.yml

framework:
    #...
    assets:
        base_urls: //my.cdn.com
        packages:
            pdf:
                version: %version%
                version_format: /name/space/%%2$s/pdf%%1$s
            localized_doc:
                version: %version%
                version_strategy: my.abstract.service.id # default is assets.static_version_strategy falling back on assets.empty_version_strategy if version is null (current behavior)
                version_format: /name/space/%%2$s/doc/%%3$s%/abc%1$s #%3$s would be my locale

A convenient abstract BaseVersionStrategy class could be provided that would declare setVersion and setVersionFormat to inject said parameters.

This solves a particular problem I have where I want to inject known parameters in an asset base_urls or base_path. Please provide your thoughts.

@xavierbriand xavierbriand changed the title [2.7][Assets] Framework config to support custom version strategy [2.7][Asset] Framework config to support custom version strategy Aug 14, 2015
@CaptainJojo
Copy link

Sory but you have example, I dont know how "use a compiler pass to extract the version and version format parameters from the default static strategy my package is compiled with"

@xavierbriand
Copy link
Contributor Author

@CaptainJojo You can find an example in symfony/symfony-docs#5489

@CaptainJojo
Copy link

Thx :)

@stof
Copy link
Member
stof commented Aug 28, 2015

@xavierbriand if we allow to pass the service id of a version strategy, this should be the id of a fully configured service (configuring it would be the responsibility of the code defining the service). Taking a definition id and then trying to modify it to inject some extra configuration in it requires making some assumptions about the API of the service class, and creates many cases where it is very hard to decide what would be the right behavior.
Doing a feature which works only for the basic use case is a no-go IMO (especially given that the most basic use case is already covered as it corresponds to the static version strategy).

And I cannot create services on-demand during the compiler pass.

why not ? Compiler passes are allowed to do that.

@xavierbriand
Copy link
Contributor Author

@stof I agree. And thanks for the heads up on the compiler pass, my first attempt was unsuccessful.

My point is it's inconvenient/unelegant/hacky to define your own strategy and/or have a different strategy for different packages. It might be that the default way constrict you in a specific pass (a version and version format, period) that is hard to deviate from. I'm not used to spend much time change/extend Sf's behavior.
An other direction could be to use a variables provider a la Assetic. That would allow to dynamically provide/compute whatever variables you need for you assets paths (locale, version, subpackage, fiel format, ...)

@craigh
Copy link
craigh commented Oct 4, 2015

status on this? any solutions provided anywhere?

@Laulibrius
Copy link
Contributor

Why not use tagged services to add packages ? like this:
https://gist.github.com/Laulibrius/f84dde94d38ee0183f0b

Or allow to add in config something like:

framework:
    assets:
        packages:
            some_name: @service_id

@xabbuh
Copy link
Member
xabbuh commented Jan 30, 2016

Symfony 3.1 will come with new version_strategy options thanks to the great work done by @ewgRa in #17532.

@xabbuh xabbuh closed this as completed Jan 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants
0