2017-12-30 07:34:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Console\Command;
|
2017-12-30 07:34:09 +08:00
|
|
|
use App\Services\DatabaseService;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class YamlImport
|
|
|
|
* @package App\Console\Commands
|
|
|
|
*/
|
|
|
|
class YamlImport extends Command
|
2017-12-30 07:34:09 +08:00
|
|
|
{
|
2018-03-21 05:10:52 +08:00
|
|
|
protected $signature = 'phpvms:yaml-import {files*}';
|
2017-12-30 07:34:09 +08:00
|
|
|
protected $description = 'Developer commands';
|
|
|
|
protected $dbSvc;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* YamlImport constructor.
|
|
|
|
* @param DatabaseService $dbSvc
|
|
|
|
*/
|
2017-12-30 07:34:09 +08:00
|
|
|
public function __construct(DatabaseService $dbSvc)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->dbSvc = $dbSvc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run dev related commands
|
2018-03-20 09:50:40 +08:00
|
|
|
* @throws \Exception
|
2017-12-30 07:34:09 +08:00
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$files = $this->argument('files');
|
2018-03-20 09:50:40 +08:00
|
|
|
if (empty($files)) {
|
2017-12-30 07:34:09 +08:00
|
|
|
$this->error('No files to import specified!');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ignore_errors = true;
|
2018-03-20 09:50:40 +08:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
$this->error('File '.$file.' doesn\'t exist');
|
2017-12-30 07:34:09 +08:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Importing '.$file);
|
2017-12-30 07:34:09 +08:00
|
|
|
|
|
|
|
$imported = $this->dbSvc->seed_from_yaml_file($file, $ignore_errors);
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($imported as $table => $count) {
|
2017-12-30 07:34:09 +08:00
|
|
|
$this->info('Imported '.$count.' records from "'.$table.'"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|