fhefh2015
2/22/2019 - 7:19 AM

用Laravel的artisan命令执行php脚本

用Laravel的artisan命令执行php脚本

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;

class RunFile extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'run';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run php file with system env support.';
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = $this->argument('php_file');
        if (is_file($file)) {
            include $file;
        } else {
            $this->error("file '{$file}' not found!");
        }
    }
    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return array(
            array('php_file', InputArgument::REQUIRED, 'The php file to run with Laputa.'),
        );
    }
}

//Laravel 5.7 
//使用:php artisan run [file path]
php artisan run ./app/Utils/Tools.php