GithubHelp home page GithubHelp logo

northwoods / container Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 3.0 51 KB

Container wrapper for Auryn dependency injector

License: MIT License

PHP 100.00%
auryn psr-11 container injector dependency-injection

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!

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

container's People

Contributors

musiksammler avatar shadowhand avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

container's Issues

Expressive compatibility

Hello!

Just letting you know that we recently published container configuration documentation:
https://docs.zendframework.com/zend-expressive/v3/features/container/config/
We are working on library which covers all these cases and deliver common tests to check if the container is compatible and behave as expected, please see http://github.com/weierophinney/zend-container-config-test and the latest PR with tests updates:
https://github.com/weierophinney/zend-container-config-test/pull/4
We have already prepared fixed for Aura.Di and Pimple containers:

Would be nice if also this library is updated to use common tests. Please bare in mind it's still dev version, and the structure can be changes, but I think at this point we have quite good coverage of the desired behaviour.

Zend Expressive FlashMiddleware or SessionMiddleware are not invoked

Hello!

I've added zend-expressive-session, zend-expressive-session-ext and zend-expressive-flash to my zend-expressive project (as documented in https://docs.zendframework.com/zend-expressive/v3/cookbook/flash-messengers/) and I'm using Auryn for dependencies.

With Northwoods Container 2.1.0 everything works fine. But with the current 3.1.0 branch invokables from these packages fail with the error that the classes are expected to be invokable.

As far as I could see it's expected that these classes have a "__invoke()" method, which is not the case. Instead they have simple constructors without any required parameters. So they should be treated like they are invokable but they're not.

InjectorContainer has to implement Interop\Container\ContainerInterface too

Hello!

I'm using Zend Eventmanager and when initializing a LazyListenerAggregate (https://docs.zendframework.com/zend-eventmanager/lazy-listeners/lazy-listener-aggregate/) the aggregate needs the current container as a second argument.

Problem is, that this container still has to implement \Interop\Container\ContainerInterface and not \Psr\Container\ContainerInterface. As long as this is not fixed in the zend eventmanager package (and probably others) it would be good if the InjectorContainer simply implements the interop container too.

use Psr\Container\ContainerInterface;
use Interop\Container\ContainerInterface as InteropContainerInterface;

class InjectorContainer implements ContainerInterface, InteropContainerInterface

This does not break anything and compatibility/usability with other packages is improved.

Delegators not working correctly

Seems like delegators in zend-expressive should map to Auryn's Injector::prepare. The current implementation is only coupled to factories and invokables and doesn't allow you to use a delegator on it's own.

Where do parameter definitions go?

I'm unclear where I should put parameter definitions like the following:

    $injector->defineParam('globalParam', 'value');

    $injector->define(MyClass::class, [
        ':myParam' => 'value',
    ]);

Currently adding them to config/container.php but not feeling that it's the "correct" place

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.