Laravel框架学习 -- php artisan down/up

    因为某种缘由,公司总体框架由python的flask框架,转换为php的laravel。在断断续续几个月的时间里,边继续写着flask框架,边学着laravel。说下本身如今的状态吧。前段时间差很少都在个1-2点睡觉,大概四月份有段时间居然到了3-4点才睡的地步。php

    路漫漫其修远兮,总感受时间不够用的。大概是本身以前浪费的时间太多了,是时候还上了。python


    laravel文档中文版的,大概看到过三个。随便找个看看就能够了。http://laravel-china.org/docs/5.1 laravel

# 输入 php artisan 便可看到所有可用命令

  down                 Put the application into maintenance mode
  up                   Bring the application out of maintenance mode

    总体流程大致(由于详细的我也不是很清楚╮(╯_╰)╭)说下吧。flask

1、命令的实现

    1. 做为服务提供者,加载到程序中。

// config/app.php 中。
'providers' => [
    // 这个即是 laravel自带的 artisan 命令提供者
    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
]

    2.而后找到 Up/Down 命令入口

/**
 * Register the command.
 *
 * @return void
 */
protected function registerUpCommand()
{
    $this->app->singleton('command.up', function () {
        return new UpCommand;
    });
}


/**
 * Register the command.
 *
 * @return void
 */
protected function registerDownCommand()
{
    $this->app->singleton('command.down', function () {
        return new DownCommand;
    });
}

    3.1 DownCommand实现

class DownCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'down';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Put the application into maintenance mode';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // 关键点: 在当前存储目录/framework 下面建立一个 down文件
        touch($this->laravel->storagePath().'/framework/down');

        $this->comment('Application is now in maintenance mode.');
    }
}


// touch() 函数php文档解释
/**
 * Sets access and modification time of file
 * @link http://php.net/manual/en/function.touch.php
 * @param string $filename <p>
 * The name of the file being touched.
 * </p>
 * @param int $time [optional] <p>
 * The touch time. If time is not supplied, 
 * the current system time is used.
 * </p>
 * @param int $atime [optional] <p>
 * If present, the access time of the given filename is set to 
 * the value of atime. Otherwise, it is set to
 * time.
 * </p>
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function touch ($filename, $time = null, $atime = null) {}

    3.2 UpCommand 实现

class UpCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'up';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Bring the application out of maintenance mode';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // 关键:删除laravel存储目录/fromework 下面的 down 文件
        @unlink($this->laravel->storagePath().'/framework/down');

        $this->info('Application is now live.');
    }
}


// @unlink() php文档解释
/**
 * Deletes a file
 * @link http://php.net/manual/en/function.unlink.php
 * @param string $filename <p>
 * Path to the file.
 * </p>
 * @param resource $context [optional] &note.context-support;
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function unlink ($filename, $context = null) {}

 

2、如何工做的

    1. 固然是使用中间件了

// Http/Kernel.php 文件里面
class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        
        // 就是这个东西了
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];
}

    2. 继续看中间件的实现

class CheckForMaintenanceMode
{
    /**
     * The application implementation.
     *
     * @var \Illuminate\Contracts\Foundation\Application
     */
    protected $app;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return void
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     *
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
     */
    public function handle($request, Closure $next)
    {
       
        // 当这个条件成立时,直接抛出 HttpException(503) 异常。
        // 默认状况下,该请求会直接显示 resources/views/errors/503.blade.php 页面
        if ($this->app->isDownForMaintenance()) {
            throw new HttpException(503);
        }

        return $next($request);
    }
}



// 再看 isDownForMaintenance() 函数

/**
 * Determine if the application is currently down for maintenance.
 *
 * @return bool
 */
public function isDownForMaintenance()
{
    // 重点:判断一下 laravel的storagePath/framework 下面是否存在 down 文件
    return file_exists($this->storagePath().'/framework/down');
}

总结:

其实呢,这些只是一个抛砖引玉的过程。只是拿框架的一个小东西来扯扯而已。仍是那句话:路漫漫其修远兮。加油吧,少年~bash

1.  php artisan down => 在storagePath/framework 下面建立 down文件; php artisan up => 删除 down 建立 down文件app

2.  laravel 默认中间件,检查storagePath/framework 下面是否存在down文件,若存在则抛出503异常框架

相关文章
相关标签/搜索