streamcommon/factory-container-interop

默认带有PSR-11容器的工厂

此软件包的官方仓库似乎已不存在,因此该软件包已被冻结。

1.0.3 2019-02-16 19:55 UTC

This package is auto-updated.

Last update: 2019-12-08 20:34:23 UTC


README

Latest Stable Version Total Downloads License

此软件包为PSR-11标准提供默认的FactoryInterface

分支

Master Build Status Coverage Status

Develop Build Status Coverage Status

安装

控制台运行

    composer require streamcommon/factory-container-interop

或者添加到你的composer.json

    "require": {
        "streamcommon/factory-container-interop": "*"
    }

示例

use Psr\Container\ContainerInterface;
use Streamcommon\Factory\Container\Interop\{FactoryInterface, CallableFactoryTrait};
use Streamcommon\Factory\Container\Interop\Exception\NotFoundException;

class FooFactory implements FactoryInterface
{
    // if you want used as static factory [FooFactory::class, 'name']
    use CallableFactoryTrait;
    
    /**
     * Create an object
     *
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param null|array $options
     * @return object
     * @throws NotFoundException A call to the get method with a non-existing object
     */
    public function __invoke(ContainerInterface $container, string $requestedName, ?array $options = null): object
    {
        if (!class_exists($requestedName)) {
            throw new NotFoundException();
        }
        return new $requestedName($options);
    }
}