2017 © Pedro PelĆ”ez
 

library container

Dependency Container for PHP

image

njasm/container

Dependency Container for PHP

  • Tuesday, December 26, 2017
  • by njasm
  • Repository
  • 2 Watchers
  • 18 Stars
  • 12,435 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 4 Open issues
  • 22 Versions
  • 5 % Grown

The README.md

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version License HHVM Status, (*1)

Dependency Container / Service Locator

Features

  • Alias Service Keys support
  • Circular Dependency guard
  • Primitive data-types registration
  • Automatic Constructor Dependency Resolution and Injection for non-registered Services
  • Lazy and Eager instantiation approaches
  • Lazy and Eager Instantiation Singleton services registration
  • Support for public Setter injection/Method calls after service instantiation
  • Support for public Properties/Attributes Injection after Service instantiation
  • Ability to override existing dependency (Properties & Setters) declarations by supplying new ones when call to Container::get
  • Nested Providers/Containers support
  • Comply with Container-Interop interfaces

Requirements

  • PHP 5.3 or higher.

Installation

Include njasm\container in your project, by adding it to your composer.json file., (*2)

{
    "require": {
        "njasm/container": "~1.0"
    }
}

Usage

To create a container, simply instantiate the Container class., (*3)

use Njasm\Container\Container;

$container = new Container();

Using Alias

There are time that your key are too long to be convenient for your client code, one example for instance, is when binding an interface to an implementation or when using for your key the FQCN of your classes., (*4)

namespace Very\Long\Name\Space;

interface SomeInterface {}

class SomeImplementation implements SomeInterface
{
    // code here
}

$container = new Njasm\Container\Container();
$container->set('Very\Long\Name\Space\SomeInterface', new SomeImplementation());
$container->alias('Some', 'Very\Long\Name\Space\SomeInterface');

$some = $container->get('Some');

Defining Services

Services are defined with two params. A key and a value. The order you define your services is irrelevant., (*5)

Defining Services - Primitive data-types

$container->set("Username", "John");

echo $container->get("Username");

Defining Services - Binding Services (Lazy Loading)

You can bind a key to a instantiable FCQN value., (*6)

php $container->bind("MyKey", "\My\Namespace\SomeClass");, (*7)

If you want to bind a Service, and register that Service as a Singleton Service. php $container->bindSingleton("MyKey", "\My\Namespace\SomeClass");, (*8)

Both Container::bind and Container::bindSingleton uses Lazy Loading approach, so that \My\Namespace\SomeClass will only be evaluated/instantiated when MyKey is requested., (*9)

When binding a service, constructor dependencies can be declared, public attributes be set and methods called with arguments, so they are injected/setted when instantiating the service., (*10)

namespace \App\Actors;

class Person {
    protected $name;
    protected $age = 24;
    public genre = 'Male';

