phpvms/app/Console/Commands/YamlImport.php

60 lines
1.3 KiB
PHP
Raw Normal View History

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