2017-12-30 00:23:42 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Console\BaseCommand;
|
|
|
|
use App\Models\Acars;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Airline;
|
2017-12-30 00:23:42 +08:00
|
|
|
use App\Models\Pirep;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use DB;
|
|
|
|
use PDO;
|
2018-02-22 05:02:24 +08:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2017-12-30 00:23:42 +08:00
|
|
|
|
|
|
|
class DevCommands extends BaseCommand
|
|
|
|
{
|
2018-02-22 05:02:24 +08:00
|
|
|
protected $signature = 'phpvms {cmd} {param?}';
|
2017-12-30 00:23:42 +08:00
|
|
|
protected $description = 'Developer commands';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run dev related commands
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$command = trim($this->argument('cmd'));
|
|
|
|
|
|
|
|
if (!$command) {
|
|
|
|
$this->error('No command specified!');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$commands = [
|
|
|
|
'clear-acars' => 'clearAcars',
|
2018-01-01 02:09:23 +08:00
|
|
|
'clear-users' => 'clearUsers',
|
2017-12-30 00:23:42 +08:00
|
|
|
'compile-assets' => 'compileAssets',
|
2018-01-10 13:05:52 +08:00
|
|
|
'db-attrs' => 'dbAttrs',
|
2018-02-22 05:02:24 +08:00
|
|
|
'xml-to-yaml' => 'xmlToYaml',
|
2017-12-30 00:23:42 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if(!array_key_exists($command, $commands)) {
|
|
|
|
$this->error('Command not found!');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->{$commands[$command]}();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all the data from the ACARS and PIREP tables
|
|
|
|
*/
|
|
|
|
protected function clearAcars()
|
|
|
|
{
|
|
|
|
if(config('database.default') === 'mysql') {
|
|
|
|
DB::statement('SET foreign_key_checks=0');
|
|
|
|
}
|
|
|
|
|
|
|
|
Acars::truncate();
|
|
|
|
Pirep::truncate();
|
|
|
|
|
|
|
|
if (config('database.default') === 'mysql') {
|
|
|
|
DB::statement('SET foreign_key_checks=1');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('ACARS and PIREPs cleared!');
|
|
|
|
}
|
|
|
|
|
2018-01-01 02:09:23 +08:00
|
|
|
/**
|
|
|
|
* Delete all the data from the ACARS and PIREP tables
|
|
|
|
*/
|
|
|
|
protected function clearUsers()
|
|
|
|
{
|
|
|
|
if (config('database.default') === 'mysql') {
|
|
|
|
DB::statement('SET foreign_key_checks=0');
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::statement('TRUNCATE `role_user`');
|
|
|
|
Airline::truncate();
|
|
|
|
User::truncate();
|
|
|
|
|
|
|
|
if (config('database.default') === 'mysql') {
|
|
|
|
DB::statement('SET foreign_key_checks=1');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Users cleared!');
|
|
|
|
}
|
|
|
|
|
2017-12-30 00:23:42 +08:00
|
|
|
/**
|
|
|
|
* Compile all the CSS/JS assets into their respective files
|
|
|
|
* Calling the webpack compiler
|
|
|
|
*/
|
|
|
|
protected function compileAssets()
|
|
|
|
{
|
|
|
|
$this->runCommand('npm update');
|
|
|
|
$this->runCommand('npm run dev');
|
|
|
|
}
|
2018-01-10 13:05:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output DB prepares versions
|
|
|
|
*/
|
|
|
|
protected function dbAttrs()
|
|
|
|
{
|
|
|
|
$pdo = DB::connection()->getPdo();
|
|
|
|
$emulate_prepares_below_version = '5.1.17';
|
|
|
|
$server_version = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
$emulate_prepares = version_compare($server_version, $emulate_prepares_below_version, '<');
|
|
|
|
|
|
|
|
$this->info('Server Version: '. $server_version);
|
|
|
|
$this->info('Emulate Prepares: '.$emulate_prepares);
|
|
|
|
|
|
|
|
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $emulate_prepares);
|
|
|
|
}
|
2018-02-22 05:02:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the sequelpro xml export to yaml
|
|
|
|
*/
|
|
|
|
protected function xmlToYaml()
|
|
|
|
{
|
|
|
|
$file = $this->argument('param');
|
|
|
|
$this->info('Reading '. $file);
|
|
|
|
|
|
|
|
$xml_str = file_get_contents($file);
|
|
|
|
$xml = new \SimpleXMLElement($xml_str);
|
|
|
|
|
|
|
|
$yaml = [];
|
|
|
|
$table_name = (string) $xml->database->table_data['name'];
|
|
|
|
$this->info('Writing table "'.$table_name.'"');
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$yaml[$table_name] = [];
|
|
|
|
|
|
|
|
foreach ($xml->database->table_data->row as $row) {
|
|
|
|
$yaml_row = [];
|
|
|
|
foreach($row->field as $field) {
|
|
|
|
$fname = (string) $field['name'];
|
|
|
|
$fvalue = (string) $field;
|
|
|
|
|
|
|
|
$yaml_row[$fname] = $fvalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($yaml_row['pirep_id'] === 'pirepid_2')
|
|
|
|
$yaml[$table_name][] = $yaml_row;
|
|
|
|
++$count;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Exporting '.$count.' rows');
|
|
|
|
|
|
|
|
$file_name = $table_name.'.yml';
|
|
|
|
file_put_contents(storage_path($file_name), Yaml::dump($yaml, 4, 2));
|
|
|
|
$this->info('Writing yaml to storage: '. $file_name);
|
|
|
|
}
|
2017-12-30 00:23:42 +08:00
|
|
|
}
|