· 7 years ago · Feb 10, 2019, 09:20 PM
1<?php
2
3namespace App\Console\Commands;
4
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\DB;
7
8class MigrationNew extends Command
9{
10 /**
11 * The name and signature of the console command.
12 *
13 * @var string
14 */
15 protected $signature = 'my:migration
16 {package? : Package name}
17 {migration? : Migration file name}
18 {table? : Database table name}
19 {--a|alter : Edits existing database table}';
20
21 /**
22 * The console command description.
23 *
24 * @var string
25 */
26 protected $description = 'Creates new migration database instance.';
27
28 /**
29 * Create a new command instance.
30 *
31 * @return void
32 */
33 public function __construct()
34 {
35 parent::__construct();
36 }
37
38 /**
39 * Execute the console command.
40 *
41 * @return mixed
42 */
43 public function handle()
44 {
45 // Ask user if he wishes to create new migration
46 if($this->confirm('Do you wish to create new migration?',true))
47 {
48 $package = $this->argument('package'); // Gets package command input : my:migration package_name
49
50 if($package == null) // null is default value if argument is not set
51 {
52 $package = $this->choice('What is package name',$this->getAllPackages());
53 }
54
55 $table = $this->argument('table');
56 $alter = false;
57
58 // if --alter option is present an existing database will be modified.
59 // and auto complete list with database table names will be displayed.
60 if($this->option('alter'))
61 {
62 $table = $this->choice('What is table name',$this->getAllDatabaseTables());
63 $alter = true;
64 }
65
66 $this->create($package,$this->argument('migration'),$table,$alter);
67 }
68 }
69
70 /**
71 * Create migration
72 */
73 public function create(?string $package = null, ?string $migrationName = null, ?string $tableName = null, bool $alter = false)
74 {
75 if($package == null)
76 {
77 $package = $this->ask('Whats is package name');
78 }
79
80 if($migrationName == null)
81 {
82 $migrationName = $this->ask('Whats is migration name');
83 }
84
85 $migrationName = ucfirst($migrationName);
86
87 if($tableName == null)
88 {
89 $tableName = $this->ask('Whats is table name');
90 }
91
92
93 $path = app_path('Packages/' . $package . '/Migrations/');
94 $file = date('Y_m_d_His'). '_' . $package.$migrationName. '.php'; // forgot to add package name...
95 $location = $path . $file;
96 $table = $tableName;
97
98 if(!is_dir($path))
99 {
100 $this->warn('No Package: "'.$package . '" do not exists.');
101 return;
102 }
103
104 if(is_file($location))
105 {
106 $this->warn('Package Model: "'.$file . '" already exists.');
107 return;
108 }
109
110 file_put_contents($location,($alter) ?
111 $this->templateAlter(['{package}','{migration}','{table}'],[$package,$migrationName,$table]) :
112 $this->templateCreate(['{package}','{migration}','{table}'],[$package,$migrationName,$table]));
113
114 $this->info('Migration created successfully.');
115 }
116
117 /**
118 * Retrieves list of all tables in database.
119 */
120 public function getAllDatabaseTables()
121 {
122 return array_map('reset',DB::select('SHOW TABLES'));
123 }
124
125 /**
126 * Retrieves list of all application packages.
127 */
128 public function getAllPackages()
129 {
130 $packagePath = app_path('Packages/');
131 $packages = [];
132
133 $directory = new \DirectoryIterator($packagePath);
134
135 foreach ($directory as $package)
136 {
137 if($package->isDir() and !$package->isDot())
138 {
139 $packages[] = $package->getFilename();
140 }
141 }
142
143 return $packages;
144 }
145
146 /**
147 * Template used to create new migration file.
148 *
149 * @param array $find
150 * @param array $replace
151 * @return string
152 */
153 public function templateCreate(array $find, array $replace): string
154 {
155 return str_replace($find,$replace,'<?php
156
157use Illuminate\Support\Facades\Schema;
158use Illuminate\Database\Schema\Blueprint;
159use Illuminate\Database\Migrations\Migration;
160
161class {package}{migration} extends Migration
162{
163 /**
164 * Run the migrations.
165 *
166 * @return void
167 */
168 public function up()
169 {
170 Schema::create(\'{table}\',function(Blueprint $table)
171 {
172 $table->increments(\'id\');
173
174 $table->timestamps();
175 $table->softDeletes();
176 });
177 }
178
179 /**
180 * Reverse the migrations.
181 *
182 * @return void
183 */
184 public function down()
185 {
186
187 }
188}');
189 }
190 /*
191 * Template used to alter existing database table.
192 */
193 public function templateAlter(array $find, array $replace): string
194 {
195 return str_replace($find,$replace,'<?php
196
197use Illuminate\Support\Facades\Schema;
198use Illuminate\Database\Schema\Blueprint;
199use Illuminate\Database\Migrations\Migration;
200
201class {package}{migration} extends Migration
202{
203 /**
204 * Run the migrations.
205 *
206 * @return void
207 */
208 public function up()
209 {
210 Schema::table(\'{table}\',function(Blueprint $table)
211 {
212
213 });
214 }
215
216 /**
217 * Reverse the migrations.
218 *
219 * @return void
220 */
221 public function down()
222 {
223
224 }
225}');
226 }
227}