rochamarcelo/cake-pimple-di
Composer 安装命令:
composer require rochamarcelo/cake-pimple-di
包简介
A cakephp plugin for dependency injection based on Pimple library
README 文档
README
A cakephp plugin for dependency injection based on Pimple 3
Requirements
- CakePHP 3.X
- Pimple 3
Installation
To install this plugin you'll only have to add the plugin to your required section of composer.json then load in your bootstrap file.
composer require rochamarcelo/cake-pimple-di:dev-master
Bootstrap
Load the plugin as any other plugin:
Plugin::load('RochaMarcelo/CakePimpleDi', ['bootstrap' => true, 'routes' => false]);
The bootstrap file must be loaded, to set up all configurations needed
Register dependencies
In your configuration file you can define all the services:
<?php return [ ... 'CakePimpleDi' => [ 'actionInjections' => [ '\App\Controller\BooksController' => [ 'index' => ['LibraryApp\Finder'],//should be defined in services 'view' => ['LibraryApp\Finder', 'random_func']//should be defined in services ] ], 'services' => [ 'LibraryApp\Client' => function() {//each time you get that service, will returns the same instance return new \Cake\Network\Http\Client; }, 'LibraryApp\Finder' => function($c) {//each time you get that service, will returns the same instance $finder = new \LibraryApp\Finder\SimpleFinder( $c['LibraryApp\Client'] ); return $finder; }, 'random_func' => [ 'value' => function () { return rand(); }, 'type' => 'parameter'//when you get that service, will return the original closure ], 'cookie_name' => 'SESSION_ID', [ 'id' => 'something', 'value' => function () { $std = new \stdClass; $std->rand = rand(); return $std; }, 'type' => 'factory'//will return a different instance for all calls ] ] ] ];
You could also create a provider to reuse some services that you may use in other projects.
- So create the provider that implements Pimple\ServiceProviderInterface:
<?php namespace App\Di; use Pimple\Container; use Pimple\ServiceProviderInterface; class LibraryAppProvider implements ServiceProviderInterface { public function register(Container $pimple) { $pimple['LibraryApp\Client'] = function() {//each time you get that service, will returns the same instance return new \Cake\Network\Http\Client; }; $pimple['LibraryApp\Finder'] = function($c) {//each time you get that service, will returns the same instance $finder = new \LibraryApp\Finder\SimpleFinder( $c['LibraryApp\Client'] ); return $finder; }; } }
- Then, define in your configuration file:
<?php return [ ... 'CakePimpleDi' => [ 'providers' => [ 'App\Di\LibraryAppProvider' ], 'services' => [ 'random_func' => [ 'value' => function () { return rand(); }, 'type' => 'parameter'//when you get that service, will return the original closure ] ] ] ];
Loading dependency - Basic
Get the shared instance then call method "get"
use RochaMarcelo\CakePimpleDi\Di\Di; $finder = Di::instance()->get('LibraryApp\Finder');
Loading dependency - With DiTrait
namespace App\Controller; use RochaMarcelo\CakePimpleDi\Di\DiTrait; class BooksController extends AppController { use DiTrait; public function index() { $finder = $this->di()->get('LibraryApp\Finder'); } }
Loading dependency - Injections with InvokeActionTrait
In your configuration file:
<?php return [ ... 'CakePimpleDi' => [ 'actionInjections' => [ '\App\Controller\BooksController' => [ 'index' => ['LibraryApp\Finder'],//should be defined in services 'view' => ['LibraryApp\Finder', 'random_func']//should be defined in services ] ], 'services' => [ 'LibraryApp\Client' => function() {//each time you get that service, will returns the same instance return new \Cake\Network\Http\Client; }, 'LibraryApp\Finder' => function($c) {//each time you get that service, will returns the same instance $finder = new \LibraryApp\Finder\SimpleFinder( $c['LibraryApp\Client'] ); return $finder; }, 'random_func' => [ 'value' => function () { return rand(); }, 'type' => 'parameter'//when you get that service, will return the original closure ], ] ] ];
In your controller use the InvokeActionTrait
use RochaMarcelo\CakePimpleDi\Di\InvokeActionTrait; class MyControllerController extends AppController { use InvokeActionTrait; public function view(\CakeFinder $finder, $rand, $id = null) { $finder->find(); ..... } public function index($finder) { $finder->find(); $something->doSomething(); } }
Adding The CakePHP Request and Session Objects to The Container
To get the Session and Request objects added to the container just set the key 'useRequest' with the value boolean true in the configuration.
What is Pimple?
Pimple is a simple PHP Dependency Injection Container, to more information visit: http://pimple.sensiolabs.org
rochamarcelo/cake-pimple-di 适用场景与选型建议
rochamarcelo/cake-pimple-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 181.9k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2015 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency」 「plugin」 「cakephp」 「injection」 「pimple」 「cake3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rochamarcelo/cake-pimple-di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rochamarcelo/cake-pimple-di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rochamarcelo/cake-pimple-di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
A fast and intuitive dependency injection container.
Email Toolkit Plugin for CakePHP
Rhino, a CMS and Application-Framework plugin for CakePHP
A CakePHP plugin for sending mail via SparkPost's REST API
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 181.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-17