码迷,mamicode.com
首页 > 其他好文 > 详细

每天laravel-20160709|CallEvent

时间:2016-04-08 15:32:57      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:laravel

<?php

namespace Illuminate\Console\Scheduling;

use LogicException;
use InvalidArgumentException;
use Illuminate\Contracts\Container\Container;
// namespace to use
class CallbackEvent extends Event
{
    /**
     * The callback to call.
     *
     * @var string
     */
    protected $callback;// The callback function, a instance to be call

    /**
     * The parameters to pass to the method.
     *
     * @var array
     */
    protected $parameters; // The parameters to pass to the method.

    /**
     * Create a new event instance.
     *
     * @param  string  $callback
     * @param  array  $parameters
     * @return void
     *
     * @throws \InvalidArgumentException
     */
    public function __construct($callback, array $parameters = [])// Create a new event instance.
    {
        $this->callback = $callback;// set method name , call callback function
        $this->parameters = $parameters;// set variable, that is the parameters.

        if (! is_string($this->callback) && ! is_callable($this->callback)) {// if has something wrong
            throw new InvalidArgumentException(
                ‘Invalid scheduled callback event. Must be string or callable.‘
            );
        }
    }// a callback event be done like a class or a object

    /**
     * Run the given event.
     *
     * @param  \Illuminate\Contracts\Container\Container  $container
     * @return mixed
     *
     * @throws \Exception
     */
    public function run(Container $container)// run the normal or right event method
    {// parameter is Container
        if ($this->description) {
            touch($this->mutexPath());// edit something attr
        }// more time no description

        try {
            $response = $container->call($this->callback, $this->parameters);
        } finally {
            $this->removeMutex();
        }// try do something , or remove

        parent::callAfterCallbacks($container);

        return $response;
    }

    /**
     * Remove the mutex file from disk.
     *
     * @return void
     */
    protected function removeMutex()
    {
        if ($this->description) {
            @unlink($this->mutexPath());
        }
    }// a wrong or

    /**
     * Do not allow the event to overlap each other.
     *
     * @return $this
     *
     * @throws \LogicException
     */
    public function withoutOverlapping()
    {
        if (! isset($this->description)) {
            throw new LogicException(
                "A scheduled event name is required to prevent overlapping. Use the ‘name‘ method before ‘withoutOverlapping‘."
            );
        }

        return $this->skip(function () {
            return file_exists($this->mutexPath());
        });
    }// a over loop

    /**
     * Get the mutex path for the scheduled command.
     *
     * @return string
     */
    protected function mutexPath()
    {
        return storage_path(‘framework/schedule-‘.sha1($this->description));
    }// get the file path

    /**
     * Get the summary of the event for display.
     *
     * @return string
     */
    public function getSummaryForDisplay()
    {
        if (is_string($this->description)) {
            return $this->description;
        }

        return is_string($this->callback) ? $this->callback : ‘Closure‘;
    }// Get the summary of the event for display.
}


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

每天laravel-20160709|CallEvent

标签:laravel

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

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