streamcommon/factory-container-interop

Default factory with PSR-11 container

This package's canonical repository appears to be gone and the package has been frozen as a result.

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

This package provide default FactoryInterface for PSR-11 standard.

Branches

Master Build Status Coverage Status

Develop Build Status Coverage Status

Installation

Console run:

    composer require streamcommon/factory-container-interop

Or add into your composer.json:

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

Example

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