Home > Backend Development > PHP Tutorial > DI Container on CakePHP upper

DI Container on CakePHP upper

Linda Hamilton
Release: 2024-12-24 06:57:23
Original
650 people have browsed it

DI Container on CakePHP upper

Motivation

I want to inject Service to Command and Controller by DI container.
In addition, Service uses Repository injected.
The document doesn't mention this case like nested injection.

Document

https://book.cakephp.org/4/en/development/dependency-injection.html

How to implement

Service and Repository

interface SomeRepository {
  public getAll(): array;
}
Copy after login
class SomeRepositoryImpl implements SomeRepository
{
    public function getAll(): array
    {
        print('getAll()');
        return [];
    }
}
Copy after login
class SomeService
{
    private $someRepository;

    public function __construct(SomeRepository $someRepository)
    {
        $this->someRepository = $someRepository;
    }

    public function doSomething(): void
    {
        $this->someRepository->getAll();
    }
}
Copy after login

Comamnd

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;

class SomeCommand extends Command
{
    private SomeService $service;

    public function __construct(SomeService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public static function getDescription(): string
    {
        return 'Inject service through provider';
    }

    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
    {
        $parser = parent::buildOptionParser($parser);
        return $parser;
    }

    public function execute(Arguments $args, ConsoleIo $io)
    {
        $this->service->doSomething();
    }
}
Copy after login
use Cake\Core\ContainerInterface;
use Cake\Core\ServiceProvider;

class CommandServiceProvider extends ServiceProvider
{
    protected $provides = [
        SomeCommand::class,
    ];

    public function services(ContainerInterface $container): void
    {
        $container
            ->add(SomeCommand::class)
            ->addArgument(SomeService::class);
    }
}

Copy after login

Controller

use Cake\Controller\Controller;

class SomeController extends Controller
{
    public function index(SomeService $service)
    {
        $service->doSomething();
        print('index()');
    }
}
Copy after login

Register to DI container

use Cake\Core\ContainerInterface;
use Cake\Core\ServiceProvider;

class SomeServiceProvider extends ServiceProvider
{
    protected $provides = [
        SomeService::class,
    ];

    public function services(ContainerInterface $container): void
    {
        $container
            ->add(SomeRepository::class, SomeRepositoryImpl::class);
        $container
            ->add(SomeService::class)
            ->addArgument(SomeRepository::class);
    }
}
Copy after login
// Application.php
class Application extends BaseApplication
{
    // ...
    public function services(ContainerInterface $container): void
    {
        $container->addServiceProvider(new SomeServiceProvider());
        $container->addServiceProvider(new ControllerServiceProvider());
    }
    // ...
Copy after login

The above is the detailed content of DI Container on CakePHP upper. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template