-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Framework] Add tag assets.package #14962
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Feb 11, 2021
fabpot
added a commit
to symfony/symfony
that referenced
this pull request
Feb 16, 2021
…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
symfony-splitter
pushed a commit
to symfony/framework-bundle
that referenced
this pull request
Feb 16, 2021
…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 ------- 6217ff7b6f [Asset] Add tag assets.package to register asset packages
5e62181
to
f4dbec4
Compare
Jérôme, thanks for implementing this feature and for adding its docs too! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull
302F
request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Documentation for symfony/symfony#38473