marcbln/symfony-facades
Composer 安装命令:
composer require marcbln/symfony-facades
包简介
Call Symfony services using facades.
README 文档
README
This is a fork from lagdo/symfony-facades with support for symfony 6.
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 each of them only occasionally.
Installation
Install the package with composer.
composer require marcbln/symfony-facades
If the project is not using Symfony Flex, then register the Marcbln\Symfony\Facades\FacadesBundle bundle in the src/Kernel.php file.
Usage
A facade inherits from the Marcbln\Symfony\Facades\AbstractFacade abstract class, and implements the getServiceIdentifier() method, which returns the id of the corresponding service.
namespace App\Facades; use App\Services\MyService; use Marcbln\Symfony\Facades\AbstractFacade; class MyFacade extends AbstractFacade { /** * @inheritDoc */ protected static function getServiceIdentifier() { return MyService::class; } }
The methods of the App\Services\MyService service can now be called using the App\Facades\MyFacade facade, like this.
class TheService { public function theMethod() { MyFacade::myMethod(); } }
Instead of this.
class TheService { /** * @var MyService */ protected $myService; public function __construct(MyService $myService) { $this->myService = $myService; } public function theMethod() { $this->myService->myMethod(); } }
Using a service locator
The above facade will work only for services that are declared as public.
A service locator must be declared in the config/services.yaml file, in order to create facades for private services.
See the Symfony service locators documentation.
In the following example, the Twig service is passed to the service locator.
marcbln.facades.service_locator: public: true class: Symfony\Component\DependencyInjection\ServiceLocator tags: ['container.service_locator'] arguments: - Twig\Environment: '@twig'
A facade can then be defined for the Twig service.
namespace App\Facades; use Twig\Environment; use Marcbln\Symfony\Facades\AbstractFacade; class View extends AbstractFacade { /** * @inheritdoc */ protected static function getServiceIdentifier() { return Environment::class; } }
Templates can now be rendered using the facade.
use App\Facades\View; class TheService { public function theMethod() { ... $html = View::render($template, $vars); ... } }
Provided facades
This package provides facades for some Symfony services.
Logger
The logger service must be passed to the service locator in the config/services.yaml file.
marcbln.facades.service_locator: public: true class: Symfony\Component\DependencyInjection\ServiceLocator tags: ['container.service_locator'] arguments: - Psr\Log\LoggerInterface: '@logger'
Messages can now be logged using the facade.
use Marcbln\Symfony\Facades\Log; Log::info($message, $vars);
View
The twig service must be passed to the service locator in the config/services.yaml file.
marcbln.facades.service_locator: public: true class: Symfony\Component\DependencyInjection\ServiceLocator tags: ['container.service_locator'] arguments: - Twig\Environment: '@twig'
Views can now be rendered using the facade.
use Marcbln\Symfony\Facades\View; $html = View::render($template, $vars);
Contribute
- Issue Tracker: github.com/marcbln/symfony-facades/issues
- Source Code: github.com/marcbln/symfony-facades
License
The package is licensed under the 3-Clause BSD license.
统计信息
- 总下载量: 1.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-01-23