Artisan 小王子 作品分享
作为Artisan 小王子
我要分享下
项目里面的Artisan命令 拿出来2个命令 给大家瞅瞅 。
主命令内部执行其他我做的小命令
小命令代码不给你们看了,涉及业务,给你们看最外面的主命令框架
<?php
namespace App\Console\Commands;
use App\Models\Option;
use Cache;
use DB;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
class MainCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'main {--key= : 步骤}';
/**
* The console command description.
*
* @var string
*/
protected $description = '主命令';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//TODO 关于正式环境的所有操作都必须保证,只允许执行一次,产生数据后禁止执行
// $this->productionCheckHint();
$exit = 'no';
do {
$key = $this->option('key');
if ($key) {
//其他不开放快速执行
if ($key == 3) {
$option = '网站全站加速';
}
if ($key == 2) {
$option = '一键部署';
}
} else {
$option = $this->choice('请选择功能', [
'新建用户/修改用户',
'业务通知',
'一键部署',
'网站全站加速',
'基金股票关系表生成',
]);
}
$this->line('正在为您链接 ' . $option);
switch ($option) {
case '新建用户/修改用户':
$name = $this->ask('请输入新建/修改用户的名字');
$email = $this->ask('请输入新建/修改用户的邮箱');
$password = $this->ask('请输入新建/修改用户的密码');
Artisan::call("user:generate $name $email $password");
$message = '为您强制新建/更新用户成功!';
break;
case '业务通知':
$text = $this->ask('请输入要通知的文本');
$robotKey = $this->ask('请输入需要通知的机器人Key,不输入则使用系统env默认企业微信机器人');
if ($robotKey) {
Artisan::call("business:notify " . '--text='.$text . "--key=$robotKey ");
}
Artisan::call("business:notify " . '--text='.$text);
$message = '通知发送完成';
break;
case '一键部署':
if (App::environment('production')) {
$this->output->error('检测到您处于生产模式,无法为您进行操作,请确认您的环境APP_ENV不为production后在执行');
return ;
}
try {
if (Option::get('main_command_is_used')) {
$this->output->error('本命令只允许使用一次,确保不被恶意攻击,命令内部终止');
return ;
}
} catch (\Exception $exception) {
}
$this->output->success('部署前的检查已经完成');
if($this->confirm('开始部署?')){
Artisan::call("key:generate");
$this->output->title('');
$this->output->success('网站密钥填充成功');
Artisan::call("migrate:refresh");
$this->output->title('');
$this->output->success('数据库重装并迁移完成');
Artisan::call("passport:install");
$this->output->title('');
$this->output->success('passport密钥已生成');
Artisan::call("db:seed");
$this->output->title('');
$this->output->success('数据填充完毕');
Artisan::call("storage:link");
$this->output->title('');
$this->output->success('storage软连接生成');
Artisan::call('optimize:clear');
$this->output->title('');
$this->output->success('整站缓存清理完成');
$this->output->title('');
$this->output->title('正在为您生成股票关系表,此项可能较久,请耐心等待');
Artisan::call('fund-shares:generate');
$this->output->success('股票关系表已经生成最新数据');
Option::set('main_command_is_used', true);
$this->output->success('命令正在完成,完成后,该命令将永久不可用');
$this->output->title('首次部署完毕后,请运行一次网站全站加速');
if ($this->option('key')==2) {
$this->output->info('完成,为您退出命令!');
return ;
}
$this->output->info('完成,您可以退出命令!');
}
break;
case '网站全站加速':
$this->output->progressStart(100);
$client = new Client();
$this->output->title('加速马上开始,加速过程中可能会给网站造成临时的缓慢');
$this->output->title('正在做加速前的检查........');
$url = config('app.url');
if ($url) {
try {
$client->get($url);
$this->output->success('网站URL配置正确及连接正常');
} catch (\Exception $e) {
$this->output->error('网站URL不可连接!加速无法进行,请确保您的网站连接是否正常');
return ;
}
} else {
$this->output->error('网站URL缺失,加速无法进行,请确保您的env文件项目url正常!');
return ;
}
$this->output->title('正在检测网站redis服务连接性');
if (config('cache.default') !== 'redis' || config('queue.default') !== 'redis') {
$this->output->error('检测到网站缓存或队列非redis模式!加速无法进行,请修改env配置文件!');
return ;
} else {
if (Cache::put('test', 'test', 3)) {
$this->output->success('网站已启用redis服务器,redis服务器连接正常');
} else {
$this->output->error('redis服务器连接失败,请检查redis服务器状态');
}
}
$this->output->title('加速选项-1-正在加速 -该项加速失败率较高,时间较久请耐心等待');
$this->output->progressAdvance(25);
$i = 0;
$tryMaxNum = config('url-speed.try_times');
while ($i < $tryMaxNum) {
try {
Artisan::call('url-speed:generate 1 --conditional');
break;
} catch (\Exception $e) {
$i++;
$this->output->error('很遗憾由于您的服务器配置问题,本次加速未能完成,正在为您重试第'.$i.'次!');
}
}
if ($i === $tryMaxNum) {
$this->output->error('加速选项- 1 -加速失败-跳过【您可以稍后再次运行本命令,加速过的部分将会为您跳过!并且建议您将速度配置调低以确保稳定性】');
}
$this->output->title('');
$this->output->success('加速选项-1-加速完毕');
$this->output->title('加速选项-2-正在加速');
$this->output->progressAdvance(25);
Artisan::call("url-speed:generate 1 --bullfund");
$this->output->title('');
$this->output->success('加速选项-2-加速完毕');
$this->output->title('加速选项-3-正在加速');
$this->output->progressAdvance(25);
Artisan::call("url-speed:generate 1 --real");
$this->output->title('');
$this->output->success('加速选项-3-加速完毕');
$this->output->title('加速选项-4-正在加速');
$this->output->progressAdvance(25);
Artisan::call("url-speed:generate 1 --other");
$this->output->title('');
$this->output->success('加速选项-4-加速完毕');
$this->output->progressFinish();
if ($this->option('key')==3) {
$this->output->info('完成,为您退出命令!');
return ;
}
$this->output->info('完成,您可以退出命令!');
break;
default:
$message = '请选择';
}
$this->output->title($message ?? '');
$exit = $this->confirm('需要退出本系统吗?');
} while ($exit != 'yes');
}
}
如果加速的条件多了,最好可以优化一下下,优化代码如下:
<?php
namespace App\Console\Commands;
use App\Models\Option;
use Cache;
use DB;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
class MainCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'main {--key= : 步骤}';
/**
* The console command description.
*
* @var string
*/
protected $description = '主命令';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//TODO 关于正式环境的所有操作都必须保证,只允许执行一次,产生数据后禁止执行
// $this->productionCheckHint();
$exit = 'no';
do {
$key = $this->option('key');
if ($key) {
//其他不开放快速执行
if ($key == 3) {
$option = '网站全站加速';
}
if ($key == 2) {
$option = '一键部署';
}
} else {
$option = $this->choice('请选择功能', [
'新建用户/修改用户',
'业务通知',
'一键部署',
'网站全站加速',
'基金股票关系表生成',
]);
}
$this->line('正在为您链接 ' . $option);
switch ($option) {
case '网站全站加速':
$this->output->progressStart(100);
$this->output->title('加速马上开始,加速过程中可能会给网站造成临时缓慢,请耐心等待');
$this->output->title('正在做加速前的检查........');
if ($this->healthCheckForWebSite()!==0) {
return ;
}
$urlSpeedTypes=['conditional','bullfund','real','other'];
foreach ($urlSpeedTypes as $key => $urlSpeedType) {
$this->output->title('加速选项-'.$key.'-正在加速');
$this->output->progressAdvance(25);
$this->tryTimesSpeed($urlSpeedType, $key);
}
$this->output->progressFinish();
if ($this->option('key')==3) {
$this->output->info('完成,为您退出命令!');
return ;
}
$this->output->info('完成,您可以退出命令!');
break;
default:
$message = '请选择';
}
$this->output->title($message ?? '');
$exit = $this->confirm('需要退出本系统吗?');
} while ($exit != 'yes');
}
public function tryTimesSpeed($type, $times)
{
$i = 0;
$tryMaxNum = config('url-speed.try_times');
while ($i < $tryMaxNum) {
try {
Artisan::call('url-speed:generate 1 --'.$type);
break;
} catch (\Exception $e) {
$i++;
$this->output->error('很遗憾由于您的服务器配置问题,本次加速未能完成,正在为您重试第'.$i.'次!');
}
}
if ($i === $tryMaxNum) {
$this->output->error('加速选项- '.$times.' -加速失败-跳过【您可以稍后再次运行本命令,加速过的部分将会为您跳过!并且建议您将速度配置调低以确保稳定性】');
}
$this->output->title('');
$this->output->success('加速选项-'.$times.'-加速完毕');
}
public function healthCheckForWebSite(): int
{
$client = new Client();
$url = config('app.url');
$result=0;
$this->output->title('正在检测网站URL相关内容');
if ($url) {
try {
$client->get($url);
$this->output->success('网站URL配置正确及连接正常');
} catch (\Exception $e) {
$this->output->error('网站URL不可连接!加速无法进行,请确保您的网站连接是否正常');
$result+=1;
}
} else {
$this->output->error('网站URL缺失,加速无法进行,请确保您的env文件项目url正常!');
$result+=1;
}
$this->output->title('正在检测网站redis服务连接性');
if (config('cache.default') !== 'redis' || config('queue.default') !== 'redis') {
$this->output->error('检测到网站缓存或队列非redis模式!加速无法进行,请修改env配置文件!');
$result+=1;
} else {
try {
if (Cache::put('test', 'test', 1)) {
$this->output->success('网站已启用redis服务器,redis服务器连接正常');
}
} catch (\Exception $e) {
$this->output->error('redis服务器连接失败,请检查redis服务器状态');
$result+=1;
}
}
return $result;
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 3年前 自动加精
推荐文章: