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

时间:2022-07-22
本文章向大家介绍Laravel框架学习 -- php artisan down/up,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

由于某种原因,公司整体框架由python的flask框架,转换为php的laravel。在断断续续几个月的时间里,边继续写着flask框架,边学着laravel。说下自己现在的状态吧。前段时间差不多都在个1-2点睡觉,大概四月份有段时间竟然到了3-4点才睡的地步。

路漫漫其修远兮,总感觉时间不够用的。大概是自己之前浪费的时间太多了,是时候还上了。


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

# 输入 php artisan 即可看到全部可用命令

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

整体流程大体(因为详细的我也不是很清楚╮(╯_╰)╭)说下吧。

一、命令的实现

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

// config/app.php 中。
'providers' => [
    // 这个便是 laravel自带的 artisan 命令提供者
    IlluminateFoundationProvidersArtisanServiceProvider::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) {}

二、如何工作的

1. 当然是使用中间件了

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

2. 继续看中间件的实现

class CheckForMaintenanceMode
{
    /**
     * The application implementation.
     *
     * @var IlluminateContractsFoundationApplication
     */
    protected $app;

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

    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     *
     * @throws SymfonyComponentHttpKernelExceptionHttpException
     */
    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');
}

总结:

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

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

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