|
1
|
|
|
<?php namespace Nwidart\DbExporter\Commands; |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
use Nwidart\DbExporter\DbExporter; |
|
5
|
|
|
use Nwidart\DbExporter\DbExportHandler; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
7
|
|
|
use Config, Str; |
|
8
|
|
|
|
|
9
|
|
|
class SeedGeneratorCommand extends GeneratorCommand |
|
10
|
|
|
{ |
|
11
|
|
|
protected $name = 'dbe:seeds'; |
|
12
|
|
|
|
|
13
|
|
|
protected $description = 'Export your database table data to a seed class.'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Nwidart\DbExporter\DbExportHandler |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $handler; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(DbExportHandler $handler) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct(); |
|
23
|
|
|
|
|
24
|
|
|
$this->handler = $handler; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function fire() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->comment("Preparing the seeder class for database {$this->getDatabaseName()}"); |
|
30
|
|
|
|
|
31
|
|
|
// Grab the options |
|
32
|
|
|
$ignore = $this->option('ignore'); |
|
33
|
|
|
|
|
34
|
|
|
if (empty($ignore)) { |
|
35
|
|
|
$this->handler->seed(); |
|
36
|
|
|
} else { |
|
37
|
|
|
$tables = explode(',', str_replace(' ', '', $ignore)); |
|
38
|
|
|
$this->handler->ignore($tables)->seed(); |
|
39
|
|
|
foreach (DbExporter::$ignore as $table) { |
|
40
|
|
|
$this->comment("Ignoring the {$table} table"); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Symfony style block messages |
|
45
|
|
|
$formatter = $this->getHelperSet()->get('formatter'); |
|
46
|
|
|
$filename = $this->getFilename(); |
|
47
|
|
|
|
|
48
|
|
|
$errorMessages = array('Success!', "Database seed class generated in: {$filename}"); |
|
49
|
|
|
|
|
50
|
|
|
$formattedBlock = $formatter->formatBlock($errorMessages, 'info', true); |
|
51
|
|
|
$this->line($formattedBlock); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function getFilename() |
|
55
|
|
|
{ |
|
56
|
|
|
$filename = Str::camel($this->getDatabaseName()) . "TableSeeder"; |
|
57
|
|
|
return config('db-exporter.export_path.seeds')."{$filename}.php"; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getOptions() |
|
61
|
|
|
{ |
|
62
|
|
|
return array( |
|
63
|
|
|
array('ignore', 'ign', InputOption::VALUE_REQUIRED, 'Ignore tables to export, separated by a comma', null) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |