2017 © Pedro Peláez
 

library pinject

inject in php

image

jesusslim/pinject

inject in php

  • Friday, May 26, 2017
  • by jesusslim
  • Repository
  • 3 Watchers
  • 6 Stars
  • 246 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 14 % Grown

The README.md

pinject

Inject in PHP !, (*1)

usage

[English] Chinese, (*2)

Install

pinject in packagist:https://packagist.org/packages/jesusslim/pinject, (*3)

Install:, (*4)

composer require jesusslim/pinject

If your composer not allowed dev-master,add this config, (*5)

"minimum-stability": "dev"

into your composer.json., (*6)

Injector

The InjectorInterface decalare some function for Inject,like, (*7)

map //map a concrete or class or object or closure to the inject container
get //get things your mapped
produce //produce concrete

The Injector class implements InjectorInterface.Make your own class which needs to use inject extends the Injector class.Then use the functions:, (*8)

//map a data or object
$injector->mapData('test',12345);
$injector->mapData(Student::class,new Student('Slim'));

//map a class
$injector->mapSingleton(StudentInterface::class,GoodStudent::class);

//produce
//if the key is found in mapped data,objects,it will return the things we mapped.
//if the key is found in mapped classes,it will check if this class is been produced,if it's been produced,it return the concrete that produced before,else return a new concrete of this class.
//if this key not found in any map,it will try to reflect this class unless we use the function mustReg() to make sure all the things can be produced should be mapped first.
$injector->produce(StudentInterface::class);

//call an function
//it will fill the paramters of this function with the concrete produced by pinject.
$injector->call(function(Student $std){
    ...
});

//call an function in class
//it will call a function in class.it will try to find the class from pinject if it has been produced or reflect it.and fill the paramters with concrete produced by pinject.
$injector->callInClass($class_name,$action,$out_params);

Chains

We can use Chains to do some chaining operations., (*9)

Example:, (*10)

$chains = new Chains($app);
//here $app is an Injector
$chains->chain(RequestHandler::class)
->chain(function($data,$next){
    $r = Auth::checkToken($data['token']);
    if($r !== true){
        dump("Token wrong");
    }else{
        $next($data);
    }
})
->chain(Student::class)
->chain(function($data){
    dump($data);
})
->action('handle')
->run();
//or use runWith($your_last_chain_func);

We can chain a Closure or a class into the chains.If it's a class,it will call the method named 'handle'.Every Closure or method for handle,should have two paramters:the data passing by in the chains,and the next handler.And at last of each chain,we shoule call the next handler if it's success., (*11)

Another way to use chains:, (*12)

Another way to use chains is that use runWild instead of run/runWith,and it's more like the useage of Martini/Injector in golang., (*13)

Example:, (*14)

$chains = new \Inject\Chains($app);
$app->mapData('any_param_here','Example');
$the_given_params_for_handlers = [
    'seems_wild' => 'OK'
];
$rtn = $chains->chain(function($any_param_here,$seems_wild){
    var_dump($any_param_here.' is '.$seems_wild);
})->chain(function(){
    return "END";
})->data($the_given_params_for_handlers)
->runWild();
var_dump($rtn);

As we see here,the difference between runWild and run/runWith is that,runWild support any kind of handlers,and any handler return anything will break the loop and return the result., (*15)

The Versions

26/05 2017

dev-master

9999999-dev

inject in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

inject

26/05 2017

1.2.3

1.2.3.0

inject in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

inject

26/05 2017

dev-release/1.2.2

dev-release/1.2.2

inject in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

inject

23/02 2017

dev-feature/handler_wild

dev-feature/handler_wild

inject in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

inject

21/09 2016

dev-develop

dev-develop

inject in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Avatar jesusslim

inject