borodulin/container

dev-master 2020-05-18 10:43 UTC

This package is auto-updated.

Last update: 2024-10-18 20:28:27 UTC


README

Build Status Code Coverage Scrutinizer Code Quality Code Intelligence Status

Features

  • PSR-11 full featured implementation.
  • Allows autowire via file finder and/or configuration file.
  • Allows delegate lookup and resolve dependencies from other containers.
  • Allows scalar types injection via parameters bag.
  • Detects circular references.
  • Supports aliasing.
  • Supports closures.
  • Supports PSR-16 cache.

Using container

Container can be initialized with file finder.

// autowire all classes in Samples directory
$fileFinder = (new \Borodulin\Finder\ClassFinder())
    ->addPath(__DIR__.'/../Samples');
// build container
$container = (new \Borodulin\Container\ContainerBuilder())
    ->setClassFinder($fileFinder)
    ->build();

Container can be initialized via configuration file.

./config/definitions.php:

<?php
return [
    'test.bar' => Bar::class,
    'test.foo' => function (Bar $bar) {
        return new Foo($bar);
    },
];
// build container
$container = (new \Borodulin\Container\ContainerBuilder())
    ->setConfig(require(__DIR__ . '/config/definitions.php'))
    ->build();

After the container is built, objects can be obtained via get():

$object = $container->get('test.foo');

But it is not recommended.

“users SHOULD NOT pass a container into an object, so the object can retrieve its own dependencies. Users doing so are using the container as a Service Locator. Service Locator usage is generally discouraged.”

Using aliases

Using closures

Using parameters bag

Delegate lookup

Using cache