MigrationServiceProvider.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. use Illuminate\Contracts\Support\DeferrableProvider;
  5. use Illuminate\Database\Console\Migrations\FreshCommand;
  6. use Illuminate\Database\Console\Migrations\InstallCommand;
  7. use Illuminate\Database\Console\Migrations\MigrateCommand;
  8. use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
  9. use Illuminate\Database\Console\Migrations\RefreshCommand;
  10. use Illuminate\Database\Console\Migrations\ResetCommand;
  11. use Illuminate\Database\Console\Migrations\RollbackCommand;
  12. use Illuminate\Database\Console\Migrations\StatusCommand;
  13. use Illuminate\Database\Migrations\DatabaseMigrationRepository;
  14. use Illuminate\Database\Migrations\MigrationCreator;
  15. use Illuminate\Database\Migrations\Migrator;
  16. use Illuminate\Support\ServiceProvider;
  17. class MigrationServiceProvider extends ServiceProvider implements DeferrableProvider
  18. {
  19. /**
  20. * The commands to be registered.
  21. *
  22. * @var array
  23. */
  24. protected $commands = [
  25. 'Migrate' => MigrateCommand::class,
  26. 'MigrateFresh' => FreshCommand::class,
  27. 'MigrateInstall' => InstallCommand::class,
  28. 'MigrateRefresh' => RefreshCommand::class,
  29. 'MigrateReset' => ResetCommand::class,
  30. 'MigrateRollback' => RollbackCommand::class,
  31. 'MigrateStatus' => StatusCommand::class,
  32. 'MigrateMake' => MigrateMakeCommand::class,
  33. ];
  34. /**
  35. * Register the service provider.
  36. *
  37. * @return void
  38. */
  39. public function register()
  40. {
  41. $this->registerRepository();
  42. $this->registerMigrator();
  43. $this->registerCreator();
  44. $this->registerCommands($this->commands);
  45. }
  46. /**
  47. * Register the migration repository service.
  48. *
  49. * @return void
  50. */
  51. protected function registerRepository()
  52. {
  53. $this->app->singleton('migration.repository', function ($app) {
  54. $migrations = $app['config']['database.migrations'];
  55. $table = is_array($migrations) ? ($migrations['table'] ?? null) : $migrations;
  56. return new DatabaseMigrationRepository($app['db'], $table);
  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. return new Migrator($repository, $app['db'], $app['files'], $app['events']);
  72. });
  73. }
  74. /**
  75. * Register the migration creator.
  76. *
  77. * @return void
  78. */
  79. protected function registerCreator()
  80. {
  81. $this->app->singleton('migration.creator', function ($app) {
  82. return new MigrationCreator($app['files'], $app->basePath('stubs'));
  83. });
  84. }
  85. /**
  86. * Register the given commands.
  87. *
  88. * @param array $commands
  89. * @return void
  90. */
  91. protected function registerCommands(array $commands)
  92. {
  93. foreach (array_keys($commands) as $command) {
  94. $this->{"register{$command}Command"}();
  95. }
  96. $this->commands(array_values($commands));
  97. }
  98. /**
  99. * Register the command.
  100. *
  101. * @return void
  102. */
  103. protected function registerMigrateCommand()
  104. {
  105. $this->app->singleton(MigrateCommand::class, function ($app) {
  106. return new MigrateCommand($app['migrator'], $app[Dispatcher::class]);
  107. });
  108. }
  109. /**
  110. * Register the command.
  111. *
  112. * @return void
  113. */
  114. protected function registerMigrateFreshCommand()
  115. {
  116. $this->app->singleton(FreshCommand::class, function ($app) {
  117. return new FreshCommand($app['migrator']);
  118. });
  119. }
  120. /**
  121. * Register the command.
  122. *
  123. * @return void
  124. */
  125. protected function registerMigrateInstallCommand()
  126. {
  127. $this->app->singleton(InstallCommand::class, function ($app) {
  128. return new InstallCommand($app['migration.repository']);
  129. });
  130. }
  131. /**
  132. * Register the command.
  133. *
  134. * @return void
  135. */
  136. protected function registerMigrateMakeCommand()
  137. {
  138. $this->app->singleton(MigrateMakeCommand::class, function ($app) {
  139. // Once we have the migration creator registered, we will create the command
  140. // and inject the creator. The creator is responsible for the actual file
  141. // creation of the migrations, and may be extended by these developers.
  142. $creator = $app['migration.creator'];
  143. $composer = $app['composer'];
  144. return new MigrateMakeCommand($creator, $composer);
  145. });
  146. }
  147. /**
  148. * Register the command.
  149. *
  150. * @return void
  151. */
  152. protected function registerMigrateRefreshCommand()
  153. {
  154. $this->app->singleton(RefreshCommand::class);
  155. }
  156. /**
  157. * Register the command.
  158. *
  159. * @return void
  160. */
  161. protected function registerMigrateResetCommand()
  162. {
  163. $this->app->singleton(ResetCommand::class, function ($app) {
  164. return new ResetCommand($app['migrator']);
  165. });
  166. }
  167. /**
  168. * Register the command.
  169. *
  170. * @return void
  171. */
  172. protected function registerMigrateRollbackCommand()
  173. {
  174. $this->app->singleton(RollbackCommand::class, function ($app) {
  175. return new RollbackCommand($app['migrator']);
  176. });
  177. }
  178. /**
  179. * Register the command.
  180. *
  181. * @return void
  182. */
  183. protected function registerMigrateStatusCommand()
  184. {
  185. $this->app->singleton(StatusCommand::class, function ($app) {
  186. return new StatusCommand($app['migrator']);
  187. });
  188. }
  189. /**
  190. * Get the services provided by the provider.
  191. *
  192. * @return array
  193. */
  194. public function provides()
  195. {
  196. return array_merge([
  197. 'migrator', 'migration.repository', 'migration.creator',
  198. ], array_values($this->commands));
  199. }
  200. }