PastryBag
, (*1)
Dependencies injection plugin for CakePHP 3. This plugin provides a constructor injection and a controller action injection., (*2)
Installation
Composer
composer require eryw/pastry-bag=@stable
Configuration
Add the following line to your config/bootstrap.php
:, (*3)
Plugin::load('PastryBag', ['bootstrap' => true]);
For CakePHP 3.2 with dispatch filter
Replace your controller factory filter in config/bootstrap.php
:, (*4)
DispatcherFactory::add('ControllerFactory');
With the following:, (*5)
DispatcherFactory::add('PastryBag\Routing\Filter\ControllerFactoryFilter');
For CakePHP 3.3 with middleware
Please override method getDispatcher()
on your Application
class, (*6)
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
., (*7)
So, change your AppController
parent class from Cake\Controller\Controller
into PastryBag\Controller\Controller
:, (*8)
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., (*9)
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
, (*10)
Example content of config/container_configs.php
:, (*11)
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, (*12)
If you want to access the instance of container directly, you can use static method PastryBag::getContainer()
:, (*13)
// `$di` is instance of Aura\Di\Container
$di = PastryBag::getContainer();
This plugin is inspired by PipingBag plugin but does not require annotations., (*14)