Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

borodulin/Container

Repository files navigation

Simple Fast Autowiring Container

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages