lagdo / facades
Base classes to implement service facades.
Requires
- php: >=8.0.0
- psr/container: >=1.0.0 <3.0.0
Requires (Dev)
- league/container: ^5.0
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpcov: ^8.2
- phpunit/phpunit: ^9.5
README
Base classes for service facades
This package provides base classes for service facades implementations.
The goal of the separation between this package and the framework related ones is to make the facades portable across different frameworks. Once defined, a facade can be use without any change with various frameworks, provided that a package for this framework is available.
The following packages are currently available:
- Symfony: https://github.com/lagdo/symfony-facades
- Laravel (yes): https://github.com/lagdo/laravel-facades
- CakePHP: https://github.com/lagdo/cake-facades
- Yii: https://github.com/lagdo/yii-facades
Facades definitions
The Lagdo\Facades\AbstractFacade
abstract class is the base class for user defined service facades.
namespace App\Facades; use App\Services\MyService; use Lagdo\Facades\AbstractFacade; /** * @extends AbstractFacade<MyService> */ class MyFacade extends AbstractFacade { /** * @inheritDoc */ protected static function getServiceIdentifier(): string { return MyService::class; } }
If for any reason a service doesn't need to be fetched from the container at each call, it can be saved in its facade class by using the Lagdo\Facades\ServiceInstance
trait.
namespace App\Facades; use App\Services\MyService; use Lagdo\Facades\AbstractFacade; use Lagdo\Facades\ServiceInstance; /** * @extends AbstractFacade<MyService> */ class MyFacade extends AbstractFacade { use ServiceInstance; /** * @inheritDoc */ protected static function getServiceIdentifier(): string { return MyService::class; } }
The service container will be called only once in the above example.
MyFacade::myMethod1(); // Calls the service container MyFacade::myMethod2(); // Doesn't call the service container MyFacade::myMethod1(); // Doesn't call the service container
The Lagdo\Facades\ContainerWrapper
class gives access to the underlying container. It needs to be provided with a PSR-11
container.
use Lagdo\Facades\ContainerWrapper; ContainerWrapper::setContainer($container);
Unlike the previous, this class is meant for use in the framework related packages, and not in the user applications.
Unless the framework in use provides a way to get an instance of PSR-11
container interface that can be passed to the above call in its bootstrap process.
Contribute
- Issue Tracker: github.com/lagdo/facades/issues
- Source Code: github.com/lagdo/facades
License
The package is licensed under the 3-Clause BSD license.