php - 如何在 Laravel 之外使用 Laravel 的 IOC 容器进行方法注入(inject)

标签 php dependency-injection illuminate-container laravel-5

短篇小说: 我无法使用 composer ( https://packagist.org/packages/illuminate/container ) 安装的 Laravel 容器进行方法注入(inject)。注入(inject)仅在对象的构造函数中使用时才有效。例如:

class SomeClass {
    function __construct(InjectedClassWorksHere $obj) {}
    function someFunction(InjectedClassFailsHere $obj) {}
}

长话短说: 我正在考虑重构一个主要项目以使用 Laravel,但由于业务压力,我无法投入我想要的时间。为了不把“婴儿和洗澡水一起扔掉”,我正在使用单独的 Laravel 组件来提升旧分支中正在开发的代码的优雅。在评估 Laravel 时我最喜欢的新技术之一是依赖注入(inject)的概念。我很高兴后来发现我可以在 Laravel 项目之外使用它。我现在可以正常工作,一切都很好,除了在网上找到的容器的开发版本似乎不支持方法注入(inject)。

有没有其他人能够让容器工作并在 Laravel 项目之外进行方法注入(inject)?

到目前为止我的方法...

Composer .json

"illuminate/support": "5.0.*@dev",
"illuminate/container": "5.0.*@dev",

应用引导代码:

use Illuminate\Container\Container;

$container = new Container();
$container->bind('app', self::$container); //not sure if this is necessary

$dispatcher = $container->make('MyCustomDispatcher');
$dispatcher->call('some URL params to find controller');

通过上述,我可以注入(inject)我的 Controller 的构造函数,但不能注入(inject)它们的方法。我错过了什么?

完整源代码... (C:\workspace\LMS>php cmd\test_container.php)

<?php

// This sets up my include path and calls the composer autoloader
require_once "bare_init.php";

use Illuminate\Container\Container;
use Illuminate\Support\ClassLoader;
use Illuminate\Support\Facades\Facade;

// Get a reference to the root of the includes directory
$basePath = dirname(dirname(__FILE__));

ClassLoader::register();
ClassLoader::addDirectories([
    $basePath
]);

$container = new Container();
$container->bind('app', $container);
$container->bind('path.base', $basePath);

class One {
    public $two;
    public $say = 'hi';
    function __construct(Two $two) {
        $this->two = $two;
    }
}

Class Two {
    public $some = 'thing';
    public function doStuff(One $one) {
        return $one->say;
    }
}

/* @var $one One */
$one = $container->make(One);
var_dump($one);
print $one->two->doStuff();

当我运行上面的代码时,我得到...

C:\workspace\LMS>php cmd\test_container.php
object(One)#9 (2) {
  ["two"]=>
  object(Two)#11 (1) {
    ["some"]=>
    string(5) "thing"
  }
  ["say"]=>
  string(2) "hi"
}

PHP Catchable fatal error:  Argument 1 passed to Two::doStuff() must be an instance of One, none 
given, called in C:\workspace\LMS\cmd\test_container.php on line 41
and defined in C:\workspace\LMS\cmd\test_container.php on line 33

Catchable fatal error: Argument 1 passed to Two::doStuff() must be an instance of One, none  
given, called in C:\workspace\LMS\cmd\test_container.php on line 41 and
defined in C:\workspace\LMS\cmd\test_container.php on line 33

或者,一个更基本的例子,说明注入(inject)在构造函数而不是方法中工作......

class One {
    function __construct(Two $two) {}
    public function doStuff(Three $three) {}
}

class Two {}
class Three {}

$one = $container->make(One); // totally fine. Injection works
$one->doStuff(); // Throws Exception. (sad trombone)

最佳答案

只需将 One 的实例传递到您对 Two 的调用中:

$one = $container->make('One');
var_dump($one);
print $one->two->doStuff($one);

返回...

object(One)#8 (2) {
  ["two"]=>
  object(Two)#10 (1) {
    ["some"]=>
    string(5) "thing"
  }
  ["say"]=>
  string(2) "hi"
}
hi

更新:进一步研究后更正的答案

如下所述,在 Laravel 5.0 中,方法注入(inject)仅适用于路由和 Controller 。因此,您也可以将它们拉入您的项目,并在此过程中获得更多 Laravel-y。方法如下:

composer.json中,需要在illuminate/routingilluminate/events中添加:

