juliangut/slim-php-di

Slim Framework PHP-DI container integration

1.2 2016-05-09 16:59 UTC

README

PHP version Latest Version License

Build status Style Code Quality Code Coverage Total Downloads

Slim3 PHP-DI container integration

PHP-DI dependency injection container integration for Slim3 Framework.

Prepares PHP-DI container to fit in Slim3 App by registering default services in the container.

In order to allow possible services out there expecting the container to be Slim\Container and thus implementing ArrayAccess, it has been added to the container as well. You are encouraged to use ArrayAccess syntax for assignment instead of PHP-DI set method if you plan to reuse your code with default container.

Installation

Best way to install is using Composer:

composer require juliangut/slim-php-di

Then require_once the autoload file:

require_once './vendor/autoload.php';

Usage

use Jgut\Slim\PHPDI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;

$settings = require __DIR__ . 'settings.php';
$container = ContainerBuilder::build($settings);

// Register services the "default" way
$container['service_one'] =  function (ContainerInterface $container) {
    return new ServiceOne;
};

// Register services the PHP-DI way
$container->set('service_two', function (ContainerInterface $container) {
    return new ServiceTwo($container->get('service_two'));
});

$app = new App($container);

// Set your routes

$app->run();

Configuration

You can define your services definitions in settings array as it's done with default Slim Container

use Jgut\Slim\PHPDI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;
use function DI\get;
use function DI\object;

$settings = [
    'settings' => [
        // Specific settings for PHP-DI container
        'php-di' => [
            'use_autowiring' => true,
            'use_annotations' => true,
        ],
    ],
    // Services definitions
    'my.parameter' => 'value',
    'Foo' => function (ContainerInterface $container) {
        return new Foo($container->get('my.parameter'));
    },
    'Bar' => [get('BarFactory'), 'create'],
    'Baz' => object('Baz'),
];
$container = ContainerBuilder::build($settings);
$app = new App($container);

Or you can separate service definitions and load them on container build.

use Jgut\Slim\PHPDI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;
use function DI\get;
use function DI\object;

$settings = [
    'settings' => [
        // Specific settings for PHP-DI container
        'php-di' => [
            'use_autowiring' => true,
            'use_annotations' => true,
        ],
    ],
];

$definitions = [
    'my.parameter' => 'value',
    'Foo' => function (ContainerInterface $container) {
        return new Foo($container->get('my.parameter'));
    },
    'Bar' => [get('BarFactory'), 'create'],
    'Baz' => object('Baz'),
];

$container = ContainerBuilder::build($settings, $definitions);
$app = new App($container);

Available PHP-DI settings

PHP-DI container is configured under php-di settings key as shown in previous examples.

  • use_autowiring boolean, whether or not to use autowiring (true by default)
  • use_annotations boolean, whether or not to use annotations (false by default)
  • ignore_phpdoc_errors boolean, whether or not to ignore phpDoc errors on annotations (false by default)
  • proxy_path path where PHP-DI creates its proxy files
  • definitions_cache \Doctrine\Common\Cache\Cache

Refer to PHP-DI documentation to learn more about container configurations, specially on how to use definitions which is the key element on using PHP-DI.

In order for you to use annotations you have to require doctrine/annotations. See here

In order for you to use definitions cache you have to require doctrine/cache. See here

Registration order

Services are registered in the following order:

  • Default Slim services
  • Definitions on settings array
  • Definitions provided as second argument on building

In order to override default Slim services add them in settings array or provided to build method.

use Jgut\Slim\PHPDI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;

// Overrides default services
$settings = [
    'errorHandler' => function (ContainerInterface $container) {
        return new MyErrorHandler($container->get('settings')['displayErrorDetails']);
    },
];

// Overrides default services and those in settings array
$definitions = [
    'foundHandler' => function () {
        return new MyRequestResponse;
    },
];

$container = ContainerBuilder::build($settings, $definitions);
$app = new App($container);

Important note

Be aware that if you use cache definitions for all your services must provided at container creation, and more importantly do not set any definitions later on as it is not allowed at runtime when using cache (setting values at runtime is allowed though).

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.