A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection.
Requires PHP >= 7.0
Features:
Cons:
Don't, at least not at this stage. The container is not battle tested and is only in use on some small production systems. At this point, I still might change the public API or break functionality. Feel free to try this out on small side projects. Obviously, I appreciate everyone who wants to sacrifice their time to contribute.
This package might seem very heavy for a service container, but after a short warmup the compiled container is blazing fast and has almost no overhead (3 classes/files). Binding and resolving services dynamically is slower but still won't impact performance in real-world application.
The container follows PSR-4
autoloading and can be installed using composer:
$ composer require clancats/container
The full documentation can be found on clancats.io
Following is just a rough example, a much more detailed and explained guide can be found here: Getting Started
Our target directory structure will look like this:
app.php
app.container
composer.json
cache/ # make sure this is writable
src/
Human.php
SpaceShip.php
To demonstrate how to use this service container we need to create two classes a SpaceShip
and a Human
.
Create a new php file src/Human.php
:
class Human
{
public $name;
public function setName(string $name) {
$this->name = $name;
}
}
Create another php file src/SpaceShip.php
:
class SpaceShip
{
protected $captain; // every ship needs a captain!
public function __construct(Human $captain) {
$this->captain = $captain;
}
public function ayeAye() {
return 'aye aye captain ' . $this->captain->name;
}
}
A container file allows you to bind your services & parameters using a simple meta-language.
Note: This feature is entirely optional if you prefer binding your services in PHP itself read: Service Binding
Create a new file called app.container
in your applications root folder.
@malcolm: Human
- setName('Reynolds')
@firefly: SpaceShip(@malcolm)
Now we need to parse the container file and compile it as a new class. For this task, we create the app.php
file. There you need to require the composer autoloader and require your source files or configure composer to autoload the classes from the src/
directory.
require "vendor/autoload.php";
// for the consistency of the example I leave this here
// but I strongly recommend to autolaod your classes with composer.
require "src/SpaceShip.php";
require "src/Human.php";
$factory = new \ClanCats\Container\ContainerFactory(__DIR__ . '/cache');
$container = $factory->create('AppContainer', function($builder)
{
// create a new container file namespace and parse our `app.container` file.
$namespace = new \ClanCats\Container\ContainerNamespace();
$namespace->parse(__DIR__ . '/app.container');
// import the namespace data into the builder
$builder->importNamespace($namespace);
});
Note: Make sure the ../cache
directory is writable.
The variable $container
contains now a class instance named AppContainer
.
echo $container->get('firefly')->ayeAye(); // "aye aye captain Reynolds"
The MIT License (MIT). Please see License File for more information.