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.
Installation
Install the package with composer
.
For Symfony version 6. or older, install the version 2.3 of the package.
For Symfony version 7., install the version 3.0.
Register the Lagdo\Symfony\Facades\FacadesBundle
bundle in the config/bundles.php
file.
Usage
A facade inherits from the Lagdo\Symfony\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
Starting from version 2.0.0, the instance()
method returns the instance of the service.
The ServiceInstance
trait
By default, each call to a facade method will also call the Symfony service container.
Starting from version 2.2.0, the service instance can be saved in the facade after the first call to the Symfony service container, using the ServiceInstance
trait.
The next calls with return the service instance without calling the Symfony service container.
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.