ellipse/resolvable-class

支持使用Psr-11容器实例化类的可解析类工厂

1.0.7 2018-02-23 10:01 UTC

This package is auto-updated.

Last update: 2024-08-25 23:27:19 UTC


README

此包提供了一个可解析类工厂,允许使用Psr-11容器实例化类。

要求 php >= 7.0

安装 composer require ellipse/resolvable-class

运行测试 ./vendor/bin/kahlan

解析一个类

此包提供了一个工厂,可以从类名生成Ellipse\Resolvable\ResolvableClass的实例。这些可解析类实例可以通过使用Psr-11容器和一组占位符值调用->value()方法来生成。

<?php

namespace App;

class SomeClass
{
    public function __construct(SomeOtherClass $p1, int $p2 = 0, int $p3, string $p4 = 'p4')
    {
        // $p1 is the instance returned by $container->get(SomeOtherClass::class);
        // $p2 value is 2
        // $p3 value is 3
        // $p3 value is 'p4'
    }
}
<?php

namespace App;

use Some\Psr11Container;

use Ellipse\Resolvable\DefaultResolvableClassFactory;

// Some Psr-11 container.
$container = new Psr11Container;

// Resolve SomeClass. $resolved value is an instance of SomeClass.
$factory = new DefaultResolvableClassFactory;

$resolved = $factory(SomeClass::class)->value($container, [2, 3]);