sitepoint/container

A simple, easy to follow PHP dependency injection container

v0.1.0 2015-10-24 20:48 UTC

This package is not auto-updated.

Last update: 2024-10-26 19:10:38 UTC


README

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Code Climate Total Downloads License

A simple, easy to follow PHP dependency injection container. Designed to be forked, modified, extended and hacked.

How to Use

Although it isn't required to do so, a good practice is to split up the configuration for our container. In this example we'll use three files to create our container for the Monolog component.

Another good practice is to use class and interface paths as service names. This provides a stricter naming convention that gives us more information about the services.

In the service definitions file, we define three services. All of the services require constructor injection arguments. Some of these arguments are imported from the container parameters and some are defined directly. The logger service also requires two calls to the pushHandler method, each with a different handler service imported.

<?php // config/services.php

// Value objects are used to reference parameters and services in the container
use SitePoint\Container\Reference\ParameterReference as PR;
use SitePoint\Container\Reference\ServiceReference as SR;

use Monolog\Logger;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface;

return [
    StreamHandler::class => [
        'class' => StreamHandler::class,
        'arguments' => [
            new PR('logger.file'),
            Logger::DEBUG,
        ],
    ],
    NativeMailHandler::class => [
        'class' => NativeMailerHandler::class,
        'arguments' => [
            new PR('logger.mail.to_address'),
            new PR('logger.mail.subject'),
            new PR('logger.mail.from_address'),
            Logger::ERROR,
        ],
    ],
    LoggerInterface::class => [
        'class' => Logger::class,
        'arguments' => [ 'channel-name' ],
        'calls' => [
            [
                'method' => 'pushHandler',
                'arguments' => [
                    new SR(StreamHandler::class),
                ]
            ],
            [
                'method' => 'pushHandler',
                'arguments' => [
                    new SR(NativeMailHandler::class),
                ]
            ]
        ]
    ]
];

The parameters definitions file just returns an array of values. These are defined as an N-dimensional array, but they are accessed through references using the notation: 'logger.file' or 'logger.mail.to_address'.

<?php // config/parameters.php

return [
    'logger' => [
        'file' => __DIR__.'/../app.log',
        'mail' => [
            'to_address' => 'webmaster@domain.com',
            'from_address' => 'alerts@domain.com',
            'subject' => 'App Logs',
        ],
    ],
];

The container file just extracts the service and parameter definitions and passes them to the Container class constructor.

<?php // config/container.php

use SitePoint\Container\Container;

$services   = include __DIR__.'/services.php';
$parameters = include __DIR__.'/parameters.php';

return new Container($services, $parameters);

Now we can obtain the container in our app and use the logger service.

<?php // app/file.php

use Psr\Log\LoggerInterface;

require_once __DIR__.'/../vendor/autoload.php';

$container = include __DIR__.'/../config/container.php';

$logger = $container->get(LoggerInterface::class);
$logger->debug('This will be logged to the file');
$logger->error('This will be logged to the file and the email');

Authors

Change Log

This project maintains a change log file

License

The MIT License (MIT). Please see LICENSE for more information.