2017 © Pedro Peláez
 

library container

PSR-11 compatible dependency container

image

igniphp/container

PSR-11 compatible dependency container

  • Tuesday, March 20, 2018
  • by dkraczkowski
  • Repository
  • 1 Watchers
  • 2 Stars
  • 319 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 170 % Grown

The README.md

Igni logo Build Status

Igni Container

Licensed under MIT License., (*1)

Igni container is a psr-container compliant lightweight service locator pattern implementation., (*2)

List of features:

  • Simple usage if you are familiar with psr-container there is no learning-curve required for basic usage
  • Context aware you can define custom instances of services for given use-cases.
  • Auto wiring required dependencies are auto-injected into your service

Installation

composer install igniphp/container

Basic usage

<?php
$serviceLocator = new Igni\Container\ServiceLocator();
$serviceLocator->set('my_awesome_service', new stdClass());

$myService = $serviceLocator->get('my_awesome_service');

var_dump($myService === $serviceLocator->get('my_awesome_service')); // returns true

Registering shared service

Shared services are services which are instantiated only once and reference is kept in the registry that way every time service is requested from the container it will return the same instance., (*3)

<?php
use Igni\Container\ServiceLocator;

class Service 
{
    public $a;

    public function __construct(int $a = 1) 
    {
        $this->a = $a;    
    }
}

$serviceLocator = new ServiceLocator();
$serviceLocator->share(Service::class, function() { return new Service(2); });

var_dump($serviceLocator->get(Service::class)->a === 2); //true
var_dump($serviceLocator->get(Service::class) === $serviceLocator->get(Service::class)); // true

Factored services

Factored services are instantiated every time container is asked for the service., (*4)

<?php
use Igni\Container\ServiceLocator;

class Service 
{
    public $a;

    public function __construct(int $a = 1) 
    {
        $this->a = $a;    
    }
}

$serviceLocator = new ServiceLocator();
$serviceLocator->factory(Service::class, function() { return new Service(2); });

var_dump($serviceLocator->get(Service::class)->a === 2); //true
var_dump($serviceLocator->get(Service::class) === $serviceLocator->get(Service::class)); // false

Auto-wiring

Auto-wiring allows you to simply pass fully qualified class name and all type-hinted arguments for that class will be resolved automatically by the container., (*5)

<?php
use Igni\Container\ServiceLocator;

class A
{

}

class Service 
{
    public $a;
    public $number;

    public function __construct(int $number = 7, A $a) 
    {
        $this->number = $number;
        $this->a = $a;    
    }
}

$serviceLocator = new ServiceLocator();
$serviceLocator->share(A::class);
$serviceLocator->share(Service::class);

var_dump($serviceLocator->get(Service::class)->a instanceof A);// true

That's all folks!, (*6)

The Versions

20/03 2018

dev-master

9999999-dev

PSR-11 compatible dependency container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dawid Kraczkowski

service locator fast php 7 psr-11 dependency container autowiring

20/03 2018

1.1.0

1.1.0.0

PSR-11 compatible dependency container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dawid Kraczkowski

service locator fast php 7 psr-11 dependency container autowiring

01/03 2018

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dawid Kraczkowski