eryw/pastry-bag

A plugin for CakePHP 3 provides the constructor dependencies injection and the controller method injection

Installs: 61

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 0

Open Issues: 0

Type:cakephp-plugin

v1.0.1 2016-09-25 16:29 UTC

This package is not auto-updated.

Last update: 2024-04-11 01:00:56 UTC


README

Packagist Travis branch Latest Stable Version

Dependencies injection plugin for CakePHP 3. This plugin provides a constructor injection and a controller action injection.

Installation

Composer

composer require eryw/pastry-bag=@stable

Configuration

Add the following line to your config/bootstrap.php:

Plugin::load('PastryBag', ['bootstrap' => true]);
For CakePHP 3.2 with dispatch filter

Replace your controller factory filter in config/bootstrap.php:

DispatcherFactory::add('ControllerFactory');

With the following:

DispatcherFactory::add('PastryBag\Routing\Filter\ControllerFactoryFilter');
For CakePHP 3.3 with middleware

Please override method getDispatcher() on your Application class

class Application extends BaseApplication
{
    // ... //
    
    protected function getDispatcher()
    {
        return new ActionDispatcher(new ControllerFactory(), null, DispatcherFactory::filters());
    }
}

To make the dependency injection works, all controller must extends PastryBag\Controller\Controller.

So, change your AppController parent class from Cake\Controller\Controller into PastryBag\Controller\Controller:

class AppController extends \PastryBag\Controller\Controller
{
    // ... //
}

Usage

Constructor Injection

class UsersController extends AppController
{
    protected $payment;

    public function __construct(PaymentService $payment)
    {
        parent::__construct();
        $this->payment = $payment;
    }
    
    public function payBill()
    {
        // `$this->payment` will auto injected with instance of PaymentService
        $this->payment->anyMethodOfPaymentService();
    }
}

The controller method injection

class RemoteGaleryController extends AppController
{
    public function index($id, MyHttpClient $client)
    {
        // `$client` will auto injected with instance of MyHttpClient
        $client->request('GET', 'http://remotesite.com');
    }
}
Note:

Only the type hinted parameters will auto injected.

Config

This plugin uses Aura.Di as container. Configuration should be put inside a class that implements Aura\Di\ContainerConfigInterface and the list of configuration (class name or instance) must be put at config/container_configs.php

Example content of config/container_configs.php:

use Aura\Di\Container;
use Aura\Di\ContainerConfig;
use Cake\ORM\TableRegistry;

// OPTIONAL. You can move this class to other file if you want
class DiConfig extends ContainerConfig
{
    public function define(Container $di)
    {
        $di->set(\App\Model\Table\UsersTable::class, $di->lazy(function () {
            return TableRegistry::get('Users');
        }));
        $di->types[\App\Model\Table\UsersTable::class] = $di->lazyGet(\App\Model\Table\UsersTable::class);
    }
}

// REQUIRED. This file must return list of configs as array
return [
    'My\Awesome\App\ClassImplementsAuraDiContainerConfigInterface',
    new DiConfig,
];

For advance container configuration and usage, please check Aura.Di official documentation

If you want to access the instance of container directly, you can use static method PastryBag::getContainer():

// `$di` is instance of Aura\Di\Container
$di = PastryBag::getContainer();

This plugin is inspired by PipingBag plugin but does not require annotations.