登录
首页 >  文章 >  php教程

CakePHP 上层的 DI 容器

来源:dev.to

时间:2025-01-13 19:09:07 141浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《CakePHP 上层的 DI 容器》,这篇文章主要讲到等等知识,如果你对文章相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

CakePHP 上层的 DI 容器

动机

我想通过di容器将service注入到command和controller中。
另外,service 使用 repository 注入。
文档中并没有提到嵌套注入这种情况。

文档

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

如何实施

服务和存储库

interface somerepository {
  public getall(): array;
}
class somerepositoryimpl implements somerepository
{
    public function getall(): array
    {
        print('getall()');
        return [];
    }
}
class someservice
{
    private $somerepository;

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

    public function dosomething(): void
    {
        $this->somerepository->getall();
    }
}

命令

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

控制器

use cake\controller\controller;

class somecontroller extends controller
{
    public function index(someservice $service)
    {
        $service->dosomething();
        print('index()');
    }
}

注册到di容器

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);
    }
}
// Application.php
class Application extends BaseApplication
{
    // ...
    public function services(ContainerInterface $container): void
    {
        $container->addServiceProvider(new SomeServiceProvider());
        $container->addServiceProvider(new ControllerServiceProvider());
    }
    // ...

终于介绍完啦!小伙伴们,这篇关于《CakePHP 上层的 DI 容器》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布文章相关知识,快来关注吧!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>