Kernel for building modules with PHP-DI.
The Kernel let's you build an application based on PHP-DI modules.
composer require php-di/kernel
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', ]);
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', ]);
- the Composer package name is the module name
- create a resource directory in your package, usually
res/
- 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',
]);
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.