paritybit/dependency-resolver

A simple component to resolve dependencies

v1.0.0 2016-05-13 16:11 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:26:02 UTC


README

Coverage Status Build Status

A simple framework for resolving dependencies in code and managing a mapping of configurations to determine how specific dependencies should be resolved.

This isn't optimal and there are likely better implementations out there, but I needed something very quickly for a few specific, temporary, use cases. So it may not be very useful in general.

Installation

With composer

php composer.phar require paritybit/dependency-resolver

Usage

There is an example Resolution implementation CallableResolution. A Resolution is the encapsulation of a particular dependency. If one of your dependencies is an object, then you would tell the Resolution how to create or get this object in order for it to be provided when something is dependent on it. In most cases you would want to create your own Resolution implementation. My own use case was to get an object from a container until the container could be refactored away.

Create a map of your dependencies

use ParityBit\DependencyResolver\DependencyMap;
use ParityBit\DependencyResolver\DependencyConfigurations\CallableResolution;
use My\Applications\Dependency;

$map = new DependencyMap();
$map->registerResolution(
    Dependency::class,
    new CallableResolution(function () {
        return new Dependency();
    })
);

Instantiate the appropriate resolver

$resolver = new ParityBit\DependencyResolver\ConstructorArgumentResolver($map);

Resolve your dependencies

use Class\With\Dependencies\In\Constructor as ToResolve;

// find dependencies, instantiate dependent object and inject dependencies
$resolved = $resolver->resolve(ToResolve::class);