PHP code example of rochamarcelo / cake-pimple-di

1. Go to this page and download the library: Download rochamarcelo/cake-pimple-di library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rochamarcelo / cake-pimple-di example snippets


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


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


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;
        };
    }
}


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

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


namespace App\Controller;

use RochaMarcelo\CakePimpleDi\Di\DiTrait;

class BooksController extends AppController
{
    use DiTrait;

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


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


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();
    }
}