2017 © Pedro Peláez
 

library dependency-container

A simple, PSR-4 compliant dependency container.

image

baublet/dependency-container

A simple, PSR-4 compliant dependency container.

  • Monday, April 17, 2017
  • by baublet
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

DependencyContainer

Build Status, (*1)

For simple dependency injection, (*2)

Why?

Sometimes you need dependencies across your application, plugin, or some other system that doesn't itself have a way to handle it (e.g., WordPress). So include this project as a submodule and inject dependencies into your classes at your leisure. Use a container when you don't want to pollute your constructors with a ton of dependencies., (*3)

This container also only loads dependencies when a class tries to access the dependency in question. That's why all of our injections are declared as functions. For more information on this, see below., (*4)

It's PSR compliant, implementing the (proposed) Container interfaces for the API. If the API changes in the interfaces, I'll update these to match., (*5)

This means that the API for this is subject to change, so please read any documentation before upgrading., (*6)

Installation

Add baublet/dependency-container to the required packages portion of your composer.json file, or, from the command line:, (*7)

composer require baublet/dependency-container

Example

I've tried to make the API fairly simple. First, require the container module and declare a new container., (*8)

use baublet\DependencyContainer\DependencyContainer;

$dependencies = new DependencyContainer();

Dependencies are declared as functions, so that you can ensure your dependencies are only loaded when called. This allows you to use this container as a registry of dependencies in a particular module that, when called, may or may not use all of its dependencies in a given call., (*9)

To declare a dependency:, (*10)

$constructor = "This will be passed to the function as $args";

$dependencies->set("my-dependency", function($args) {
    require_once("some/big/dependency/class.php");
    return new SomeBigDependencyClass($args);
}, $constructor);

In the above code, some/big/dependency/class.php is only required and SomeBigDependencyClass is only initialized when you call $dependencies->get("my-dependency");., (*11)

Testing

This code is tested using Travis CI and plain PHP assertions. No testing framework required. Just hop into the test directory and run the test:, (*12)

php test.php

The Versions

17/04 2017

dev-master

9999999-dev

A simple, PSR-4 compliant dependency container.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ryan Poe

dependency injection service locator dependency dependencies dependency container dependency management

17/04 2017

0.0.0

0.0.0.0

A simple, PSR-4 compliant dependency container.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ryan Poe

dependency injection service locator dependency dependencies dependency container dependency management