Download the PHP package loophp/service-alias-autoregister-bundle without Composer
On this page you can find all versions of the php package loophp/service-alias-autoregister-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-alias-autoregister-bundle
![Latest Stable Version][latest stable version] ![GitHub stars][github stars] ![Total Downloads][total downloads] ![License][license] ![Donate!][donate github]
Service Alias Auto Register
A bundle for Symfony.
Description
The S.O.L.I.D. principles are a quintet of design guidelines aimed at enhancing the clarity, flexibility, and maintainability of software designs. Among these is the Open-Closed Principle, which advocates for the use of interfaces over concrete implementations.
In Symfony, the usual practice is to depend on concrete service implementations when injecting services, as opposed to utilizing an interface. Unfortunately, this approach compromises our code's flexibility and occasionally complicates testing.
Fortunately, this limitation can be overcome in Symfony by [manually introducing aliases][50] in the container for each class, a method that enhances flexibility and testing capabilities.
To rectify this issue and augment this practice, this bundle will declare new aliases in the Symfony container. Consequently, when a discovered service implements interfaces, aliases are automatically generated. For example, if a service implements three interfaces, the container will automatically create three new corresponding aliases.
These aliases are automatically created using the Fully Qualified Domain Name (FQDN) of the service's class when the discovered services implement one or several interfaces.
Leveraging aliases allows the injection of services using interfaces and named parameters instead of specific implementations, thereby addressing the often neglected Open-Closed Principle. In this way, our code becomes more adherent to S.O.L.I.D. principles, specifically the Open-Closed Principle, and hence more robust and easier to manage.
The following examples are showing an existing situation, as you can see, we do not respect that principle and we inject an concrete implementation directly:
When the bundle is enabled, you can inject the repository using an interface, using a specific parameter name.
We can even do better by injecting it in the constructor. Then we can use the interface when injecting, and we can use the implementation in the property. Best of both world.
Installation
See the next section to learn how to enable it in your project.
Usage
The bundle can be enabled by just adding a specific tag: autoregister.alias
Adds all the aliases it can find
Adds only specific services implementing specific interfaces only
Once it is done, do the following command to verify:
Another example: find all the new aliases for Doctrine repositories:
Configure the bundle
You can configure this bundle by creating a configuration file in your application.
The configuration keys that are available:
whitelist
: Let you configure a list of interface to use. Empty the list to whitelist them all.blacklist
: Let you configure a list of interface to ignore. Default is empty array. It takes precedence on thewhitelist
.
Contributing
Feel free to contribute by sending Github pull requests.
If you can't contribute to the code, you can also sponsor me on Github.
Changelog
See [CHANGELOG.md][47] for a changelog based on git commits.
For more detailed changelogs, please check the release changelogs.
[latest stable version]: https://img.shields.io/packagist/v/loophp/service-alias-autoregister-bundle.svg?style=flat-square [github stars]: https://img.shields.io/github/stars/loophp/service-alias-autoregister-bundle.svg?style=flat-square [total downloads]: https://img.shields.io/packagist/dt/loophp/service-alias-autoregister-bundle.svg?style=flat-square [license]: https://img.shields.io/packagist/l/loophp/service-alias-autoregister-bundle.svg?style=flat-square [donate github]: https://img.shields.io/badge/Sponsor-Github-brightgreen.svg?style=flat-square
[44]: https://tomasvotruba.com/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/
[47]: https://github.com/loophp/service-alias-autoregister-bundle/blob/master/CHANGELOG.md
[50]: https://symfony.com/doc/current/service_container.html#binding-arguments-by-name-or-type