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

    /**
     * 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\[email protected]‘, [
                    ‘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\[email protected]‘, [
            ‘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
}
时间: 2024-08-28 17:31:24

[李景山php]每天laravel-20160908|Dispatcher-8的相关文章

[李景山php]每天laravel-20160920|Writer-2

    //2016-07-20     /**      * Register a file log handler.      *      * @param  string  $path      * @param  string  $level      * @return void      */     public function useFiles($path, $level = 'debug')     {         $this->monolog->pushHandle

[李景山php]每天laravel-20160902|Dispatcher-2

/**  * Register an event listener with the dispatcher.  *  * @param  string|array  $events  * @param  mixed  $listener  * @param  int  $priority  * @return void  */ public function listen($events, $listener, $priority = 0) {// Register an event liste

[李景山php]每天laravel-20160831|EventServiceProvider

<?php namespace Illuminate\Events; use Illuminate\Support\ServiceProvider; class EventServiceProvider extends ServiceProvider {// Event Service Provider extends Service Provider     /**      * Register the service provider.      *      * @return void

[李景山php]每天laravel-20160901|Dispatcher-1

namespace Illuminate\Events; use Exception; use ReflectionClass; use Illuminate\Support\Str; use Illuminate\Container\Container; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; use Illu

[李景山php]每天laravel-20161112|Factory-3.php

    /**      * Parse a class based composer name.      *      * @param  string  $class      * @param  string  $prefix      * @return array      */     protected function parseClassEvent($class, $prefix)     {//Parse a class based composer name.      

[李景山php]每天laravel-20161110|Factory.php

<?php namespace Illuminate\View; use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Str; use InvalidArgumentException; use Illuminate\Contracts\Support\Arrayable; use Illuminate\View\Engines\EngineResolver; use Illuminate\Contracts\Event

[李景山php]每天laravel-20161111|Factory-2.php

    /**      * Get the evaluated view contents for the given view.      *      * @param  string  $view      * @param  array   $data      * @param  array   $mergeData      * @return \Illuminate\Contracts\View\View      */     public function make($vie

[李景山php]每天laravel-20161021|Request.php-2

/**  * Determine if the current request URL and query string matches a pattern.  *  * @param  mixed  string  * @return bool  */ public function fullUrlIs() {// check string like URL     $url = $this->fullUrl();     foreach (func_get_args() as $patter

[李景山php]每天laravel-20160903|Dispatcher-3

   /**     * Setup a wildcard listener callback.     *     * @param  string  $event     * @param  mixed  $listener     * @return void     */    protected function setupWildcardListen($event, $listener)    {        $this->wildcards[$event][] = $this->