    public function __construct($name = 'John') {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function getAge() {
        return $this->age;
    }

    public function setAge($age) {
        $this->age = (int) $age;
    }
}

$container->bind(
    "Person",                       // key
    "\App\Actors\Person",           // FQCN
    array("Jane"),                  // constructor dependencies
    array("genre" => "Female"),     // attributes injection
    array("setAge" => array(33))    // call methods
);

// binding with chaining methods 
$container->bind("Person", '\App\Actors\Person')
    ->setConstructorArguments(array("Jane"))        // setConstructorArgument($index, $argument)
    ->setProperty("genre" => "Female")              // setProperties(array("genre" => "Female", ...) also work
    ->callMethod("setAge", array(33));              // callMethods(array('methodName' => 'methodValue', ...));

// retrieving the object    
$person = $container->get("Person");
echo $person->getName(); // Jane
echo $person->getAge();  // 33
echo $person->genre      // Female

// calling services and overriding declared dependencies 
$person2 = $container->get(
    "Person", 
    array("Mark"), 
    array("genre" => "Male"), 
    array("setAge" => array(55))
);
echo $person2->getName(); // Mark
echo $person2->getAge();  // 55
echo $person2->genre      // Male

Defining Services - Objects (Eager Loading)

$mailer = new \Namespace\For\My\MailTransport(
    "smtp.example.com", 
    "username", 
    "password", 
    25
); 

$container->set(
    "Mail.Transport", 
    $mailer, 
    array(), // constructor args 
    array(), // public properties injection
    array("withSSL" => array(false)) // calling methods
);

$mailerTransport = $container->get("Mail.Transport");

Overwriting existent declared dependencies is also possible for set definitions., (*11)

// calling methods and injecting attributes is also possible
$mailerTransportSsl = $container->get(
    "Mail.Transport", 
    array(), 
    array(), 
    array("withSSL" => array(true))
);

Defining Services - Complex builds (Lazy Loading)

There are time when you'll want to instantiate an object, but the build process is reall complex and you want to control that process. You use anonymous functions for that., (*12)

$container->set(
    "Complex",
    function($firstName = "John", $lastName = "Doe") {
        // complex logic here
        // ...
        $theComplexObject = new Complex($firstName, $lastName);

        return $theComplexObject;
    }
);

$complex = $container->get("Complex");

// injecting closure dependencies is also possible
$complexJane = $container->get("Complex", array("Jane", "Fonda")); 

Defining Services - Complex builds With Nested Dependencies (Lazy Loading)

Creation of nested dependencies is also possible. You just need to pass the container to the closure., (*13)

$container->set(
    "Mail.Transport",
    function() use (&$container) {
        return new \Namespace\For\My\MailTransport(
            $container->get("Mail.Transport.Config")
        );
    }
);

$container->set(
    "Mail.Transport.Config",
    function() {
        return new \Namespace\For\My\MailTransportConfig(
            "smtp.example.com", 
            "username", 
            "password", 
            25
        );
    }
);

$mailer = $container->get("Mail.Transport");

Defining Singleton Services

For registering singleton services, you use the singleton method invocation., (*14)

$container->singleton(
    "Database.Connection",
    function() {
        return new \Namespace\For\My\Database(
            "mysql:host=example.com;port=3306;dbname=your_db", 
            "username", 
            "password"
        );
    }
);

// MyDatabase is instantiated and stored, for future requests to this service, 
// and then returned.
$db = $container->get("Database.Connection");
$db2 = $container->get("Database.Connection");

// $db === $db2 TRUE

Defining Sub/Nested Containers

Nesting container is possible as long as you use an existing Container Adapter for your application existing container. The Adapter class must implement the ServicesProviderInterface for more examples please see the Adapterfolder., (*15)

$pimple; // is your instantiated pimple container
$pimple["Name"] = $pimple->factory(function() {
   return "John";
}

$pimpleAdapter = new \Njasm\Container\Adapter\PimpleAdapter($pimple);
$mainContainer = new \Njasm\Container\Container();
$mainContainer->provider($pimpleAdapter);

$mainContainer->has("Name"); // TRUE
echo $mainContainer->get("Name"); // John

Automatic Resolution of Services

When the Container is requested for a service that is not registered, it will try to find the class, and will automatically try to resolve your class's constructor dependencies., (*16)

namespace My\Name\Space;

class Something
{
    // code
}

// without registering the Something class in the container you can...
$container = new Njasm\Container\Container();
$something = $container->get('My\Name\Space\Something');

//$something instanceof 'My\Name\Space\Something' == true

//once again you can also inject dependencies when calling get method.
$something = $container->get(
    "My\Name\Space\Something", 
    array("constructor value 1", "constructor value 2"),
    array("attributeName" => "value 1"), // attributes
    array("methodName" => array("value 1", "value 2"))
);

Roadmap

In no Particular order - check Milestones for a more organized picture., (*17)

  • [ ] Load definitions from configuration files
  • [ ] Optimizations

Contributing

Do you wanna help on feature development/improving existing code through refactoring, etc? Or wanna discuss a feature/bug/idea? Issues and Pull Requests are welcome as long as you follow some guidelines for PRs:, (*18)

Pull Requests must: - Be PSR-2 compliant. - Submit tests with your pull request to your own changes / new code introduction. - having fun., (*19)

The Versions

26/12 2017

2.0.x-dev

2.0.9999999.9999999-dev http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nelson JoĆ£o Morais

dependency injection dic ioc dependency-container

13/11 2014
22/08 2014

1.2.3

1.2.3.0 http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nelson JoĆ£o Morais

dic ioc dependency container dependency-container

21/08 2014

1.2.2

1.2.2.0 http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nelson JoĆ£o Morais

dic ioc dependency container dependency-container

21/08 2014

1.2.1

1.2.1.0 http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nelson JoĆ£o Morais

dic ioc dependency container dependency-container

20/08 2014

1.2.0

1.2.0.0 http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nelson JoĆ£o Morais

dic ioc dependency container dependency-container

19/08 2014

1.1.1

1.1.1.0 http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nelson JoĆ£o Morais

dic ioc dependency container dependency-container

21/07 2014

1.1.0

1.1.0.0 http://github.com/njasm/container

Dependency Container for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Nelson JoĆ£o Morais

dic ioc dependency container dependency-container

11/06 2014

1.0.1

1.0.1.0 http://github.com/njasm/services-container

Services Locator/Dependency Container for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Nelson JoĆ£o Morais

service service locator dependency-injection ioc inversion of control dependency injection locator dependency container services locator

09/06 2014

1.0.0

1.0.0.0 http://github.com/njasm/services-container

Services Locator/Dependency Container for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Nelson JoĆ£o Morais

service service locator dependency-injection ioc inversion of control dependency injection locator dependency container services locator