PHP Classes

Service Container: Register container for service handlers

Recommend this page to a friend!
  Info   View files Documentation   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 41 This week: 1All time: 10,820 This week: 560Up
Version License PHP version Categories
servicecontainer 1.0Custom (specified...5PHP 5, Language, Design Patterns
Description 

Author

This package can register container for service handlers.

It can register containers that implement services by the means of callback functions or classes.

The package also call the code or loaded the registered service handler objects to be called by the applications that need the services.

Picture of Julian Finkler
  Performance   Level  
Name: Julian Finkler <contact>
Classes: 8 packages by
Country: Germany Germany
Age: 30
All time rank: 2919172 in Germany Germany
Week rank: 109 Up6 in Germany Germany Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Documentation

Travis Packagist GitHub license Packagist

Injector

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

Installation

$ composer require devtronic/injector

Usage

Register Services

To register a service you have to call the registerService-method.

ServiceContainer::registerService($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.

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

$serviceContainer->registerService('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.

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

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

$serviceContainer->registerService('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.

<?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->registerService('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.

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->registerService('app.another_service', function () {
    return [
        'name' => 'injector',
        'developer' => 'Julian',
    ];
});

$serviceContainer->registerService('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

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%

<?php

use Devtronic\Injector\ServiceContainer;

$serviceContainer = new ServiceContainer();

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

Testing

$ phpunit

Contribute

Feel free to fork and add pull-requests ?


  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (4 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageException (2 files)
  Plain text file ServiceContainer.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file ParameterNotDefinedException.php Class Class source
  Plain text file ServiceNotFoundException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file autoload.php Aux. Auxiliary script
  Plain text file ServiceContainerTest.php Class Class source
  Plain text file TestClass.php Class Class source
  Plain text file TestClassEmpty.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:41
This week:1
All time:10,820
This week:560Up