-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMigrationGenerator.php
150 lines (132 loc) · 3.61 KB
/
MigrationGenerator.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Pingpong\Generators;
use Pingpong\Generators\FormDumpers\TableDumper;
use Pingpong\Generators\Migrations\NameParser;
use Pingpong\Generators\Migrations\SchemaParser;
class MigrationGenerator extends Generator
{
/**
* Get stub name.
*
* @var string
*/
protected $stub = 'migration/plain';
/**
* Get base path of destination file.
*
* @return string
*/
public function getBasePath()
{
return base_path().'/database/migrations/';
}
/**
* Get destination path for generated file.
*
* @return string
*/
public function getPath()
{
return $this->getBasePath().$this->getFileName().'.php';
}
/**
* Get root namespace.
*
* @return string
*/
public function getRootNamespace()
{
return '';
}
/**
* Get migration name.
*
* @return string
*/
public function getMigrationName()
{
if ($this->existing) {
return 'create_'.str_plural($this->name).'_table';
}
return strtolower($this->name);
}
/**
* Get file name.
*
* @return string
*/
public function getFileName()
{
return date('Y_m_d_His_').$this->getMigrationName();
}
/**
* Get schema parser.
*
* @return SchemaParser
*/
public function getSchemaParser()
{
return new SchemaParser($this->fields);
}
/**
* Get name parser.
*
* @return NameParser
*/
public function getNameParser()
{
return new NameParser($this->name);
}
/**
* Get stub templates.
*
* @return string
*/
public function getStub()
{
$parser = $this->getNameParser();
if ($this->existing) {
$this->name = 'create_'.str_plural($this->name).'_table';
$parser = $this->getNameParser();
$fields = (new TableDumper($parser->getTable()))->toSchema();
$stub = Stub::create('/migration/create.stub', [
'class' => $this->getClass(),
'table' => $parser->getTable(),
'fields' => (new SchemaParser($fields))->render(),
]);
}
elseif ($parser->isCreate()) {
$stub = Stub::create('/migration/create.stub', [
'class' => $this->getClass(),
'table' => $parser->getTable(),
'fields' => $this->getSchemaParser()->render(),
]);
} elseif ($parser->isAdd()) {
$stub = Stub::create('/migration/add.stub', [
'class' => $this->getClass(),
'table' => $parser->getTable(),
'fields_up' => $this->getSchemaParser()->up(),
'fields_down' => $this->getSchemaParser()->down(),
]);
} elseif ($parser->isDelete()) {
$stub = Stub::create('/migration/delete.stub', [
'class' => $this->getClass(),
'table' => $parser->getTable(),
'fields_down' => $this->getSchemaParser()->up(),
'fields_up' => $this->getSchemaParser()->down(),
]);
} elseif ($parser->isDrop()) {
$stub = Stub::create('/migration/drop.stub', [
'class' => $this->getClass(),
'table' => $parser->getTable(),
'fields' => $this->getSchemaParser()->render(),
]);
} else {
$stub = false;
}
if ($stub) {
return $stub->render();
}
return parent::getStub();
}
}