1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Database;
|
4
|
|
5
|
use Illuminate\Support\ServiceProvider;
|
6
|
use Illuminate\Database\Migrations\Migrator;
|
7
|
use Illuminate\Database\Migrations\MigrationCreator;
|
8
|
use Illuminate\Database\Console\Migrations\ResetCommand;
|
9
|
use Illuminate\Database\Console\Migrations\StatusCommand;
|
10
|
use Illuminate\Database\Console\Migrations\InstallCommand;
|
11
|
use Illuminate\Database\Console\Migrations\MigrateCommand;
|
12
|
use Illuminate\Database\Console\Migrations\RefreshCommand;
|
13
|
use Illuminate\Database\Console\Migrations\RollbackCommand;
|
14
|
use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
|
15
|
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
|
16
|
|
17
|
class MigrationServiceProvider extends ServiceProvider
|
18
|
{
|
19
|
/**
|
20
|
* Indicates if loading of the provider is deferred.
|
21
|
*
|
22
|
* @var bool
|
23
|
*/
|
24
|
protected $defer = true;
|
25
|
|
26
|
/**
|
27
|
* Register the service provider.
|
28
|
*
|
29
|
* @return void
|
30
|
*/
|
31
|
public function register()
|
32
|
{
|
33
|
$this->registerRepository();
|
34
|
|
35
|
// Once we have registered the migrator instance we will go ahead and register
|
36
|
// all of the migration related commands that are used by the "Artisan" CLI
|
37
|
// so that they may be easily accessed for registering with the consoles.
|
38
|
$this->registerMigrator();
|
39
|
|
40
|
$this->registerCreator();
|
41
|
|
42
|
$this->registerCommands();
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Register the migration repository service.
|
47
|
*
|
48
|
* @return void
|
49
|
*/
|
50
|
protected function registerRepository()
|
51
|
{
|
52
|
$this->app->singleton('migration.repository', function ($app) {
|
53
|
$table = $app['config']['database.migrations'];
|
54
|
|
55
|
return new DatabaseMigrationRepository($app['db'], $table);
|
56
|
});
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Register the migrator service.
|
61
|
*
|
62
|
* @return void
|
63
|
*/
|
64
|
protected function registerMigrator()
|
65
|
{
|
66
|
// The migrator is responsible for actually running and rollback the migration
|
67
|
// files in the application. We'll pass in our database connection resolver
|
68
|
// so the migrator can resolve any of these connections when it needs to.
|
69
|
$this->app->singleton('migrator', function ($app) {
|
70
|
$repository = $app['migration.repository'];
|
71
|
|
72
|
return new Migrator($repository, $app['db'], $app['files']);
|
73
|
});
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Register the migration creator.
|
78
|
*
|
79
|
* @return void
|
80
|
*/
|
81
|
protected function registerCreator()
|
82
|
{
|
83
|
$this->app->singleton('migration.creator', function ($app) {
|
84
|
return new MigrationCreator($app['files']);
|
85
|
});
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Register all of the migration commands.
|
90
|
*
|
91
|
* @return void
|
92
|
*/
|
93
|
protected function registerCommands()
|
94
|
{
|
95
|
$commands = ['Migrate', 'Rollback', 'Reset', 'Refresh', 'Install', 'Make', 'Status'];
|
96
|
|
97
|
// We'll simply spin through the list of commands that are migration related
|
98
|
// and register each one of them with an application container. They will
|
99
|
// be resolved in the Artisan start file and registered on the console.
|
100
|
foreach ($commands as $command) {
|
101
|
$this->{'register'.$command.'Command'}();
|
102
|
}
|
103
|
|
104
|
// Once the commands are registered in the application IoC container we will
|
105
|
// register them with the Artisan start event so that these are available
|
106
|
// when the Artisan application actually starts up and is getting used.
|
107
|
$this->commands(
|
108
|
'command.migrate', 'command.migrate.make',
|
109
|
'command.migrate.install', 'command.migrate.rollback',
|
110
|
'command.migrate.reset', 'command.migrate.refresh',
|
111
|
'command.migrate.status'
|
112
|
);
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Register the "migrate" migration command.
|
117
|
*
|
118
|
* @return void
|
119
|
*/
|
120
|
protected function registerMigrateCommand()
|
121
|
{
|
122
|
$this->app->singleton('command.migrate', function ($app) {
|
123
|
return new MigrateCommand($app['migrator']);
|
124
|
});
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Register the "rollback" migration command.
|
129
|
*
|
130
|
* @return void
|
131
|
*/
|
132
|
protected function registerRollbackCommand()
|
133
|
{
|
134
|
$this->app->singleton('command.migrate.rollback', function ($app) {
|
135
|
return new RollbackCommand($app['migrator']);
|
136
|
});
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Register the "reset" migration command.
|
141
|
*
|
142
|
* @return void
|
143
|
*/
|
144
|
protected function registerResetCommand()
|
145
|
{
|
146
|
$this->app->singleton('command.migrate.reset', function ($app) {
|
147
|
return new ResetCommand($app['migrator']);
|
148
|
});
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Register the "refresh" migration command.
|
153
|
*
|
154
|
* @return void
|
155
|
*/
|
156
|
protected function registerRefreshCommand()
|
157
|
{
|
158
|
$this->app->singleton('command.migrate.refresh', function () {
|
159
|
return new RefreshCommand;
|
160
|
});
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Register the "make" migration command.
|
165
|
*
|
166
|
* @return void
|
167
|
*/
|
168
|
protected function registerMakeCommand()
|
169
|
{
|
170
|
$this->app->singleton('command.migrate.make', function ($app) {
|
171
|
// Once we have the migration creator registered, we will create the command
|
172
|
// and inject the creator. The creator is responsible for the actual file
|
173
|
// creation of the migrations, and may be extended by these developers.
|
174
|
$creator = $app['migration.creator'];
|
175
|
|
176
|
$composer = $app['composer'];
|
177
|
|
178
|
return new MigrateMakeCommand($creator, $composer);
|
179
|
});
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Register the "status" migration command.
|
184
|
*
|
185
|
* @return void
|
186
|
*/
|
187
|
protected function registerStatusCommand()
|
188
|
{
|
189
|
$this->app->singleton('command.migrate.status', function ($app) {
|
190
|
return new StatusCommand($app['migrator']);
|
191
|
});
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Register the "install" migration command.
|
196
|
*
|
197
|
* @return void
|
198
|
*/
|
199
|
protected function registerInstallCommand()
|
200
|
{
|
201
|
$this->app->singleton('command.migrate.install', function ($app) {
|
202
|
return new InstallCommand($app['migration.repository']);
|
203
|
});
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Get the services provided by the provider.
|
208
|
*
|
209
|
* @return array
|
210
|
*/
|
211
|
public function provides()
|
212
|
{
|
213
|
return [
|
214
|
'migrator', 'migration.repository', 'command.migrate',
|
215
|
'command.migrate.rollback', 'command.migrate.reset',
|
216
|
'command.migrate.refresh', 'command.migrate.install',
|
217
|
'command.migrate.status', 'migration.creator',
|
218
|
'command.migrate.make',
|
219
|
];
|
220
|
}
|
221
|
}
|