-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Framework] Add tag assets.package to register asset packages #38366
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
Conversation
02ab020 to
379cae3
Compare
379cae3 to
4fbcde6
Compare
nicolas-grekas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we register the new tag for autoconfiguration?
4fbcde6 to
be31942
Compare
We need the name of the package. This property is not provided by the |
c6d2684 to
e854986
Compare
|
I figured out that there are 3 ways to name the package when a service is tagged (code in 1. Explicit name in tag definition avatar.package:
class: Symfony\Component\Asset\Package
arguments:
- '@avatar.strategy'
tags:
- { name: assets.package, package: avatars }The package get its name from the tag definition: <img src="{{ asset('style.css', 'avatars') }}">2. Using a static method of the "custom" classWhen using a custom class, the package name is statically returned by the method App\Asset\AvatarPackage:
arguments:
- '@avatar.strategy'
tags:
- { name: assets.package }namespace App\Asset;
use Symfony\Component\Asset\Package;
class AvatarPackage extends Package
{
public static function getDefaultName(): string
{
return 'avatars';
}
}Usage: <img src="{{ asset('style.css', 'avatars') }}">2. Fallback to the service Id avatar.package:
class: Symfony\Component\Asset\Package
arguments:
- '@avatar.strategy'
tags:
- { name: assets.package }When the tag does not have the attribute Usage: <img src="{{ asset('style.css', 'avatar.package') }}">ConclusionI think, this is an acceptable solution to setup autoconfiguration of the tag for |
|
We've just moved away from |
…ackages (GromNaN) This PR was merged into the 5.3-dev branch. Discussion ---------- [Framework] Add tag assets.package to register asset packages Replaces #38366 | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | symfony/symfony-docs#14962 To configure asset packages in an application, we have to declare it in the `framework` configuration ([doc for assets config](https://symfony.com/doc/current/reference/configuration/framework.html#assets)). In some case we cannot use this configuration: - To use a custom class as package - To register an asset package in a shared bundle (my use-case). This PR adds the `assets.package` tag. This tag is use to collect and inject package services into the `assets.packages` service, that is the registry for all packages. Since every package needs a name, the `package` attribute of the tag is used (same naming convention that the `console.command` tag). Main changes: - the packages defined in the `framework.assets` configuration are injected into the `assets.packages` using the tag instead of being directly injected in service declaration. - changed signature of `Symfony\Components\Assets\Packages` constructor to accept an iterator (backward compatible). - a new alias `assets._default_package` is defined even if assets are not configured. ### Example in `symfony/demo` ([commit](symfony/demo@e5e5a8f...GromNaN:assets-package-tag)): In `config/services.yaml`: ```yaml avatar.strategy: class: Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy arguments: - '%kernel.project_dir%/public/build/manifest.json' avatar.package: class: Symfony\Component\Asset\Package arguments: - '@avatar.strategy' - '@assets.context' tags: - { name: assets.package, package: avatars } ``` Then we can use the package anywhere ```twig <img src="{{ asset('anna.jpg', 'avatars') }}"> ``` ### Alternative using autoconfiguration with a custom class: With a custom class implementing the `PackageInterface`, the package name can be provided by a the static method `getDefaultPackageName`. Autowiring and autoconfiguration will import the package. ```php namespace App\Asset; use Symfony\Component\Asset\PackageInterface; class AvatarPackage implements PackageInterface { public static function getDefaultPackageName(): string { return 'avatars'; } // ... Implements the interface } ``` Commits ------- 6217ff7 [Asset] Add tag assets.package to register asset packages
To configure asset packages in an application, we have to declare it in the
frameworkconfiguration (doc for assets config). In some case we cannot use this configuration:This PR adds the
assets.packagetag. This tag is use to collect and inject package services into theassets.packagesservice, that is the registry for all packages. Since every package needs a name, thepackageattribute of the tag is used (same naming convention that theconsole.commandtag).Main changes:
framework.assetsconfiguration are injected into theassets.packagesusing the tag instead of being directly injected in service declaration.Symfony\Components\Assets\Packagesconstructor to accept an iterator (backward compatible).assets._default_packageis defined even if assets are not configured.Example in
symfony/demo(commit):In
config/services.yaml:Then we can use the package anywhere