Skip to content

Commit

Permalink
feature #30645 Alias for each assets package (gpenverne)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3-dev branch.

Discussion
----------

Alias for each assets package

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | ?     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | <!-- required for new features -->

Add autowiring by type + name on assets packages
```yaml
framework:
    assets:
        packages:
            xxx:
                base_urls: 'xxxx'
```
```php
<?php

class MyService
{
    private $xxxPackage;

    public function __construct(PackageInterface $xxxPackage)
    {
        $this->xxxPackage = $xxxPackage;
        ...
    }

    public function myMethod(): string
    {
        return $this->xxxPackage->getUrl('some-image.png');
    }
}
```
instead of:
```php
<?php

class MyService
{
    private $packages;

    public function __construct(Packages $packages)
    {
        $this->packages = $packages;
        ...
    }

    public function myMethod(): string
    {
        return $this->packages->getPackage('xxx')->getUrl('some-image.png');
    }
}
```

Commits
-------

e8b9856 Alias for each assets package
  • Loading branch information
fabpot committed Mar 27, 2019
2 parents 8952c02 + e8b9856 commit 0c02a40
Showing 1 changed file with 2 additions and 0 deletions.
Expand Up @@ -23,6 +23,7 @@
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
use Symfony\Bundle\FullStack;
use Symfony\Component\Asset\PackageInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\AdapterInterface;
Expand Down Expand Up @@ -996,6 +997,7 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
}

$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
$container->registerAliasForArgument('assets._package_'.$name, PackageInterface::class, $name.'.package');
$namedPackages[$name] = new Reference('assets._package_'.$name);
}

Expand Down

0 comments on commit 0c02a40

Please sign in to comment.