passchn/cakephp-simple-di
最新稳定版本:v3.4.1
Composer 安装命令:
composer require passchn/cakephp-simple-di
包简介
Simple Dependency Injection plugin for CakePHP
README 文档
README
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require passchn/cakephp-simple-di
Load the plugin:
bin/cake plugin load Passchn/SimpleDI
Usage
In your Application.php:
public function services(ContainerInterface $container): void { Configure::load('app_di'); DIManager::create($container) // to add individual services: ->addServices(Configure::readOrFail('DI.services')) /** * to collect multiple services, define a module: * @see \Passchn\SimpleDI\Module\Module\ModuleInterface */ ->addModules(Configure::readOrFail('DI.modules')) /** * a plugin can define multiple modules: * @see \Passchn\SimpleDI\Module\Plugin\PluginInterface */ ->addPlugin(SomePlugin::class); }
Then, define Factories/Modules in your app_di.php:
return [ 'DI' => [ 'services' => [ NewsletterService::class => NewsletterServiceFactory::class, CheckoutService::class => CheckoutServiceFactory::class, PaymentService::class => fn () => new PaymentService(), ], 'modules' => [ MyModule::class, ], ], ];
Factories should be Invokables or class-strings of Invokables.
You can then use the Service e.g. in Controller Actions:
class ExamplesController { public function someAction(NewsletterService $service): Response { $service->doSomething(); } }
If real DI is not possible, e.g. in ViewCells, you can use the ServiceLocator to receive the container or Services.
Passchn\SimpleDI\Module\ServiceLocator\ServiceLocator::get(NewsletterService::class); // the service
This only works if you loaded the plugin or registered the container yourself, e.g.:
// in your Application::services() ServiceLocator::setContainer($container);
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-24