如何将依赖注入容器合并到此项目中

时间:2016-09-15 02:50:55

标签: php

我已经尝试了好几天才能让它发挥作用。

我正在使用这个框架:https://github.com/DennisSkoko/discord-bot来创建一个机器人。但我不想将DIC容器http://container.thephpleague.com/添加到此项目中。

我想要做的是在Main类中注册服务提供者(模块/包/包)或者你想要调用它们的任何东西,就像Laravel等一样。

所以我创建了一个模块/目录并添加了一个serviceprovider类和一些其他类作为文档状态:http://container.thephpleague.com/service-providers/

首先我编辑了start.php以注入容器:

use League\Container\Container;

require "vendor/autoload.php";
require "bot.php";

$container = new Container;

$main = new DS\Main(new Example($container));
$main->run();

我更改了示例机器人以包含一个getContainer()函数,因此我可以从Main类中读取它,如下所示:

use DS\Bot;
use Discord\Discord;
use DS\Service;

class Example extends Bot
{
    /**
     * Example constructor
     */
    public function __construct($container)
    {
        $this->config = [
            "token" => "My token"
        ];

        $this->container = $container;
    }


    /**
     * Will be executed when the WebSocket is ready and before the services are implemented.
     *
     * @param Discord $discord
     * 
     * @return void
     */
    public function setup(Discord $discord)
    {
        echo "Example is ready to start!";


    }


    public function getContainer()
    {
        return $this->container;
    }


    /**
     * Will return an array of Service that the bot uses.
     *
     * @return Service[]
     */
    public function getServices()
    {
        return [];
    }
}

然后我将Main类更改为注册我的服务提供者。

namespace DS;

use Discord\Discord;

/**
 * A class that bring bots script into life.
 */
class Main
{
    /**
     * @var Discord
     */
    private $discord;

    /**
     * @var Bot
     */
    private $bot;


    /**
     * Main constructor.
     *
     * @param Bot $bot
     */
    public function __construct(Bot $bot)
    {
        $this->bot = $bot;

        $this->discord = new Discord($this->bot->getConfig());

        $this->discord->on("ready", function ($discord) {
            $this->bot->setup($discord);
            $this->setServices($this->bot->getServices());
        });

        $this->container = $this->bot->getContainer();
        $this->container->addServiceProvider('Mynamespace\HelloWorld\ServiceProvider');

    }


    /**
     * Will run the bot.
     *
     * @return void
     */
    public function run()
    {
        $this->discord->run();
    }


    /**
     * Will add the services for the WebSocket.
     *
     * @param Service[] $services
     *
     * @return void
     */
    private function setServices($services)
    {
        foreach ($services as $service) {
            $this->discord->on($service->getEvent(), $service->getListener());
        }
    }
}

问题在于:

PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'A service provider must be a fully qualified class name or instance of (\League\Container\ServiceProvider\ServiceProviderInterface)' 

这很奇怪,因为我就像在文档和扩展的League \ Container \ ServiceProvider \ AbstractServiceProvider

中那样做了

我已经进行了20次双重检查。

所以我不知道该怎么做。如何在Main类中使用addServiceProvider()并在DIC中注册?

如果可能的话,我真的不想在Example类中这样做。因为这应该由用户扩展,Main类将引导机器人。

我也尝试了$ this-> bot-> addServiceProvider()但是我收到以下错误:

PHP Fatal error:  Call to undefined method Example::addServiceProvider()

任何帮助表示感谢。

0 个答案:

没有答案