starbug / di
A dependency injection container initialization library.
Installs: 4 576
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/starbug/di
Requires
- php-di/php-di: ^6.0
Requires (Dev)
This package is auto-updated.
Last update: 2025-10-17 11:30:34 UTC
README
A dependecy injection meta package and container initialization library for PHP-DI.
What's included
- PHP-DI/PHP-DI The actual dependency injection library.
- ContainerFactory - A configurable factory for initializing containers and running post initialization steps.
- DefaultConfigurationHandler - A default handler for the factory that loads container definitions from
etc/di.phpin the root package and overrides frometc/di.phpin modules that provide it. It can also set the default timezone and register an error handler after the container is initialized.
Usage
Application bootstrapping
The simplest way to use this library is to bootstrap your application with it and let it use the default configuration handler.
include("vendor/starbug/di/bootstrap/init.php"); // You now have a Psr\Container\ContainerInterface instance $application = $container->make("MyApp");
Direct initialization
To do the same thing directly.
use Starbug\DI\ContainerFactory; include("vendor/autoload.php"); $container = ContainerFactory::withDefaultHandler() ->create(); // You now have a Psr\Container\ContainerInterface instance $application = $container->make("MyApp");
Definitions
The default handler will look for a file at etc/di.php to load definitions from. To customize MyApp our file might look like this.
return [
"MyApp" => function () {
$app = new MyApp();
$app->addMiddleware(new MyMiddleware());
return $app;
}
];
See the PHP-DI documentation on PHP definitions.
Modules
If you are using starbug/composer-modules-plugin, the default handler will also check each module for an etc/di.php file to load additional definitions from.
Custom Handlers
To use custom handlers, call withHandlers instead of withDefaultHandler and pass your handlers.
use Starbug\DI\ContainerFactory; $container = ContainerFactory::withHandlers( new MyHandler(), new SecondHandler() )->create();
Alternatively, if you don't want to use the static helper method you can easily do it manually.
use Starbug\DI\ContainerFactory; $factory = new ContainerFactory(); $factory->addHandler(new MyHandler()); $container = $factory->create();