GithubHelp home page GithubHelp logo

devtronic / injector Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 22 KB

A service container for humans

License: MIT License

PHP 100.00%
service-container dependency-injection dependency-injection-container psr-11 container containerinterface service php

injector's Introduction

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 register-method.

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.

<?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.

<?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.

<?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.

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

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->register('my.service', function ($hostname) {
    return 'Connecting to ' . $hostname;
}, ['%database.host%']);

Testing

$ phpunit

Contribute

Feel free to fork and add pull-requests ๐Ÿค“

injector's People

Contributors

devtronic avatar

Stargazers

 avatar

Watchers

 avatar  avatar

injector's Issues

Warning on static dependency

If a non-string static dependency is passed to a service, the service container shows a warning because preg_match expects a string as second argument.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.