Giter Site home page Giter Site logo

container's Introduction

Northwoods Container

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!

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

container's People

Contributors

shadowhand avatar

Stargazers

hack-club avatar

Watchers

James Cloos avatar George avatar hack-club avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.