Copy & Paste Engineer

【Laravel】コマンドの実行制限をさせる方法

【Laravel】コマンドの実行制限をさせる方法

Laravel側で用意されているコマンドをたとえば本番環境で実行させたくない時などに使えるコマンドの実行制限をさせる方法です。

特定のコマンドの実行制限をする

<?php

namespace App\Console;

use Illuminate\Support\Str;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    // 実行させたくないコマンド
    const EXCLUDE_COMMAND = [
        'migrate:fresh',
        'migrate:reset',
    ];

    public function handle($input, $output = null)
    {
        // Laravel Load
        $this->bootstrap();

        if (
            // 実行させたくない環境
            env('APP_ENV') == 'production' &&
            // 入力されたコマンドの除外コマンド判定
            Str::contains($input->getFirstArgument(), self::EXCLUDE_COMMAND)
        ) {
            // 除外コマンドであれば処理を停止
            echo '################# Warning #################' . PHP_EOL;
            echo $input->getFirstArgument() . ' is this ' . env('APP_ENV') . ' can\'t run ' . PHP_EOL;
            echo '###########################################' . PHP_EOL;
            return 1;
        }

        // 処理を実行
        return parent::handle($input, $output);
    }
}

コマンドを実行すると以下のようなエラーが出ます。

$ php artisan migrate:fresh
################# Warning #################
migrate:fresh is this production can't run
###########################################

参考