Download the PHP package lagdo/symfony-facades without Composer
On this page you can find all versions of the php package lagdo/symfony-facades. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package symfony-facades
Facades for Symfony services
With this package, Symfony services can be called using facades, with static method syntax.
It is a simpler alternative to passing services as parameters in the constructors of other classes, or using lazy services. It will be especially interesting in the case when a class depends on many services, but calls some of them only occasionally.
I have published this two parts articles to explain why and when to use service facades in a Symfony application.
- Part one: https://medium.com/@thierry.feuzeu/using-service-facades-in-a-symfony-application-part-1-971867d74ab5
- Part two: https://medium.com/@thierry.feuzeu/using-service-facades-in-a-symfony-application-part-2-9a3804afdff2
Facades definition
The base classes for service facade definitions are provided by the lagdo/facades package.
A service facade based on this package can be use without any change with other frameworks, if a package for this framework is available, or a PSR-11 container can be provided.
The following packages are also available:
- Laravel (yes): https://github.com/lagdo/laravel-facades
- CakePHP: https://github.com/lagdo/cake-facades
- Yii: https://github.com/lagdo/yii-facades
Installation
Install the package with composer.
For Symfony version 6. or older, install the version 2.3 of the package.
For Symfony version 7. and newer, install the version 3.*.
Register the Lagdo\Symfony\Facades\FacadesBundle bundle in the config/bundles.php file.
Usage
[!NOTE] If you are migrating from an older version to 3.1.0 or 2.4.0, please keep in mind that some classes have been moved or renamed.
A facade inherits from the Lagdo\Facades\AbstractFacade abstract class, and implements the getServiceIdentifier() method, which must return the id of the corresponding service in the Symfony service container.
The methods of the App\Services\MyService service can now be called using the App\Facades\MyFacade facade, like this.
Instead of this.
The @extends AbstractFacade<MyService> phpdoc will prevent errors during code analysis with PHPStan, and allow code completion on calls to facades in editors.
Using a service locator
The above facade will work only for services that are declared as public.
In order to call private services with facades, a service locator with id lagdo.facades.service_locator must be declared in the config/services.yaml file.
See the Symfony service locators documentation.
The private services that need to be accessed with a facade must be passed as arguments to the service locator. For each argument, the key is the service id in the facade, while the value is the service id in the container.
In the following example, the Twig service is passed to the service locator.
A facade can then be defined for the Twig service.
Templates can now be rendered using the facade.
The lagdo.facades.service tag
Starting from version 2.3.0, the private services that need to be accessed with a facade can be tagged with lagdo.facades.service.
These services will then be automatically passed to the service locator, together with those received as arguments.
In the following example, the Twig and App\Services\TaggedService services will be passed to the service locator.
A facade can then be defined for the service.
Getting the service instance
The instance() method of a service facade returns the instance of the linked service.
The Lagdo\Facades\ServiceInstance trait
By default, each call to a facade method will also call the Symfony service container.
The service instance can be saved in the facade after the first call to the Symfony service container, using the Lagdo\Facades\ServiceInstance trait.
The next calls with return the service instance without calling the Symfony service container.
[!IMPORTANT] The
Lagdo\Facades\ServiceInstancetrait must be used in each service facade class, and not in a parent class. The same instance will be shared by all the classes inheriting the same base class using the trait, and the service facades will ot work as expected.
The Symfony service container is called only once in this example code.
Provided facades
This package includes facades for some Symfony services.
Logger
The logger service must be passed to the service locator in the config/services.yaml file.
Messages can now be logged using the facade.
View
The twig service must be passed to the service locator in the config/services.yaml file.
Views can now be rendered using the facade.
Contribute
- Issue Tracker: github.com/lagdo/symfony-facades/issues
- Source Code: github.com/lagdo/symfony-facades
License
The package is licensed under the 3-Clause BSD license.
All versions of symfony-facades with dependencies
lagdo/facades Version ^1.0
psr/container Version ^2.0
symfony/framework-bundle Version 7.*|8.*
