8000 feature #38473 [Framework] Add tag assets.package to register asset p… · symfony/symfony@1262b06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1262b06

Browse files
committed
feature #38473 [Framework] Add tag assets.package to register asset packages (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
2 parents e2b1d9c + 6217ff7 commit 1262b06

File tree

7 files changed

+20
-14
lines changed

7 files changed

+20
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Added the `event_dispatcher.dispatcher` tag
1414
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
1515
* Add support for configuring UUID factory services
16+
* Add tag `assets.package` to register asset packages
1617

1718
5.2.0
1819
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class UnusedTagsPass implements CompilerPassInterface
2323
{
2424
private $knownTags = [
2525
'annotations.cached_reader',
26+
'assets.package',
2627
'auto_alias',
2728
'cache.pool',
2829
'cache.pool.clearer',

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ public function load(array $configs, ContainerBuilder $container)
472472
$loader 8000 ->load('mime_type.php');
473473
}
474474

475+
$container->registerForAutoconfiguration(PackageInterface::class)
476+
->addTag('assets.package');
475477
$container->registerForAutoconfiguration(Command::class)
476478
->addTag('console.command');
477479
$container->registerForAutoconfiguration(ResourceCheckerInterface::class)
@@ -1120,7 +1122,6 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
11201122
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
11211123
$container->setDefinition('assets._default_package', $defaultPackage);
11221124

1123-
$namedPackages = [];
11241125
foreach ($config['packages'] as $name => $package) {
11251126
if (null !== $package['version_strategy']) {
11261127
$version = new Reference($package['version_strategy']);
@@ -1134,15 +1135,11 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
11341135
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name);
11351136
}
11361137

1137-
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
1138+
$packageDefinition = $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version)
1139+
->addTag('assets.package', ['package' => $name]);
1140+
$container->setDefinition('assets._package_'.$name, $packageDefinition);
11381141
$container->registerAliasForArgument('assets._package_'.$name, PackageInterface::class, $name.'.package');
1139-
$namedPackages[$name] = new Reference('assets._package_'.$name);
11401142
}
1141-
1142-
$container->getDefinition('assets.packages')
1143-
->replaceArgument(0, new Reference('assets._default_package'))
1144-
->replaceArgument(1, $namedPackages)
1145-
;
11461143
}
11471144

11481145
/**

src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
$container->services()
3131
->set('assets.packages', Packages::class)
3232
->args([
33-
service('assets.empty_package'),
34-
[],
33+
service('assets._default_package'),
34+
tagged_iterator('assets.package', 'package'),
3535
])
3636

3737
->alias(Packages::class, 'assets.packages')
@@ -41,6 +41,8 @@
4141
service('assets.empty_version_strategy'),
4242
])
4343

44+
->alias('assets._default_package', 'assets.empty_package')
45+
4446
->set('assets.context', RequestStackContext::class)
4547
->args([
4648
service('request_stack'),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,13 @@ public function testAssets()
626626
$this->assertUrlPackage($container, $defaultPackage, ['http://cdn.example.com'], 'SomeVersionScheme', '%%s?version=%%s');
627627

628628
// packages
629-
$packages = $packages->getArgument(1);
630-
$this->assertCount(9, $packages);
629+
$packageTags = $container->findTaggedServiceIds('assets.package');
630+
$this->assertCount(9, $packageTags);
631+
632+
$packages = [];
633+
foreach ($packageTags as $serviceId => $tagAttributes) {
634+
$packages[$tagAttributes[0]['package']] = $serviceId;
635+
}
631636

632637
$package = $container->getDefinition((string) $packages['images_path']);
633638
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');

src/Symfony/Component/Asset/Packages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Packages
2828
/**
2929
* @param PackageInterface[] $packages Additional packages indexed by name
3030
*/
31-
public function __construct(PackageInterface $defaultPackage = null, array $packages = [])
31+
public function __construct(PackageInterface $defaultPackage = null, iterable $packages = [])
3232
{
3333
$this->defaultPackage = $defaultPackage;
3434

src/Symfony/Component/Asset/Tests/PackagesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testGetUrl()
5151
{
5252
$packages = new Packages(
5353
new Package(new StaticVersionStrategy('default')),
54-
['a' => new Package(new StaticVersionStrategy('a'))]
54+
new \ArrayIterator(['a' => new Package(new StaticVersionStrategy('a'))])
5555
);
5656

5757
$this->assertSame('/foo?default', $packages->getUrl('/foo'));

0 commit comments

Comments
 (0)
0