northwoods/container

Container wrapper for Auryn dependency injector

3.2.0 2018-09-20 13:55 UTC

This package is auto-updated.

Last update: 2024-11-21 03:55:29 UTC


README

Become a Supporter Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

Auryn is awesome, so why not use it as a container when packages require it?

Note: This goes completely against the philosophy of not using Auryn as a service locator. This package is only meant to be a pragmatic solution for Auryn users that want to use a package that requires a service locator.

Attempts to be PSR-1, PSR-2, PSR-4, and PSR-11 compliant.

Install

composer require northwoods/container

Usage

use Auryn\Injector;
use Northwoods\Container\InjectorContainer;
use Psr\Container\ContainerInterface;

// Make an Injector and configure it.
$injector = new Injector();

// Optional: Declare a single container instance.
$injector->share(ContainerInterface::class);

// Use InjectorContainer as the implementation of ContainerInterface.
$injector->alias(ContainerInterface::class, InjectorContainer::class);

// InjectorContainer will wrap this Injector instance.
$injector->define(InjectorContainer::class, [':injector' => $injector]);

Configuration

This package provides a InjectorBuilder that can be used to configure Auryn using separate classes. The builder takes a list of configuration objects and applies each of them to the injector.

use Northwoods\Container\Config\ContainerConfig;
use Northwoods\Container\InjectorBuilder;

$builder = new InjectorBuilder([
    new ContainerConfig(),
]);

If you prefer to have the injector instantiate the configuration classes, use the LazyInjectorBuilder:

use Northwoods\Container\Config\ContainerConfig;
use Northwoods\Container\LazyInjectorBuilder;

$builder = new LazyInjectorBuilder([
    ContainerConfig::class,
]);

The builder can then be used to create an Injector instance:

$injector = $builder->build();
$container = $injector->make(ContainerInterface::class);

Note: An existing instance of Auryn can also be provided to the build() method.

Zend Service Manager Compatibility

This package is compatible with Zend Expressive Container Config:

use Northwoods\Container\Zend\Config;
use Northwoods\Container\Zend\ContainerFactory;

$factory = new ContainerFactory();

$container = $factory(new Config(
    require 'path/to/services.php',
));

Note: All injections configured this way will be shared!

An existing Injector can also be passed into ContainerFactory:

$factory = new ContainerFactory($injector);

This can be combined with InjectorBuilder or LazyInjectorBuilder to apply configuration such as define() calls.

Identifiers

PSR-11 does not require the container identifier to be a class name, while Auryn does. The only exception to this rule in Auryn is that a class alias can be anything. These container "service names" must resolve to a class and will need to be aliased.

For example a package may require a config entry in the container that is meant to resolve to an array. This can be achieved by creating a delegate that creates an instance of ArrayObject:

use ArrayObject;
use Auryn\Injector;
use Northwoods\Container\InjectorContainer;

// Share a global "config" array as an object
$injector->share('config')->delegate('config', function () {
    return new ArrayObject(require 'path/to/config.php');
});

// Create the container
$container = new InjectorContainer($injector);

Now whenever $container->get('config') is called the ArrayObject will be returned.

Examples

Additional examples are available in the examples/ directory.

License

MIT