2017 © Pedro Peláez
 

library injector

A service container for humans

image

devtronic/injector

A service container for humans

  • Saturday, April 14, 2018
  • by Devtronic
  • Repository
  • 1 Watchers
  • 1 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 5 % Grown

The README.md

Travis Packagist GitHub license Packagist, (*1)

Injector

Injector is a dependency injection container.
It's fast, reliable and easy to understand., (*2)

Installation

$ composer require devtronic/injector

Usage

Register Services

To register a service you have to call the register-method., (*3)

ServiceContainer::register($name, $service, $arguments = [])
Parameter Description Example
name The unique name of the service. app.my_service
service The service callable. function($arg1) {}
arguments The arguments for the service. Entries with @-prefix are service references ['@app.foo', 1]

Register a service with static arguments

Since not all services need an service injection, the arguments array also supports static entries., (*4)

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

$serviceContainer->register('app.my_service', function ($name) {
  return 'Hello ' . $name;
}, ['Your Name']);

$serviceContainer->getRegisteredServices(); // Contains the registered Service 

Register a service with a service dependency

Sometimes you need another registered service in your service. In that case you can pass the service name with a @-prefix to reference to it. The (sub-) dependencies are solved recursively., (*5)

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

$serviceContainer->register('app.another_service', function () {
    return [
        'name' => 'injector',
        'developer' => 'Julian',
    ];
});

$serviceContainer->register('app.my_service', function (array $anotherService) {
    return "Name: {$anotherService['name']}, developer: {$anotherService['developer']}";
}, ['@app.another_service']);

Register a class as a service

You can also register a class as a service. If the service is loaded, the constructor gets called with the dependencies., (*6)

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

class Car
{
    /** @var int */
    public $maxSpeed = 0;

    /** @var string */
    public $color = '';

    public function __construct($maxSpeed, $color)
    {
        $this->maxSpeed = $maxSpeed;
        $this->color = $color;
    }
}

$serviceContainer->register('app.my_car', Car::class, [250, 'red']);

$myCar = $serviceContainer->get('app.my_car');
echo "My Car: Speed: {$myCar->maxSpeed}, Color: {$myCar->color}"; // My Car: Speed: 250, Color: red

Load a service

To load a service you have to call the loadService-method.
Once a service is loaded, it remains in memory at runtime. When the same service is loaded again, the first instance is returned., (*7)

ServiceContainer::loadService($name)
Parameter Description Example
name The unique name of the service. app.my_service
<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

$serviceContainer->register('app.another_service', function () {
    return [
        'name' => 'injector',
        'developer' => 'Julian',
    ];
});

$serviceContainer->register('app.my_service', function (array $anotherService) {
    return "Name: {$anotherService['name']}, developer: {$anotherService['developer']}";
}, ['@app.another_service']);

echo $serviceContainer->get('app.my_service'); // Name: injector, developer: Julian

Add Parameters

The service container also supports static parameters.
You can add a parameter using the addParameter-method, (*8)

ServiceContainer::addParameter($name)
Parameter Description Example
name The unique name of the parameter. database.host

To pass a parameter to a service, add before and after the name a '%': %name.of.the.parameter%, (*9)

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

$serviceContainer->addParameter('database.host', 'localhost');
$serviceContainer->register('my.service', function ($hostname) {
    return 'Connecting to ' . $hostname;
}, ['%database.host%']);

Testing

$ phpunit

Contribute

Feel free to fork and add pull-requests 🤓, (*10)

The Versions

14/04 2018

dev-master

9999999-dev

A service container for humans

  Sources   Download

MIT

The Requires

 

The Development Requires

by Julian Finkler

14/04 2018

v1.2.1

1.2.1.0

A service container for humans

  Sources   Download

MIT

The Requires

 

The Development Requires

by Julian Finkler

14/04 2018

v1.2.0

1.2.0.0

A service container for humans

  Sources   Download

MIT

The Requires

 

The Development Requires

by Julian Finkler

14/04 2018

dev-refactor_code

dev-refactor_code

A service container for humans

  Sources   Download

MIT

The Requires

 

The Development Requires

by Julian Finkler

23/11 2017

v1.1.0

1.1.0.0

A service container for humans

  Sources   Download

MIT

The Requires

 

The Development Requires

by Julian Finkler

23/11 2017

v1.0.8

1.0.8.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

11/09 2017

v1.0.7

1.0.7.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

15/08 2017

v1.0.6

1.0.6.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

11/08 2017

v1.0.5

1.0.5.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

06/08 2017

v1.0.4

1.0.4.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

05/08 2017

v1.0.3

1.0.3.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

03/08 2017

v1.0.2

1.0.2.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

29/07 2017

v1.0.1

1.0.1.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler

29/07 2017

v1.0.0

1.0.0.0

A service container for humans

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julian Finkler