Almari is a micro IoC framework which implement service locator and facade pattern
You can use composer to install Almari
composer require lotus/almari
Or you can put it on composer.json
"require": {
"tonjoo/almari": "*"
}
The main container class is Lotus\Almari\Container
use Lotus\Almari\Container as Container
$app = new Container();
// Create service, return new Foo instance
$app->bind('foo',function(){
return new Foo();
});
$newFoo = $app->make('foo');
$newFoo2 = $app->make('foo');
use Lotus\Almari\Container as Container
$app = new Container();
$foo = new Foo();
// Share an instance
$app->share('fooSingeleton',$foo);
// Share an instance (lazy load)
$app->shareDeferred('fooSingeleton',function(){
return new Foo();
});
// Share instance using array access
$app['fooSingeleton'] = $foo;
$myFoo = app->get('foo',$defaultValue);
// Or using array access
$myFoo = app['foo'];
Full example here : https://github.com/tonjoo/almari-boilerplate
// Facading $foo to FooFacade
$aliasMapper = AliasMapper::getInstance();
$alias['FooFacade'] = 'MyProject\MyPackage\Facade\FooFacade';
$aliasMapper->facadeClassAlias($alias);
//Register container to facade
MyFacade::setFacadeContainer($app);
FooFacade::greet("Todi");