2017 © Pedro Peláez
 

library kos

php container/di

image

jesusslim/kos

php container/di

  • Thursday, May 17, 2018
  • by jesusslim
  • Repository
  • 1 Watchers
  • 1 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

kos

php concise container/di, (*1)

usage

Install

kos in packagist:https://packagist.org/packages/jesusslim/kos, (*2)

Install:, (*3)

composer require jesusslim/kos

Methods

getInstance()

get instance of kos\Container., (*4)

map($key,$class = null)

map a class or closure to container., (*5)

Container::getInstance()->map(Foo::class);
Container::getInstance()->map('foo',Foo::class);
Container::getInstance()->map('bar',function($foo,$bar){return $foo + $bar;});

mapInstance($key,$instance)

map an instance to container., (*6)

Container::getInstance()->mapInstance('foo',new Foo());

isMapped($key)

get($key,$params = [])

if $key is mapped in container as an instance,return the instance;, (*7)

if $key is mapped in container as a class,return the an instance of the class;, (*8)

if $key is mapped in container as a closure,return the result of the closure;, (*9)

if $key is not mapped in container,reflect $key and return the instance/result;, (*10)

the values in $params can be used as the params of the constructor of class or the params of closure;, (*11)

class Foo{

    private $bar;

    public function __construct($bar)
    {
        $this->bar = $bar;
    }

    public function getBar(){
        return $this->bar;
    }
}
$container = Container::getInstance();

$container->map(Foo::class);
$container->mapInstance('bar',677);
$container->map('sum',function(Foo $foo,$inc){
    return $foo->getBar()+$inc;
});

$foo = $container->get(Foo::class);
/* @var Foo $foo */
echo $foo->getBar();
//result is 677

$foo = $container->get(Foo::class,['bar' => 688]);
echo $foo->getBar();
//result is 688;

echo $container->get('sum',['inc' => 100]);
//result is 777

invoke(Closure $closure,$params = [])

invoke a closure , the params of the closure will be filled by the container., (*12)

echo $container->invoke(function(Foo $foo,$inc){
    return $foo->getBar()+$inc;
},['inc' => 100]);
//result is 777

invokeClass($class,$method,$params = [])

invoke a class->method , the params of the closure will be filled by the container., (*13)

echo $container->invokeClass(Foo::class,'getBar',[]);
//result is 677

others

kos is a concise container/DI lib for simple use., (*14)

and pinject is a lib for the complex use,like caching instances,ArrayAccess implements and chaining operations/workflow with container., (*15)

The Versions

17/05 2018

dev-master

9999999-dev

php container/di

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

container di

17/05 2018

1.0

1.0.0.0

php container/di

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

container di