mooti / container
simple service container
Requires
- php: >=5.5.9
- container-interop/container-interop: ^1.1
- mooti/factory: ^1.0
Requires (Dev)
- phpunit/phpunit: 5.1.*
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-11-09 20:50:40 UTC
README
A simple service container written in php
Installation
You can install this through packagist
$ composer require mooti/container
Run the tests
If you would like to run the tests. Use the following:
$ ./vendor/bin/phpunit
Usage
Create the container and add a service
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.
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
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:
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.
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();