2022-05-13 14:40:59 +08:00
< ? php
namespace App\Console\Commands ;
use Illuminate\Console\Command ;
use Nexus\Install\Update ;
class NexusUpdate extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
2022-10-28 23:30:24 +08:00
protected $signature = 'nexus:update {--tag=} {--branch=} {--keep_tmp} {--include_composer}' ;
2022-05-13 14:40:59 +08:00
/**
* The console command description .
*
* @ var string
*/
2022-10-28 23:30:24 +08:00
protected $description = 'Update nexusphp after code updated, remember run `composer update` first. Options: --tag=, --branch, --keep_tmp, --include_composer' ;
2022-05-13 14:40:59 +08:00
private $update ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
$this -> update = new Update ();
}
/**
* Execute the console command .
*
* @ return int
*/
public function handle ()
{
define ( 'WITH_LARAVEL' , true );
require ROOT_PATH . 'nexus/Database/helpers.php' ;
2022-06-24 14:55:10 +08:00
$tag = $this -> option ( 'tag' );
2022-10-28 23:30:24 +08:00
$branch = $this -> option ( 'branch' );
2022-06-24 14:55:10 +08:00
$keepTmp = $this -> option ( 'keep_tmp' );
2022-07-03 14:00:07 +08:00
$includeComposer = $this -> option ( 'include_composer' );
$includes = [];
if ( $includeComposer ) {
$includes [] = 'composer' ;
}
2022-08-04 19:50:41 +08:00
2022-05-13 14:40:59 +08:00
//Step 1
$step = $this -> update -> currentStep ();
2022-05-13 15:56:09 +08:00
$log = sprintf ( 'Step %s, %s...' , $step , $this -> update -> getStepName ( $step ));
2022-05-13 14:40:59 +08:00
$this -> doLog ( $log );
$requirements = $this -> update -> listRequirementTableRows ();
$fails = $requirements [ 'fails' ];
if ( ! empty ( $fails )) {
foreach ( $fails as $value ) {
$this -> doLog ( " Error: " . nexus_json_encode ( $value ), 'error' );
}
return 0 ;
}
$this -> update -> gotoStep ( ++ $step );
2022-08-04 19:50:41 +08:00
//Download
if ( $tag !== null ) {
if ( $tag === 'dev' ) {
2022-10-28 23:30:24 +08:00
if ( $branch ) {
$url = " https://github.com/xiaomlove/nexusphp/archive/refs/heads/ { $branch } .zip " ;
} else {
$url = " https://github.com/xiaomlove/nexusphp/archive/refs/heads/php8.zip " ;
}
2022-08-04 19:50:41 +08:00
} else {
2022-08-17 17:56:44 +08:00
if ( ! str_starts_with ( $tag , 'v' )) {
$tag = " v $tag " ;
}
$url = " https://api.github.com/repos/xiaomlove/nexusphp/tarball/ $tag " ;
2022-08-04 19:50:41 +08:00
}
$this -> doLog ( " Specific tag: ' $tag ', download from ' $url ' and extra code, includes: " . implode ( ', ' , $includes ));
$tmpPath = $this -> update -> downAndExtractCode ( $url , $includes );
2022-11-26 03:25:30 +08:00
if ( ! $keepTmp ) {
$this -> doLog ( " Delete tmp files in: $tmpPath " );
$this -> update -> executeCommand ( " rm -rf " . rtrim ( $tmpPath , '/' ));
} else {
$this -> doLog ( " Keep tmp files in: $tmpPath " );
}
$this -> doLog ( " Code update successfully, run this command without --tag option to run the upgrade please! " , 'warn' );
return 0 ;
2022-08-04 19:50:41 +08:00
}
2022-05-13 14:40:59 +08:00
//Step 2
2022-05-13 15:56:09 +08:00
$log = sprintf ( 'Step %s, %s, cli skip...' , $step , $this -> update -> getStepName ( $step ));
2022-05-13 14:40:59 +08:00
$this -> doLog ( $log );
$this -> update -> gotoStep ( ++ $step );
//Step 3
2022-05-13 15:56:09 +08:00
$log = sprintf ( 'Step %s, %s, cli skip...' , $step , $this -> update -> getStepName ( $step ));
2022-05-13 14:40:59 +08:00
$this -> doLog ( $log );
$this -> update -> gotoStep ( ++ $step );
//Step 4
2022-05-13 15:56:09 +08:00
$log = sprintf ( 'Step %s, %s...' , $step , $this -> update -> getStepName ( $step ));
2022-05-13 14:40:59 +08:00
$this -> doLog ( $log );
$settingTableRows = $this -> update -> listSettingTableRows ();
2025-05-04 21:07:32 +07:00
// $settings = $settingTableRows['settings'];
2022-05-13 14:40:59 +08:00
$symbolicLinks = $settingTableRows [ 'symbolic_links' ];
$fails = $settingTableRows [ 'fails' ];
$mysqlInfo = $this -> update -> getMysqlVersionInfo ();
2022-07-18 15:13:03 +08:00
$redisInfo = $this -> update -> getRedisVersionInfo ();
2022-05-13 14:40:59 +08:00
if ( ! empty ( $fails )) {
foreach ( $fails as $value ) {
$this -> doLog ( " Error: " . nexus_json_encode ( $value ), 'error' );
}
return 0 ;
}
if ( ! $mysqlInfo [ 'match' ]) {
2025-05-11 21:21:29 +07:00
$this -> doLog ( " Error: MySQL version: { $mysqlInfo [ 'version' ] } is too low, please use the newest version of { $mysqlInfo [ 'minVersion' ] } or above. " , 'error' );
2022-05-13 14:40:59 +08:00
return 0 ;
}
2022-07-18 15:13:03 +08:00
if ( ! $redisInfo [ 'match' ]) {
2025-05-11 21:21:29 +07:00
$this -> doLog ( " Error: Redis version: { $mysqlInfo [ 'version' ] } is too low, please use { $mysqlInfo [ 'minVersion' ] } or above. " , 'error' );
2022-07-18 15:13:03 +08:00
return 0 ;
}
2024-12-30 01:35:05 +08:00
if ( $includeComposer ) {
$this -> doLog ( " going to update .env file ... " );
$this -> update -> updateEnvFile ();
$this -> doLog ( " update .env file done! " );
}
2022-05-13 14:40:59 +08:00
$this -> doLog ( " going to createSymbolicLinks... " );
$this -> update -> createSymbolicLinks ( $symbolicLinks );
$this -> doLog ( " createSymbolicLinks done! " );
2025-05-04 21:07:32 +07:00
// $this->doLog("going to saveSettings...");
// $this->update->saveSettings($settings);
// $this->doLog("saveSettings done!");
2022-05-13 14:40:59 +08:00
$this -> doLog ( " going to runExtraQueries... " );
$this -> update -> runExtraQueries ();
$this -> doLog ( " runExtraQueries done! " );
$this -> doLog ( " going to runMigrate... " );
$this -> update -> runMigrate ();
$this -> doLog ( " runMigrate done! " );
$this -> doLog ( " going to runExtraMigrate... " );
$this -> update -> runExtraMigrate ();
$this -> doLog ( " runExtraMigrate done! " );
$this -> doLog ( " All done! " );
return 0 ;
}
private function doLog ( $log , string $level = 'info' )
{
$this -> update -> doLog ( $log );
$this -> { $level }( sprintf (
'[%s] [%s] %s' ,
date ( 'Y-m-d H:i:s' ), $this -> update -> currentStep (), $log
));
}
}