码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-06-28 13:19:28      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:listener   create   function   return   

/**
     * 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}


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1793500

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

标签:listener   create   function   return   

原文地址:http://jingshanls.blog.51cto.com/3357095/1793500

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!