juliangut/slim-php-di

Slim Framework PHP-DI container integration

2.0.1 2018-09-17 23:24 UTC

README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Slim Framework PHP-DI container integration

PHP-DI (v6) dependency injection container integration for Slim Framework.

In order to allow possible services out there expecting the container to be Slim\Container (extending Pimple) and thus implementing ArrayAccess, it has been added to default provided container.

You are encouraged to use array syntax for assignment instead of PHP-DI set method if you plan to reuse your code with default Slim 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\App which will build PHP-DI container.

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

$settings = require __DIR__ . '/settings.php';
$configuration = new Configuration($settings);
$configuration->setDefinitions('/path/to/definitions/file.php');

$app = new App($configuration);
$container = $app->getContainer();

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

// \Jgut\Slim\PHPDI\Container accepts registering services à la Pimple
$container['service_two'] =  function (ContainerInterface $container) {
    return new ServiceTwo();
};

// Set your routes

$app->run();

ContainerBuilder

Or build container and provide it to default Slim App.

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContinerBuilder;
use Slim\App

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

// ...

$app = new App($container);

// ...

$app->run();

Configuration

use Jgut\Slim\PHPDI\Configuration;

$settings = [
    'useAnnotations' => true,
    'ignorePhpDocErrors' => true,
];
$configuration = new Configuration($settings);

// Can be set after creation
$configuration->setProxiesPath(sys_get_temp_dir());
$configuration->setDefinitions('/path/to/definitions/file.php');

PHP-DI settings

  • useAutoWiring, whether or not to use auto wiring (true by default)
  • useAnnotations, whether or not to use annotations (false by default)
  • useDefinitionCache, whether or not to use definition cache (false by default)
  • ignorePhpDocErrors, whether or not to ignore phpDoc errors on annotations (false by default)
  • wrapContainer, wrapping container (none by default)
  • proxiesPath, path where PHP-DI creates its proxy files (none by default)
  • compilationPath, path to where PHP-DI creates its compiled container (none by default)

Refer to PHP-DI documentation to learn more about container configurations.

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

Additional settings

  • containerClass, container class that will be built. Must implement \Interop\Container\ContainerInterface, \DI\FactoryInterface and \DI\InvokerInterface (\Jgut\Slim\PHPDI\Container by default)
  • definitions, an array of paths to definition files/directories or arrays of definitions. Definitions are loaded in order of appearance

Services registration order

Services are registered in the following order:

  • Default Slim services
  • Definitions provided in configuration in the order they are in the array

Slim's settings direct access

Default \Jgut\Slim\PHPDI\Container container allows direct access to Slim's settings array values by prepending 'settings.' to setting key. If setting is not defined normal container's ContainerValueNotFoundException is thrown

$container->get('settings')['displayErrorDetails'];
$container->get('settings.displayErrorDetails');

Migration from 1.x

  • Minimum Slim version is now 3.9
  • PHP-DI have been upgraded to v6. Review PHP-DI documentation: container compilation, create/autowire functions, etc
  • PHP-DI settings have been moved into Configuration object. This object accepts an array of settings on instantiation so it's just a matter of providing the settings to it
  • Configuration settings names have changed from snake_case to camelCase
  • Definitions are included in Configuration object rather than set apart. Now you can as well define path(s) to load definition files from

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.