-
Notifications
You must be signed in to change notification settings - Fork 41
/
MigrateMakeCommand.php
120 lines (105 loc) · 3.27 KB
/
MigrateMakeCommand.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
<?php
/*
* Copyright (c) Taylor Otwell, Leroy Merlin Brasil
* Copyrights licensed under the MIT License.
* See the accompanying LICENSE file for terms.
*/
namespace MongolidLaravel\Migrations\Commands;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;
use MongolidLaravel\Migrations\MigrationCreator;
class MigrateMakeCommand extends BaseCommand
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'make:mongolid-migration {name : The name of the migration}
{--path= : The location where the migration file should be created}
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new migration file';
/**
* The migration creator instance.
*
* @var \MongolidLaravel\Migrations\MigrationCreator
*/
protected $creator;
/**
* The Composer instance.
*
* @var \Illuminate\Support\Composer
*/
protected $composer;
/**
* Create a new migration install command instance.
*
* @param \MongolidLaravel\Migrations\MigrationCreator $creator
* @param \Illuminate\Support\Composer $composer
*/
public function __construct(MigrationCreator $creator, Composer $composer)
{
parent::__construct();
$this->creator = $creator;
$this->composer = $composer;
}
/**
* Execute the console command.
*/
public function handle()
{
// It's possible for the developer to specify the collections to modify in this
// schema operation. The developer may also specify if this collection needs
// to be freshly created so we can create the appropriate migrations.
$name = Str::snake(trim($this->input->getArgument('name')));
// Now we are ready to write the migration out to disk. Once we've written
// the migration out, we will dump-autoload for the entire framework to
// make sure that the migrations are registered by the class loaders.
$this->writeMigration($name);
$this->composer->dumpAutoloads();
}
/**
* Write the migration file to disk.
*
* @param string $name
*/
protected function writeMigration($name)
{
$file = pathinfo(
$this->creator->create(
$name,
$this->getMigrationPath()
),
PATHINFO_FILENAME
);
$this->line("<info>Created Migration:</info> {$file}");
}
/**
* Get migration path (either specified by '--path' option or default location).
*
* @return string
*/
protected function getMigrationPath()
{
if (!is_null($targetPath = $this->input->getOption('path'))) {
return !$this->usingRealPath()
? $this->laravel->basePath().'/'.$targetPath
: $targetPath;
}
return parent::getMigrationPath();
}
/**
* Determine if the given path(s) are pre-resolved "real" paths.
*
* @return bool
*/
protected function usingRealPath()
{
return $this->input->hasOption('realpath') && $this->option('realpath');
}
}