Git Product home page Git Product logo

kos's Introduction

kos

php concise container/di

usage

Install

kos in packagist:https://packagist.org/packages/jesusslim/kos

Install:

composer require jesusslim/kos

Methods

getInstance()

get instance of kos\Container.

map($key,$class = null)

map a class or closure to container.

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.

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

isMapped($key)

get($key,$params = [])

if $key is mapped in container as an instance,return the instance;

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

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

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

the values in $params can be used as the params of the constructor of class or the params of closure;

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.

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.

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

others

kos is a concise container/DI lib for simple use.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.