{
    "require-dev": {
        "illuminate/contracts": "5.0.*@dev",
        "illuminate/support": "5.0.*@dev",
        "illuminate/container": "5.0.*@dev",
        "illuminate/routing": "5.0.*@dev",
        "illuminate/events": "5.0.*@dev"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

routing.php 中,设置 Laravel 的路由和 Controller 服务:

/**
 * routing.php
 *
 * Sets up Laravel's routing and controllers
 *
 * adapted from http://www.gufran.me/post/laravel-components
 * and http://www.gufran.me/post/laravel-illuminate-router-package-in-your-application
 */
$basePath = str_finish(dirname(__FILE__), '/app/');

$controllersDirectory = $basePath . 'Controllers';

// Register directories into the autoloader
Illuminate\Support\ClassLoader::register();
Illuminate\Support\ClassLoader::addDirectories($controllersDirectory);

// Instantiate the container
$app = new Illuminate\Container\Container();
$app['env'] = 'production';

$app->bind('app', $app); // optional
$app->bind('path.base', $basePath); // optional

// Register service providers
with (new Illuminate\Events\EventServiceProvider($app))->register();
with (new Illuminate\Routing\RoutingServiceProvider($app))->register();

require $basePath . 'routes.php';

$request = Illuminate\Http\Request::createFromGlobals();
$response = $app['router']->dispatch($request);
$response->send();

Controllers/One.php中,创建类作为Controller,这样我们就可以使用L5的方法注入(inject):

/**
 * Controllers/One.php
 */
Class One extends Illuminate\Routing\Controller {
    public $some = 'thingOne';
    public $two;
    public $three;

    function __construct(Two $two) {
        $this->two = $two;
        echo('<pre>');
        var_dump ($two);
        echo ($two->doStuffWithTwo().'<br><br>');
    } 

    public function doStuff(Three $three) {
        var_dump ($three);
        return ($three->doStuffWithThree());
    }
}

routes.php中,定义我们的测试路由:

$app['router']->get('/', 'One@dostuff');

最后,在 index.php 中,启动一切并定义我们的类来测试依赖注入(inject):

/**
 * index.php
 */

// turn on error reporting
ini_set('display_errors',1);  
error_reporting(E_ALL);

require 'vendor/autoload.php';
require 'routing.php';

// the classes we wish to inject
Class Two {
    public $some = 'thing Two';
    public function doStuffWithTwo() {
        return ('Doing stuff with Two');
    }
}
Class Three {
    public $some = 'thing Three';
    public function doStuffWithThree() {
        return ('Doing stuff with Three');
    }
}

点击 index.php,你应该得到这个:

object(Two)#40 (1) {
  ["some"]=>
  string(9) "thing Two"
}
Doing stuff with Two

object(Three)#41 (1) {
  ["some"]=>
  string(11) "thing Three"
}
Doing stuff with Three

一些笔记...

  • 无需显式绑定(bind)类。 Laravel 负责这件事。
  • 现在你有了 Laravel 的路由和 Controller 的额外好处
  • 之所以可行,是因为现在我们不必调用 $one->doStuff();,并使用会引发异常的空参数(因为 doStuff 需要个实例)。相反,路由器调用 doStuff 并为我们解析 IoC 容器。
  • 归功于 http://www.gufran.me/post/laravel-illuminate-router-package-in-your-application它会引导您完成所有这些,这似乎是上面提到的 Matt Stauffer 项目的灵感来源。两者都非常酷,值得一读。

关于php - 如何在 Laravel 之外使用 Laravel 的 IOC 容器进行方法注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25837941/

相关文章:

php - laravel 之外的 Laravel 缓存

php - 有没有办法判断字体是否支持 Imagick 中的给定字符?

php - Laravel 6 config()->get ('database.connections.mysql' ) 不匹配 DB :connection()

php - 在不使用 mysqli 的情况下使用 PHP 和 MySQL 准备语句

c++ - iOS 和 C++ MVC 策略

android - 如何修复 Koin lib 中的 "No compatible definition found for type ' 上下文?

c# - 在有很多类需要动态生成其他类的项目中进行依赖注入(inject)

php - 照亮库胶囊对象不接受 'addConnection' 命令

php - 尝试在PHP中将YouTube网址回显到iframe中

PHP PDO 语法错误或访问冲突 : 1064