GithubHelp home page GithubHelp logo

guoyu07 / slim-php-di Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juliangut/slim-php-di

0.0 0.0 0.0 161 KB

Slim Framework PHP-DI container integration

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

slim-php-di's Introduction

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.

slim-php-di's People

Contributors

juliangut avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.