1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
|
9
|
|
|
trait DbHelper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string Table name where migrations info is kept |
13
|
|
|
*/ |
14
|
|
|
private $migrationsTable = 'migrations'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string Table name where seeds info is kept |
18
|
|
|
*/ |
19
|
|
|
private $seedsTable = 'seeds'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Return class name by file basename |
23
|
|
|
* @param string $baseName |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
private function getClassName($baseName) |
28
|
|
|
{ |
29
|
|
|
$filenameParts = explode('_', $baseName); |
30
|
|
|
$class = ''; |
31
|
|
|
|
32
|
|
|
array_shift($filenameParts); |
33
|
|
|
|
34
|
|
|
foreach ($filenameParts as $key => $filenamePart) { |
35
|
|
|
$class .= ucfirst($filenamePart); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $class; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $tableName |
43
|
|
|
*/ |
44
|
|
|
private function safeCreateTable($tableName) |
45
|
|
|
{ |
46
|
|
|
if (!Capsule::schema()->hasTable($tableName)) { |
47
|
|
|
Capsule::schema()->create($tableName, function($table) { |
48
|
|
|
$table->string('version'); |
49
|
|
|
$table->timestamp('apply_time')->useCurrent(); |
50
|
|
|
$table->primary('version'); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $name |
57
|
|
|
* @param $table |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
private function isRowExist($name, $table) |
61
|
|
|
{ |
62
|
|
|
$item = Capsule::table($table)->where('version', $name)->first(); |
63
|
|
|
return !is_null($item); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $name |
68
|
|
|
* @param $table |
69
|
|
|
*/ |
70
|
|
|
private function insertRow($name, $table) |
71
|
|
|
{ |
72
|
|
|
Capsule::table($table)->insert([ |
73
|
|
|
'version' => $name, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $name |
79
|
|
|
* @param $table |
80
|
|
|
*/ |
81
|
|
|
private function deleteRow($name, $table) |
82
|
|
|
{ |
83
|
|
|
Capsule::table($table)->where([ |
84
|
|
|
'version' => $name, |
85
|
|
|
])->delete(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Run list of commands in files |
90
|
|
|
* |
91
|
|
|
* @param Finder $files list of files to run |
92
|
|
|
* @param OutputInterface $output |
93
|
|
|
* @param string $tableName |
94
|
|
|
* @param string $method |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
private function runActions(Finder $files, OutputInterface $output, $tableName, $method) |
99
|
|
|
{ |
100
|
|
|
foreach ($files as $file) { |
101
|
|
|
$baseName = $file->getBasename('.php'); |
102
|
|
|
$class = $this->getClassName($baseName); |
103
|
|
|
|
104
|
|
|
if ($this->isRowExist($baseName, $tableName)) { |
105
|
|
|
$output->writeln([sprintf('`%s` - already exists.', $baseName)]); |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
require_once($file); |
110
|
|
|
|
111
|
|
|
$obj = new $class(); |
112
|
|
|
$obj->$method(); |
113
|
|
|
|
114
|
|
|
$this->insertRow($baseName, $tableName); |
115
|
|
|
$output->writeln([sprintf('`%s` - done.', $baseName)]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$output->writeln(['<info>Completed.</info>']); |
119
|
|
|
|
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|