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' => 'command.migrate',
  26. 'MigrateFresh' => 'command.migrate.fresh',
  27. 'MigrateInstall' => 'command.migrate.install',
  28. 'MigrateRefresh' => 'command.migrate.refresh',
  29. 'MigrateReset' => 'command.migrate.reset',
  30. 'MigrateRollback' => 'command.migrate.rollback',
  31. 'MigrateStatus' => 'command.migrate.status',
  32. 'MigrateMake' => 'command.migrate.make',
  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. $table = $app['config']['database.migrations'];
  55. return new DatabaseMigrationRepository($app['db'], $table);
  56. });
  57. }
  58. /**
  59. * Register the migrator service.
  60. *
  61. * @return void
  62. */
  63. protected function registerMigrator()
  64. {
  65. // The migrator is responsible for actually running and rollback the migration
  66. // files in the application. We'll pass in our database connection resolver
  67. // so the migrator can resolve any of these connections when it needs to.
  68. $this->app->singleton('migrator', function ($app) {
  69. $repository = $app['migration.repository'];
  70. return new Migrator($repository, $app['db'], $app['files'], $app['events']);
  71. });
  72. }
  73. /**
  74. * Register the migration creator.
  75. *
  76. * @return void
  77. */
  78. protected function registerCreator()
  79. {
  80. $this->app->singleton('migration.creator', function ($app) {
  81. return new MigrationCreator($app['files'], $app->basePath('stubs'));
  82. });
  83. }
  84. /**
  85. * Register the given commands.
  86. *
  87. * @param array $commands
  88. * @return void
  89. */
  90. protected function registerCommands(array $commands)
  91. {
  92. foreach (array_keys($commands) as $command) {
  93. $this->{"register{$command}Command"}();
  94. }
  95. $this->commands(array_values($commands));
  96. }
  97. /**
  98. * Register the command.
  99. *
  100. * @return void
  101. */
  102. protected function registerMigrateCommand()
  103. {
  104. $this->app->singleton('command.migrate', function ($app) {
  105. return new MigrateCommand($app['migrator'], $app[Dispatcher::class]);
  106. });
  107. }
  108. /**
  109. * Register the command.
  110. *
  111. * @return void
  112. */
  113. protected function registerMigrateFreshCommand()
  114. {
  115. $this->app->singleton('command.migrate.fresh', function () {
  116. return new FreshCommand;
  117. });
  118. }
  119. /**
  120. * Register the command.
  121. *
  122. * @return void
  123. */
  124. protected function registerMigrateInstallCommand()
  125. {
  126. $this->app->singleton('command.migrate.install', function ($app) {
  127. return new InstallCommand($app['migration.repository']);
  128. });
  129. }
  130. /**
  131. * Register the command.
  132. *
  133. * @return void
  134. */
  135. protected function registerMigrateMakeCommand()
  136. {
  137. $this->app->singleton('command.migrate.make', function ($app) {
  138. // Once we have the migration creator registered, we will create the command
  139. // and inject the creator. The creator is responsible for the actual file
  140. // creation of the migrations, and may be extended by these developers.
  141. $creator = $app['migration.creator'];
  142. $composer = $app['composer'];
  143. return new MigrateMakeCommand($creator, $composer);
  144. });
  145. }
  146. /**
  147. * Register the command.
  148. *
  149. * @return void
  150. */
  151. protected function registerMigrateRefreshCommand()
  152. {
  153. $this->app->singleton('command.migrate.refresh', function () {
  154. return new RefreshCommand;
  155. });
  156. }
  157. /**
  158. * Register the command.
  159. *
  160. * @return void
  161. */
  162. protected function registerMigrateResetCommand()
  163. {
  164. $this->app->singleton('command.migrate.reset', function ($app) {
  165. return new ResetCommand($app['migrator']);
  166. });
  167. }
  168. /**
  169. * Register the command.
  170. *
  171. * @return void
  172. */
  173. protected function registerMigrateRollbackCommand()
  174. {
  175. $this->app->singleton('command.migrate.rollback', function ($app) {
  176. return new RollbackCommand($app['migrator']);
  177. });
  178. }
  179. /**
  180. * Register the command.
  181. *
  182. * @return void
  183. */
  184. protected function registerMigrateStatusCommand()
  185. {
  186. $this->app->singleton('command.migrate.status', function ($app) {
  187. return new StatusCommand($app['migrator']);
  188. });
  189. }
  190. /**
  191. * Get the services provided by the provider.
  192. *
  193. * @return array
  194. */
  195. public function provides()
  196. {
  197. return array_merge([
  198. 'migrator', 'migration.repository', 'migration.creator',
  199. ], array_values($this->commands));
  200. }
  201. }