rochamarcelo/cake-pimple-di

基于Pimple库的依赖注入的cakephp插件

安装次数: 137,576

依赖项: 0

建议者: 0

安全: 0

星标: 12

关注者: 5

分支: 3

开放问题: 0

类型:cakephp-plugin

v4.0.0 2021-10-10 19:16 UTC

This package is auto-updated.

Last update: 2024-09-11 01:37:37 UTC


README

基于 Pimple 3 的 cakephp 插件,用于依赖注入

要求

安装

要安装此插件,只需将其添加到 composer.json 的 required 部分,然后在 bootstrap 文件中加载即可。

composer require rochamarcelo/cake-pimple-di:dev-master

Bootstrap

像其他插件一样加载插件

Plugin::load('RochaMarcelo/CakePimpleDi', ['bootstrap' => true, 'routes' => false]);

必须加载 bootstrap 文件以设置所有必要的配置

注册依赖项

在配置文件中,您可以定义所有服务

<?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
            ]
        ]
    ]
];

您还可以创建一个提供者来重用您可能在其他项目中使用的某些服务。

  • 因此,创建一个实现 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;
        };
    }
}
  • 然后,在配置文件中定义
<?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
            ]
        ]
    ]
];

加载依赖项 - 基础

获取共享实例,然后调用 "get" 方法

use RochaMarcelo\CakePimpleDi\Di\Di;
$finder = Di::instance()->get('LibraryApp\Finder');

加载依赖项 - 使用 DiTrait

namespace App\Controller;

use RochaMarcelo\CakePimpleDi\Di\DiTrait;

class BooksController extends AppController
{
    use DiTrait;

    public function index()
    {
        $finder = $this->di()->get('LibraryApp\Finder');
    }
}

加载依赖项 - 使用 InvokeActionTrait 注入

在配置文件中

<?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
            ],          
        ]
    ]
];

在控制器中使用 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();
    }
}

将 CakePHP 请求和会话对象添加到容器中

要获取添加到容器中的会话和请求对象,只需在配置中将键 'useRequest' 的值设置为布尔值 true 即可。

什么是 Pimple?

Pimple 是一个简单的 PHP 依赖注入容器,更多信息请访问: http://pimple.sensiolabs.org