Mooti Container
, (*1)
A simple service container written in php, (*2)
Installation
You can install this through packagist, (*3)
$ composer require mooti/container
Run the tests
If you would like to run the tests. Use the following:, (*4)
$ ./vendor/bin/phpunit
Usage
Create the container and add a service, (*5)
use Mooti\Container\Container;
$container = new Container();
$container->set('logger', function () { return new Logger();});
//returns a new instance of Logger. Subsequent calls return the same object
$logger = $container->get('logger');
You also define multiple services by implementing ServiceProviderInterface and then implementing it's getServices method to return an associative array. If the element is a callable function it will be called and the result will be returned., (*6)
use Mooti\Container\ContainerAware;
class ServiceProvider implements ServiceProviderInterface
{
public function getServices()
{
return [
'logger' => function () { return new Logger();},
'message' => 'Hello World'},
];
}
}
To us the container you will need to use the ContainerAware trait in you class. You will then be able to use the get method to get any items from the container, (*7)
use Mooti\Container\ContainerAware;
class App
{
use ContainerAware;
public function run()
{
$config = $this->get('config');
$message = $this->get('message');
$logger->alert($message);
}
}
So, putting it all together you will get something like:, (*8)
use Mooti\Container\Container;
use Mooti\Container\ContainerAware;
use Mooti\Container\ServiceProvider\ServiceProviderInterface;
class App
{
use ContainerAware;
public function run()
{
$config = $this->get('config');
$message = $this->get('message');
$logger->alert($message);
}
}
class ServiceProvider implements ServiceProviderInterface
{
public function getServices()
{
return [
'logger' => function () { return new Logger();},
'message' => 'Hello World'},
];
}
}
$container = new Container();
$serviceProvider = new ServiceProvider::class;
$app = new App();
$container->registerServices($serviceProvider);
$app->setContainer($container);
// Should log "Hello World"
$app->run();
The ContainerAware trait extends functionality from mooti/factory. You can instantiate an object using the method createNew and it will try to inject the container into the object if it users the ContainerAware trait., (*9)
use Mooti\Container\Container;
use Mooti\Container\ContainerAware;
use Mooti\Container\ServiceProvider\ServiceProviderInterface;
class FooBar
{
use ContainerAware;
private $foo;
private $bar;
public function __construct($foo, $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}
public function logMessage()
{
$config = $this->get('config');
$message = $this->get('message');
$logger->alert($this->foo . ' ' . $this->bar . ' ' . $message);
}
}
class App
{
use ContainerAware;
public function run()
{
$fooBar = $this->createNew(FooBar::class, ['test 1', 'test 2']);
$fooBar->logMessage();
}
}
class ServiceProvider implements ServiceProviderInterface
{
public function getServices()
{
return [
'logger' => function () { return new Logger();},
'message' => 'Hello World'},
];
}
}
$container = new Container();
$serviceProvider = new ServiceProvider::class;
$app = new App();
$container->registerServices($serviceProvider);
$app->setContainer($container);
// Should log "test 1 test 2 Hello World"
$app->run();