2017 © Pedro Peláez
 

library ttinjector

Construct anything using dependency injection

image

thumbtack/ttinjector

Construct anything using dependency injection

  • Wednesday, June 21, 2017
  • by jmikkola
  • Repository
  • 99 Watchers
  • 3 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

ttinjector

Build Status, (*1)

This is a simple dependency injector., (*2)

It works in two stages. First, you register all the dependencies and describe how they relate to each other. Then, you build an injector out of those dependencies that can inject the values you have added., (*3)

Installation

To install, use composer and run:, (*4)

composer require thumbtack/ttinjector

How to use the injector:

1. Create a Dependencies object:

use TT\injector\Dependencies;

$dependencies = new Dependencies();

2. Add some dependencies:

// Use register_value() to add constant values.
$dependencies->register_value('current_user_id', 12345);

// Use register_factory() to add things you don't want to construct unless used...
$dependencies->register_factory('db_connection', [], function() {
   return Database::Connect();
});

// ...Or things that have dependencies of their own.
$dependencies->register_factory(
   'users_source'
   ['db_connection'],
   function ($db) {
       return function($user_id) use ($db) {
          return $db->query('users')->where_equals('user_id', $user_id);
       };
   }
);

3. Build the injector:

$injector = $dependencies->build_injector();

The build_injector() function will check to make sure there aren't any errors in the dependencies you've set up (missing dependencies or dependency cycles)., (*5)

Injector objects are immutable., (*6)

4. Inject your dependencies!

function current_user($current_user_id, $users_source) {
   return $users_source($current_user_id);
}

$user = $injector->inject('current_user', ['current_user_id', 'users_source']);

The Versions

21/06 2017

dev-master

9999999-dev https://github.com/thumbtack/ttinjector

Construct anything using dependency injection

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jeremy Mikkola

dependency-injection

16/03 2016

1.0.4

1.0.4.0 https://github.com/thumbtack/ttinjector

Construct anything using dependency injection

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jeremy Mikkola

dependency-injection

04/03 2015

1.0.2

1.0.2.0 https://github.com/thumbtack/ttinjector

Construct anything using dependency injection

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jeremy Mikkola

dependency-injection

03/03 2015

dev-dev

dev-dev https://github.com/thumbtack/ttinjector

Construct anything using dependency injection

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jeremy Mikkola

dependency-injection

13/02 2015

1.0.0

1.0.0.0 https://github.com/thumbtack/ttinjector

Construct anything using dependency injection

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jeremy Mikkola

dependency-injection