| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Console\Migrations;
|
|
|
4 |
|
| 399 |
lars |
5 |
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
| 148 |
lars |
6 |
use Illuminate\Database\Migrations\MigrationCreator;
|
|
|
7 |
use Illuminate\Support\Composer;
|
|
|
8 |
use Illuminate\Support\Str;
|
|
|
9 |
|
| 399 |
lars |
10 |
class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput
|
| 148 |
lars |
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* The console command signature.
|
|
|
14 |
*
|
|
|
15 |
* @var string
|
|
|
16 |
*/
|
|
|
17 |
protected $signature = 'make:migration {name : The name of the migration}
|
|
|
18 |
{--create= : The table to be created}
|
|
|
19 |
{--table= : The table to migrate}
|
|
|
20 |
{--path= : The location where the migration file should be created}
|
|
|
21 |
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
|
|
|
22 |
{--fullpath : Output the full path of the migration (Deprecated)}';
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* The console command description.
|
|
|
26 |
*
|
|
|
27 |
* @var string
|
|
|
28 |
*/
|
|
|
29 |
protected $description = 'Create a new migration file';
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* The migration creator instance.
|
|
|
33 |
*
|
|
|
34 |
* @var \Illuminate\Database\Migrations\MigrationCreator
|
|
|
35 |
*/
|
|
|
36 |
protected $creator;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* The Composer instance.
|
|
|
40 |
*
|
|
|
41 |
* @var \Illuminate\Support\Composer
|
|
|
42 |
*/
|
|
|
43 |
protected $composer;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Create a new migration install command instance.
|
|
|
47 |
*
|
|
|
48 |
* @param \Illuminate\Database\Migrations\MigrationCreator $creator
|
|
|
49 |
* @param \Illuminate\Support\Composer $composer
|
|
|
50 |
* @return void
|
|
|
51 |
*/
|
|
|
52 |
public function __construct(MigrationCreator $creator, Composer $composer)
|
|
|
53 |
{
|
|
|
54 |
parent::__construct();
|
|
|
55 |
|
|
|
56 |
$this->creator = $creator;
|
|
|
57 |
$this->composer = $composer;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Execute the console command.
|
|
|
62 |
*
|
|
|
63 |
* @return void
|
|
|
64 |
*/
|
|
|
65 |
public function handle()
|
|
|
66 |
{
|
|
|
67 |
// It's possible for the developer to specify the tables to modify in this
|
|
|
68 |
// schema operation. The developer may also specify if this table needs
|
|
|
69 |
// to be freshly created so we can create the appropriate migrations.
|
|
|
70 |
$name = Str::snake(trim($this->input->getArgument('name')));
|
|
|
71 |
|
|
|
72 |
$table = $this->input->getOption('table');
|
|
|
73 |
|
|
|
74 |
$create = $this->input->getOption('create') ?: false;
|
|
|
75 |
|
|
|
76 |
// If no table was given as an option but a create option is given then we
|
|
|
77 |
// will use the "create" option as the table name. This allows the devs
|
|
|
78 |
// to pass a table name into this option as a short-cut for creating.
|
|
|
79 |
if (! $table && is_string($create)) {
|
|
|
80 |
$table = $create;
|
|
|
81 |
|
|
|
82 |
$create = true;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// Next, we will attempt to guess the table name if this the migration has
|
|
|
86 |
// "create" in the name. This will allow us to provide a convenient way
|
|
|
87 |
// of creating migrations that create new tables for the application.
|
|
|
88 |
if (! $table) {
|
|
|
89 |
[$table, $create] = TableGuesser::guess($name);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Now we are ready to write the migration out to disk. Once we've written
|
|
|
93 |
// the migration out, we will dump-autoload for the entire framework to
|
|
|
94 |
// make sure that the migrations are registered by the class loaders.
|
|
|
95 |
$this->writeMigration($name, $table, $create);
|
|
|
96 |
|
|
|
97 |
$this->composer->dumpAutoloads();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Write the migration file to disk.
|
|
|
102 |
*
|
|
|
103 |
* @param string $name
|
|
|
104 |
* @param string $table
|
|
|
105 |
* @param bool $create
|
|
|
106 |
* @return string
|
|
|
107 |
*/
|
|
|
108 |
protected function writeMigration($name, $table, $create)
|
|
|
109 |
{
|
|
|
110 |
$file = $this->creator->create(
|
|
|
111 |
$name, $this->getMigrationPath(), $table, $create
|
|
|
112 |
);
|
|
|
113 |
|
|
|
114 |
$this->components->info(sprintf('Migration [%s] created successfully.', $file));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Get migration path (either specified by '--path' option or default location).
|
|
|
119 |
*
|
|
|
120 |
* @return string
|
|
|
121 |
*/
|
|
|
122 |
protected function getMigrationPath()
|
|
|
123 |
{
|
|
|
124 |
if (! is_null($targetPath = $this->input->getOption('path'))) {
|
|
|
125 |
return ! $this->usingRealPath()
|
|
|
126 |
? $this->laravel->basePath().'/'.$targetPath
|
|
|
127 |
: $targetPath;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
return parent::getMigrationPath();
|
|
|
131 |
}
|
| 399 |
lars |
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Prompt for missing input arguments using the returned questions.
|
|
|
135 |
*
|
|
|
136 |
* @return array
|
|
|
137 |
*/
|
|
|
138 |
protected function promptForMissingArgumentsUsing()
|
|
|
139 |
{
|
|
|
140 |
return [
|
|
|
141 |
'name' => 'What should the migration be named?',
|
|
|
142 |
];
|
|
|
143 |
}
|
| 148 |
lars |
144 |
}
|