2017 © Pedro Peláez
 

library rc-container

This is a simple Dependency Injection Container for PHP.

image

robert430404/rc-container

This is a simple Dependency Injection Container for PHP.

  • Tuesday, April 4, 2017
  • by robert430404
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Latest Stable Version Build Status codecov, (*1)

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., (*2)

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., (*3)

Installing The Package

To install and use this package, install it with composer:, (*4)

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., (*5)

<?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:, (*6)

<?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:, (*7)

<?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., (*8)

<?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., (*9)

<?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:, (*10)

<?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., (*11)

The Versions

04/04 2017

dev-master

9999999-dev

This is a simple Dependency Injection Container for PHP.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Robert Cox

04/04 2017

1.2.0

1.2.0.0

This is a simple Dependency Injection Container for PHP.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Robert Cox

28/03 2017

1.1.0

1.1.0.0

This is a simple Dependency Injection Container for PHP.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Robert Cox

22/02 2017

1.0.0

1.0.0.0

This is a simple Dependency Injection Container for PHP.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Robert Cox