php-di/kernel

Kernel for PHP-DI applications

0.4.1 2017-07-16 13:45 UTC

This package is auto-updated.

Last update: 2024-10-19 20:46:30 UTC


README

Kernel for building modules with PHP-DI.

Build Status Coverage Status

Introduction

The Kernel let's you build an application based on PHP-DI modules.

Installation

composer require php-di/kernel

Usage

The kernel's role is to create the container. It does so by registering all the configuration files of the modules we ask it to load:

$kernel = new Kernel([
    'twig/twig',
    'doctrine/dbal',
    'vendor/app',
]);

$container = $kernel->createContainer();

If you want to register configuration on the container, you can:

  • create a module - this is the recommended solution, read the next sections to learn more

  • or set the configuration directly - this is useful in micro-frameworks or micro-applications:

    $kernel = new Kernel();
    $kernel->addConfig([
        'db.host' => 'localhost',
    ]);

Installing a module

To install a 3rd party module:

  • install the package using Composer

  • add it to the list of modules your kernel will load, for example:

    $kernel = new Kernel([
        'twig/twig',
    ]);

Creating a module

  1. the Composer package name is the module name
  2. create a resource directory in your package, usually res/
  3. create as many PHP-DI configuration files as needed in res/config/

That's it. Here is what your package should look like:

res/
    config/
        config.php
    ...
src/
    ...
composer.json

When the module is registered in the kernel like this:

$kernel = new Kernel([
    'foo/bar',
]);

all the files in vendor/foo/bar/res/config/*.php will be loaded.

Your main application will probably contain configuration files too: it is also a module. Since it may not have a package name in composer.json you will need to set one. You can name it app, for example:

{
    "name": "app",
    "require": {
        // ...
    }
}

That way you can let the kernel load your application as a module:

$kernel = new Kernel([
    'app',
]);

Environments

Applications often need to behave differently according to the environment: dev, prod, etc.

PHP-DI's Kernel let you write config for specific environments through a simple convention:

res/
    config/
        config.php
        env/
            dev.php
            prod.php
    ...

You can then instruct the environment to load:

$kernel = new Kernel($modules, 'dev'); // dev environment
$kernel = new Kernel($modules, 'prod'); // prod environment

Note that environments are optional.