PHP code example of passchn / cakephp-simple-di

1. Go to this page and download the library: Download passchn/cakephp-simple-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/ */

    

passchn / cakephp-simple-di example snippets


public function services(ContainerInterface $container): void
{
    Configure::load('app_di');
    
    DIManager::create($container)
        // to add individual services: 
        ->addServices(Configure::readOrFail('DI.services'))
        /**
        * to collect multiple services, define a module: 
        * @see \Passchn\SimpleDI\Module\Module\ModuleInterface
        */
        ->addModules(Configure::readOrFail('DI.modules')) 
        /**
        * a plugin can define multiple modules: 
        * @see \Passchn\SimpleDI\Module\Plugin\PluginInterface
        */
        ->addPlugin(SomePlugin::class); 
}

return [
    'DI' => [
        'services' => [
            NewsletterService::class => NewsletterServiceFactory::class,
            CheckoutService::class => CheckoutServiceFactory::class,
            PaymentService::class => fn () => new PaymentService(),
        ],
        'modules' => [
            MyModule::class,
        ],
    ],
];

class ExamplesController {
    
    public function someAction(NewsletterService $service): Response 
    {
        $service->doSomething();
    }
}

Passchn\SimpleDI\Module\ServiceLocator\ServiceLocator::get(NewsletterService::class); // the service

// in your Application::services()
ServiceLocator::setContainer($container);