-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathMigrationCreator.php
115 lines (92 loc) · 2.82 KB
/
MigrationCreator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Encore\Admin\Helpers\Scaffold;
use Illuminate\Database\Migrations\MigrationCreator as BaseMigrationCreator;
class MigrationCreator extends BaseMigrationCreator
{
/**
* @var string
*/
protected $bluePrint = '';
/**
* Create a new model.
*
* @param string $name
* @param string $path
* @param null $table
* @param bool|true $create
*
* @return string
*/
public function create($name, $path, $table = null, $create = true)
{
$this->ensureMigrationDoesntAlreadyExist($name);
$path = $this->getPath($name, $path);
$stub = $this->files->get(__DIR__.'/stubs/create.stub');
$this->files->put($path, $this->populateStub($name, $stub, $table));
$this->firePostCreateHooks($table);
return $path;
}
/**
* Populate stub.
*
* @param string $name
* @param string $stub
* @param string $table
*
* @return mixed
*/
protected function populateStub($name, $stub, $table)
{
return str_replace(
['DummyClass', 'DummyTable', 'DummyStructure'],
[$this->getClassName($name), $table, $this->bluePrint],
$stub
);
}
/**
* Build the table blueprint.
*
* @param array $fields
* @param string $keyName
* @param bool|true $useTimestamps
* @param bool|false $softDeletes
*
* @throws \Exception
*
* @return $this
*/
public function buildBluePrint($fields = [], $keyName = 'id', $useTimestamps = true, $softDeletes = false)
{
$fields = array_filter($fields, function ($field) {
return isset($field['name']) && !empty($field['name']);
});
if (empty($fields)) {
throw new \Exception('Table fields can\'t be empty');
}
$rows[] = "\$table->increments('$keyName');\n";
foreach ($fields as $field) {
$column = "\$table->{$field['type']}('{$field['name']}')";
if ($field['key']) {
$column .= "->{$field['key']}()";
}
if (isset($field['default']) && $field['default']) {
$column .= "->default('{$field['default']}')";
}
if (isset($field['comment']) && $field['comment']) {
$column .= "->comment('{$field['comment']}')";
}
if (array_get($field, 'nullable') == 'on') {
$column .= '->nullable()';
}
$rows[] = $column.";\n";
}
if ($useTimestamps) {
$rows[] = "\$table->timestamps();\n";
}
if ($softDeletes) {
$rows[] = "\$table->softDeletes();\n";
}
$this->bluePrint = trim(implode(str_repeat(' ', 12), $rows), "\n");
return $this;
}
}