robert430404/rc-container

This is a simple Dependency Injection Container for PHP.

1.2.0 2017-04-04 17:42 UTC

This package is not auto-updated.

Last update: 2024-11-09 21:06:54 UTC


README

Latest Stable Version Build Status codecov

What Is This?

This is RC Container. This is a simple dependency injection container that allows you to register services, parameters, and factories for your application and then retrieve/de-register them.

Why Write This?

I did it to flex my brain, and get a full understanding of how DI-Containers work in the PHP space. Rather than just reading about it and assuming I knew what did what, I wrote this to solidify my knowledge.

Installing The Package

To install and use this package, install it with composer:

composer require robert430404/rc-container

How Does It Work?

The container relies on composer for the autoloading of the classes. You then create a new instance of the Container() object and start assigning your services/parameters/factories to the instance.

<?php

use RcContainer\Container;

require 'vendor/autoload.php';

$container = new Container();

$container->registerParameter('test-parameter', function () {
    return 'this-is-the-test-param';
});

$container->registerService('test-service', function () {
    return new stdClass();
});

$container->registerFactory('test-factory', function () {
    return new stdClass();
});

You can also assign multiple services, parameters, or factories in a single call using this method:

<?php

use RcContainer\Container;

require 'vendor/autoload.php';

$container = new Container();

$container->registerServices([
    'test-service-1' => function () {
        return new stdClass();
    },
    'test-service-2' => function () {
        return new stdClass();
    }
]);

$container->registerParameters([
    'test-parameter-1' => function () {
        return 'parameter variable';
    },
    'test-parameter-2' => function () {
        return 'second parameter variable';
    }
]);

$container->registerFactories([
    'test-factory-1' => function () {
        return new stdClass();
    },
    'test-factory-2' => function () {
        return new stdClass();
    }
]);

Or you can use a more conceise syntax like this:

<?php

use RcContainer\Container;

require 'vendor/autoload.php';

$container = new Container();

$container->registerServices([
    'test-service-1' => 'stdClass',
    'test-service-2' => 'stdClass'
]);

$container->registerParameters([
    'test-parameter-1' => 'parameter variable',
    'test-parameter-2' => 'second parameter variable'
]);

$container->registerFactories([
    'test-factory-1' => 'stdClass',
    'test-factory-2' => 'stdClass'
]);

Once you have your services/parameters/factories defined, you then call the retrieval methods on the container to get access to your registered services/parameters/factories.

<?php

$container->parameter('test-parameter'); // Returns your param
$container->service('test-service'); // Returns the service (Same Instance)
$container->factory('test-factory'); // Returns the factory's object (New Instance)

What Are Some Of The Features?

The container allows you to bind service, factories, and parameters to it. This allows you to have a central place to access your dependencies and inject what ever is needed as needed. You can also inject interlocking dependencies via the container by passing the container into the closure.

<?php

use RcContainer\Container;
use Vendor\SDKObject; // Made Up Namespace

require 'vendor/autoload.php';

$container = new Container();

// Parameters
$container->registerParameter('api-key', function () {
    return '000-000-000-000-0000';
});

// Services
$container->registerService('the-api', function () use ($container) {
    $apiKey = $container->parameter('api-key'); // Retrieves Registered Key
    
    return new SDKObject($apiKey); // Made Up Object
});

The same method works to pass around services:

<?php

use RcContainer\Container;
use Vendor\SDKObject; // Made Up Namespace
use Vendor\DataFactory; // Made Up Namespace

require 'vendor/autoload.php';

$container = new Container();

// Parameters
$container->registerParameter('api-key', function () {
    return '000-000-000-000-0000';
});

// Services
$container->registerService('the-api', function () use ($container) {
    $apiKey = $container->parameter('api-key'); // Retrieves Registered Key
    
    return new SDKObject($apiKey); // Made Up Object
});

// Factories
$container->registerFactory('data-factory', function () use ($container) {
    $apiSdk = $container->service('the-api');
    
    return new DataFactory($apiSdk);
});

This gives you a robust and easy to use container that should suit most DI needs.