2017 © Pedro Peláez
 

library lib-dependency

Dependency injection library of the Ride framework

image

ride/lib-dependency

Dependency injection library of the Ride framework

  • Wednesday, April 4, 2018
  • by ride-user
  • Repository
  • 7 Watchers
  • 1 Stars
  • 3,186 Installations
  • PHP
  • 17 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 2 % Grown

The README.md

Ride: Dependency Injection Library

Dependency injection library of the PHP Ride framework., (*1)

This module can create objects and invoke callbacks with dynamic argument injection., (*2)

Read more about the dependency injection pattern on Wikipedia., (*3)

What's In This Library

Dependency

The Dependency class is used to define your class instances. You can tell what interfaces a class implements so the DependencyInjector knows when to use this instance. Define method calls to build up your instance as you need it, ready to work., (*4)

When you have multiple instances of a class, you can set an id to the dependency to specify each instance., (*5)

Dependencies can be tagged to retrieve a dependency subset for a specified interface., (*6)

Instead of literally constructing the object, a dependency can also be defined as being constructed by a factory., (*7)

DependencyInjector

The DependencyInjector is the facade of this library. It has different getters to retrieve a single or multiple instances. Dependencies are requested by interface, optionally an id, or for multiple instances, tag(s)., (*8)

When a requested interface, or instance dependency, is not defined in the container, an attempt is made to automatically construct the instance., (*9)

DependencyContainer

The DependencyContainer is like it says, a container of dependencies. All your definitions are kept here for the dependency injector to use as it's source., (*10)

DependencyArgumentParser

When defining method calls for your dependencies, you can pass arguments to those calls. You have different type of arguments. This library defines the following types by default: null, scalar, array, dependency and call., (*11)

By implementing the DependencyArgumentParser interface, you can create your own argument types. Ride will add the parameter and route type with the ride/app and ride/web modules., (*12)

Code Sample

Check this code sample to see the possibilities of this library:, (*13)

<?php

use ride\library\dependency\Dependency;
use ride\library\dependency\DependencyCall;
use ride\library\dependency\DependencyCallArgument;
use ride\library\dependency\DependencyContainer;
use ride\library\dependency\DependencyInjector;

// Your dependencies are stored in a dependency container. For the sake of
// explaining this library, let's initialize it manually. This should be done
// through configuration to get real benefit of this library
$dependencyContainer = new DependencyContainer();

// most generic definition of a dependency is a class, however this definition 
// is obsulete since the dependency injector attempts to create undefined 
// dependencies as well
$dependency = new Dependency('some\Class');

// give your dependency an id to retrieve a specific instance of an interface
$dependency = new Dependency('some\Class', 'id1');

// some\Class implements some interfaces
$dependency->addInterface('some\Interface');
$dependency->addInterface('some\OtherInterface');

// now add it to the container
$dependencyContainer->addDependency($dependency);

// lets create another another, this time with a constructor and some action

// define the constructor call
$argument = new DependencyCallArgument('name', 'dependency', array(
    'interface' => 'some\Interface', 
    'id' => 'id1',
));
$call = new DependencyCall('__construct');
$call->addArgument($argument);

// define the dependency and add some calls
$dependency = new Dependency('another\Class', 'id2');
$dependency->addCall($call);
$dependency->addCall(new DependencyCall('doSomething'));
$dependency->addInterface('some\Interface');

// add it to the container
$dependencyContainer->addDependency($dependency);

// define a factory for a dependency
$constructCall = new DependencyConstructCall('some\Factory', 'methodOnFactory');

$dependency = new Dependency($constructCall, 'id');
$dependency->addCall(new DependencyCall('doSomething'));
$dependency->addInterface('some\Interface');

$dependencyContainer->addDependency($dependency);

// Your dependency container gets filled up with this kind of definitions.
// Once setup, you are ready to get your instances.

// First we need to create the dependency injector itself.
$dependencyInjector = new DependencyInjector($dependencyContainer);

// Let's get an instance, the thing you are most likely to do...
$instance = $dependencyInjector->get('some\Interface'); // another\Class since it's last defined
$instance = $dependencyInjector->get('some\Interface', 'id1'); // some\Class
try {
    $instance = $dependencyInjector->get('third\Class');
    // your instance if the third\Class can be created with the available dependencies
} catch (Exception $e) {
    // when the dependency is an interface,
    // or when it's a class and it's not constructable or when some required arguments could
    // not be injected.
}

// You can also invoke callbacks where not provided arguments are injected if possible
$callback = 'function';
$callback = array('some\Static', 'call');
$callback = array(new some\Class(), 'call');
$arguments = array('name' => $value); // arguments you know/want, the rest will be injected
$returnValue = $dependencyInjector->invoke($callback, $arguments);

Installation

You can use Composer to install this library., (*14)

composer require ride/lib-dependency

The Versions

04/04 2018

dev-master

9999999-dev

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joris Vandeweerd

04/04 2018

dev-develop

dev-develop

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joris Vandeweerd

04/04 2018

1.0.3

1.0.3.0

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joris Vandeweerd

04/04 2018

dev-peter279k-test_enhancement

dev-peter279k-test_enhancement

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joris Vandeweerd

16/03 2018

1.0.2

1.0.2.0

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

08/09 2016

1.0.1

1.0.1.0

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

20/07 2016

1.0.0

1.0.0.0

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

21/02 2016

dev-feature/dependency-intelligence

dev-feature/dependency-intelligence

Dependency injection library of the Ride framework

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd