lotos/container

2.0.0 2022-03-05 18:08 UTC

This package is auto-updated.

Last update: 2024-04-05 22:38:57 UTC


README

Установка

composer require lotos/container

Установка зависимостей

$collection = CollectionFactory::createCollection();
$repository = RepositoryFactory::createRepository($collection->newInstance());

$container = ContainerFactory::createContainer(
    repository: $repository,
    builder: BuilderFactory::createBuilder(
        repository: $repository,
        collection: $collection
    )
);

$container->saveClass(Lotos\Http\ServerRequestFactory::class)
    ->forInterface(Psr\Http\Message\ServerRequestFactoryInterface::class)
    ->withAlias('serverRequestFactory');

$container->saveClass(Lotos\Http\Strategies\JsonStrategy::class)
    ->forInterface(Lotos\Http\StrategyInterface::class)
    ->withPriority(1)
    ->withAlias('jsonStrategy');

$container->saveClass(Lotos\Http\Strategies\HtmlStrategy::class)
    ->forInterface(Lotos\Http\StrategyInterface::class)
    ->withAlias('htmlStrategy');

Получение объекта из контейнера

$router = $container->get('router');

Получение результата работы метода объекта, хранящегося в контейнере

$container->saveClass(Lotos\Http\ServerRequest\ServerRequest::class)
    ->forInterface(Psr\Http\Message\ServerRequestInterface::class)
    ->setInstance(
        $container->call('serverRequestFactory', 'fromGlobals')
    );

Инъекции зависимостей

ВНИМАНИЕ!

В текущей версии была удалена возможность устанавливать инъекции в свойства объектов, например:

class Example
{
    /**
     * @var Psr\Http\Message\RequestInterface
     * */
    private RequestInterface $request;
}

теперь работать не будет и нужно явно указать в какой медот нужно внедрить инъекцию:

use Psr\Http\Message\{
    RequestInterface,
    ResponseInterface,
    ResponseFactoryInterface
};

class User
{
    public function __construct(
        private ResponseFactoryInterface $responseFactory
    )
    {}

    public function create(RequestInterface $request) : ResponseInterface
    {
        //...
    }

}