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

[李景山php]每天laravel-20160908|Dispatcher-8

2016-06-28 08:24 651 查看
/**
* Create the class based event callable.
*
* @param  string  $listener
* @param  \Illuminate\Container\Container  $container
* @return callable
*/
protected function createClassCallable($listener, $container)
{// create the class based event callable.
list($class, $method) = $this->parseClassCallable($listener);// set this parameters like class and method// i like this way more ,
if ($this->handlerShouldBeQueued($class)) {// if true
return $this->createQueuedHandlerCallable($class, $method);// use a wrap class
} else {            return [$container->make($class), $method];//  a make and a method
}
}    /**
* Parse the class listener into class and method.
*
* @param  string  $listener
* @return array
*/
protected function parseClassCallable($listener)
{
$segments = explode('@', $listener);// if has @ ,use it ,

return [$segments[0], count($segments) == 2 ? $segments[1] : 'handle'];// else return it
}//parse Class Call able

/**
* Determine if the event handler class should be queued.
*
* @param  string  $class
* @return bool
*/
protected function handlerShouldBeQueued($class)
{// handler shouldBeQueued
try {            return (new ReflectionClass($class))->implementsInterface(                'Illuminate\Contracts\Queue\ShouldQueue'
);// get the reflection Class
} catch (Exception $e) {            return false;
}
}    /**
* Create a callable for putting an event handler on the queue.
*
* @param  string  $class
* @param  string  $method
* @return \Closure
*/
protected function createQueuedHandlerCallable($class, $method)
{// Create a callable for putting an event handler on the queue.
return function () use ($class, $method) {
$arguments = $this->cloneArgumentsForQueueing(func_get_args());// get the arguments// get the arguments, get all arguments like a array
// prepare this argument
if (method_exists($class, 'queue')) {// check has method
$this->callQueueMethodOnHandler($class, $method, $arguments);// call QueueMethodOnHandler
} else {
$this->resolveQueue()->push('Illuminate\Events\CallQueuedHandler@call', [                    'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
}// other ,set something wrong
};
}    /**
* Clone the given arguments for queueing.
*
* @param  array  $arguments
* @return array
*/
protected function cloneArgumentsForQueueing(array $arguments)
{        return array_map(function ($a) {            return is_object($a) ? clone $a : $a;
}, $arguments);// just a array_map
}// done every thing just a good way
// too beautiful

/**
* Call the queue method on the handler class.
*
* @param  string  $class
* @param  string  $method
* @param  array  $arguments
* @return void
*/
protected function callQueueMethodOnHandler($class, $method, $arguments)
{
$handler = (new ReflectionClass($class))->newInstanceWithoutConstructor();// a handler method

$handler->queue($this->resolveQueue(), 'Illuminate\Events\CallQueuedHandler@call', [            'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);// set the handler
}//Call the queue method on the handler class

/**
* Remove a set of listeners from the dispatcher.
*
* @param  string  $event
* @return void
*/
public function forget($event)
{        if (Str::contains($event, '*')) {
unset($this->wildcards[$event]);// unset all
} else {
unset($this->listeners[$event], $this->sorted[$event]);// unset this point just you want
}
}// forget , like remove a set of listeners from the dispatcher

/**
* Forget all of the pushed listeners.
*
* @return void
*/
public function forgetPushed()
{
foreach ($this->listeners as $key => $value) {            if (Str::endsWith($key, '_pushed')) {
$this->forget($key);
}
}
}// forget Pushed

/**
* Get the queue implementation from the resolver.
*
* @return \Illuminate\Contracts\Queue\Queue
*/
protected function resolveQueue()
{        return call_user_func($this->queueResolver);// just a magic call
}//resolve Queue

/**
* Set the queue resolver implementation.
*
* @param  callable  $resolver
* @return $this
*/
public function setQueueResolver(callable $resolver)
{
$this->queueResolver = $resolver;        return $this;
}// return self is better}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Create listener function