1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Lib\Slime\Console\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\Lib\Helpers\TextFormatter; |
8
|
|
|
use App\Lib\Slime\Console\GeneratorHelperCommand; |
9
|
|
|
|
10
|
|
|
class CreateMigrationCommand extends GeneratorHelperCommand |
11
|
|
|
{ |
12
|
|
|
protected $incrementalFileName = null; |
13
|
|
|
protected $prefix = 'M'; //stands for migration |
14
|
|
|
protected $filePath = 'database/migrations/'; |
15
|
|
|
|
16
|
|
|
protected function getFileName() |
17
|
|
|
{ |
18
|
|
|
return $this->getIncrementalFileName(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
protected function getHead() |
23
|
|
|
{ |
24
|
|
|
$fileHead = parent::getHead(); |
25
|
|
|
$fileHead .= PHP_EOL |
26
|
|
|
. PHP_EOL |
27
|
|
|
. 'use App\Lib\Slime\Interfaces\DatabaseHelpers\DbHelperInterface;' |
28
|
|
|
. PHP_EOL |
29
|
|
|
. 'use Illuminate\Database\Capsule\Manager as Capsule; ' |
30
|
|
|
. PHP_EOL |
31
|
|
|
. 'use \Illuminate\Database\Schema\Blueprint as Blueprint;' |
32
|
|
|
. PHP_EOL; |
33
|
|
|
return $fileHead; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function getFilePath() |
37
|
|
|
{ |
38
|
|
|
return $this->filePath; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
protected function getStub() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
return PHP_EOL . 'class ' . |
44
|
|
|
$this->getIncrementalFileName() . |
45
|
|
|
' implements DbHelperInterface {' |
46
|
|
|
. PHP_EOL |
47
|
|
|
. ' |
48
|
|
|
public function run() |
49
|
|
|
{ |
50
|
|
|
$tableName = \'table_name\'; |
51
|
|
|
Capsule::schema()->dropIfExists($tableName); |
52
|
|
|
Capsule::schema()->create($tableName, function (Blueprint $table) { |
53
|
|
|
$table->increments(\'id\'); |
54
|
|
|
$table->string(\'name\'); |
55
|
|
|
$table->timestamps(); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
' |
59
|
|
|
. PHP_EOL |
60
|
|
|
. '}'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function getIncrementalFileName() |
64
|
|
|
{ |
65
|
|
|
if (empty($this->incrementalFileName)) { |
66
|
|
|
$this->incrementalFileName = $this->prefix . time() . TextFormatter::snakeToCamelCase($this->getArg(0)); |
67
|
|
|
} |
68
|
|
|
return $this->incrementalFileName; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.