2017 © Pedro Peláez
 

cakephp-plugin cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

image

rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  • Tuesday, April 24, 2018
  • by rochamarcelo
  • Repository
  • 3 Watchers
  • 10 Stars
  • 5,383 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 6 Versions
  • 13 % Grown

The README.md

CakePimpleDi - Cake 3 Plugin

A cakephp plugin for dependency injection based on Pimple 3, (*1)

Requirements

# 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., (*2)

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

# Bootstrap

Load the plugin as any other plugin:, (*3)

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

The bootstrap file must be loaded, to set up all configurations needed, (*4)

Register dependencies

In your configuration file you can define all the services:, (*5)

<?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., (*6)

  • 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", (*7)

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:, (*8)

<?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, (*9)


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., (*10)

What is Pimple?

Pimple is a simple PHP Dependency Injection Container, to more information visit: http://pimple.sensiolabs.org, (*11)

The Versions

24/04 2018

dev-master

9999999-dev https://github.com/rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp dependency injection cake3 pimple

24/04 2018

v3.0.0

3.0.0.0 https://github.com/rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp dependency injection cake3 pimple

07/04 2018

v2.1.0

2.1.0.0 https://github.com/rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp dependency injection cake3 pimple

20/12 2017

v2.0.1

2.0.1.0 https://github.com/rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp dependency injection cake3 pimple

20/12 2017

v2.0.0

2.0.0.0 https://github.com/rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp dependency injection cake3 pimple

28/08 2017

v1.0.0

1.0.0.0 https://github.com/rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp dependency injection cake3 pimple