您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天laravel-20160925|Pipeline.php

2016-07-23 09:15 483 查看
<?php

namespace Illuminate\Pipeline;

use Closure;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
// use name space like Pipeline Contract
class Pipeline implements PipelineContract
{//class Pipeline implements PipelineContract
/**
* The container implementation.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;// The container implementation.
// one instance of this implements
// about this Pipeline Contract

/**
* The object being passed through the pipeline.
*
* @var mixed
*/
protected $passable;//a flag about this The object being passed through the pipeline.

/**
* The array of class pipes.
*
* @var array
*/
protected $pipes = [];//The array of class pipes.

/**
* The method to call on each pipe.
*
* @var string
*/
protected $method = 'handle';// The method to call on each pipe.
// get this handle

/**
* Create a new class instance.
*
* @param  \Illuminate\Contracts\Container\Container  $container
* @return void
*/
public function __construct(Container $container)
{
$this->container = $container;
}// Create a new class instance about this container

/**
* Set the object being sent through the pipeline.
*
* @param  mixed  $passable
* @return $this
*/
public function send($passable)
{
$this->passable = $passable;// a passable

return $this;// use this pipeline class instance back or return this
}//Set the object being sent through the pipeline.

/**
* Set the array of pipes.
*
* @param  array|mixed  $pipes
* @return $this
*/
public function through($pipes)
{
$this->pipes = is_array($pipes) ? $pipes : func_get_args();//this is a array

return $this;//return this
}// Set the array of pipes

/**
* Set the method to call on the pipes.
*
* @param  string  $method
* @return $this
*/
public function via($method)
{
$this->method = $method;//like via like

return $this;
}//Set the method to call on the pipes.

/**
* Run the pipeline with a final destination callback.
*
* @param  \Closure  $destination
* @return mixed
*/
public function then(Closure $destination)
{
$firstSlice = $this->getInitialSlice($destination);//first Slice

$pipes = array_reverse($this->pipes);// array_reverse get a un sort array

return call_user_func(// call a function
array_reduce($pipes, $this->getSlice(), $firstSlice), $this->passable
);
}//Run the pipeline with a final destination callback.

/**
* Get a Closure that represents a slice of the application onion.
*
* @return \Closure
*/
protected function getSlice()
{// Get a Closure that represent a slice of the application onion
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
// If the pipe is an instance of a Closure, we will just call it directly but
// otherwise we'll resolve the pipes out of the container and call it with
// the appropriate method and arguments, returning the results back out.
if ($pipe instanceof Closure) {
return call_user_func($pipe, $passable, $stack);// call a function
} else {
list($name, $parameters) = $this->parsePipeString($pipe);// get a list

return call_user_func_array([$this->container->make($name), $this->method],
array_merge([$passable, $stack], $parameters));
}//call function
};//other return a function
};//just a function
}

/**
* Get the initial slice to begin the stack call.
*
* @param  \Closure  $destination
* @return \Closure
*/
protected function getInitialSlice(Closure $destination)
{
return function ($passable) use ($destination) {
return call_user_func($destination, $passable);
};// return function as a use way,never use like it
}// Get the initial slice to begin the stack call

protected function jingshan(Closure $a){
return function ($c) use($a){
return call_user_func($a,$c);
};
}//maybe it is a good type , a method about this function

/**
* Parse full pipe string to get name and parameters.
*
* @param  string $pipe
* @return array
*/
protected function parsePipeString($pipe)
{
list($name, $parameters) = array_pad(explode(':', $pipe, 2), 2, []);// very good way to make it!

if (is_string($parameters)) {
$parameters = explode(',', $parameters);
}// if a parameters

return [$name, $parameters];// return array
}//Parse full pipe string to get name and parameters.
//
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: