GithubHelp home page GithubHelp logo

dacontainer's Introduction

DaContainer

Build Status Coverage Status Latest Stable Version Dependency Status

A simpe IoC container for PHP.

DaContainer is a powerful inversion of control container for managing class dependencies.

Installation

Simply add dagardner/dacontainer to your composer dependencies and run composer install.

"require": {
    "dagardner/dacontainer": "1.*"
}

The last step is to include composer' s autoload file (like this: require './vendor/autoload.php'.

And thats it. In the next chapter we we' ll cover basic usage.

Basic Usage

DaContainer can resolve both via Closure and direct resolution. Let' s start with Closures:

Binding...

$container = new \DaGardner\DaContainer\Container;
$container->bind('foo', function() {
  return new stdClass;
});

Resolving

$container->resolve('foo');

This will call the closure callback and therefor returns a new instance of a stdClass.

Singletons

Even if this pattern is often consired to be an anti-pattern, I implemented it, just in case somebody craves for it...

$container->singelton('foo', function() {
  return new stdClass;
});

This closure is only called once during execution. The returned instance is stored and a second resolve call will return the stored instance:

($container->resolve('foo') === $container->resolve('foo));

This statement will be true.

You can also store existing objects into the container:

$object = new stdClass;
$container->instance('foo', $object);

Automatic Injection

The IoC DaContainer can also auto-inject dependencies of a class into it' s constrcutor and injector methods.

class Foo
{
  public function __construct(stdClass $class) {}
  public function setLogger(LoggerInterface $logger) {}
}

With some other containers this wouldn' t be possible. The DaContainer will analyze the class with Reflections and auto-inject classes.

$container->resolve('Foo');

This will just return a new instance of Foo with a stdClass injected into the constructor. But how do we enable the injector method detection?

$container->enableInjecterDetection();
$container->resolve('Foo');

NOTE This feature (enableInjecterDetection()) requires PHP 5.4 or higher. In PHP 5.3 closure to object binding is not supported, which is crucial for this feature to work.

But this won' t work right now and throws a \DaGardner\DaContainer\Exceptions\ResolveException exception. Why?

The container doesn' t know what to do with this LoggerInterface. Since the container doesn' t know which implementation to use you have to tell it the container!

$container->bind('LoggerInterface', 'BasicLogger');

If a class requires an implementation of the LoggerInterface the container knows which implementation to use!

The Container::enableInjecterDetection() takes as optional argument a blacklist.

The exact display of the method is:

\DaGardner\DaContainer\Container::enableInjecterDetection([array $blacklist = array() [, string $version = PHP_VERSION]])

The blacklist should (has to be) constructed like this:

[
  'setString',
  'setArray',
  '_CLASSES_' => [
    'SomeClass' => [
      'setLogger'
    ]
  ]

]

Strings in the main array are consired to be global and are ignored everytime.

The class specific blacklist is only checked if the object is an instance of this class. A blacklist for a class should go into the _CLASSES_ key.

Events

The IoC Container has it' s own simple event system, which can be used standalone or getting hooked into the main event dispatcher!

$container->onResolving(function($object) {
  // Do something
});

dacontainer's People

Contributors

christiangaertner 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.