/vendor/laravel/framework/src/Illuminate/Database/MigrationServiceProvider.php

https://gitlab.com/madwanz64/laravel · PHP · 225 lines · 118 code · 25 blank · 82 comment · 0 complexity · 4854ac20139b61f34b6c61752cc731d6 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Contracts\Support\DeferrableProvider;
  4. use Illuminate\Database\Console\Migrations\FreshCommand;
  5. use Illuminate\Database\Console\Migrations\InstallCommand;
  6. use Illuminate\Database\Console\Migrations\MigrateCommand;
  7. use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
  8. use Illuminate\Database\Console\Migrations\RefreshCommand;
  9. use Illuminate\Database\Console\Migrations\ResetCommand;
  10. use Illuminate\Database\Console\Migrations\RollbackCommand;
  11. use Illuminate\Database\Console\Migrations\StatusCommand;
  12. use Illuminate\Database\Migrations\DatabaseMigrationRepository;
  13. use Illuminate\Database\Migrations\MigrationCreator;
  14. use Illuminate\Database\Migrations\Migrator;
  15. use Illuminate\Support\ServiceProvider;
  16. class MigrationServiceProvider extends ServiceProvider implements DeferrableProvider
  17. {
  18. /**
  19. * The commands to be registered.
  20. *
  21. * @var array
  22. */
  23. protected $commands = [
  24. 'Migrate' => 'command.migrate',
  25. 'MigrateFresh' => 'command.migrate.fresh',
  26. 'MigrateInstall' => 'command.migrate.install',
  27. 'MigrateRefresh' => 'command.migrate.refresh',
  28. 'MigrateReset' => 'command.migrate.reset',
  29. 'MigrateRollback' => 'command.migrate.rollback',
  30. 'MigrateStatus' => 'command.migrate.status',
  31. 'MigrateMake' => 'command.migrate.make',
  32. ];
  33. /**
  34. * Register the service provider.
  35. *
  36. * @return void
  37. */
  38. public function register()
  39. {
  40. $this->registerRepository();
  41. $this->registerMigrator();
  42. $this->registerCreator();
  43. $this->registerCommands($this->commands);
  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. return new DatabaseMigrationRepository($app['db'], $table);
  55. });
  56. }
  57. /**
  58. * Register the migrator service.
  59. *
  60. * @return void
  61. */
  62. protected function registerMigrator()
  63. {
  64. // The migrator is responsible for actually running and rollback the migration
  65. // files in the application. We'll pass in our database connection resolver
  66. // so the migrator can resolve any of these connections when it needs to.
  67. $this->app->singleton('migrator', function ($app) {
  68. $repository = $app['migration.repository'];
  69. return new Migrator($repository, $app['db'], $app['files'], $app['events']);
  70. });
  71. }
  72. /**
  73. * Register the migration creator.
  74. *
  75. * @return void
  76. */
  77. protected function registerCreator()
  78. {
  79. $this->app->singleton('migration.creator', function ($app) {
  80. return new MigrationCreator($app['files']);
  81. });
  82. }
  83. /**
  84. * Register the given commands.
  85. *
  86. * @param array $commands
  87. * @return void
  88. */
  89. protected function registerCommands(array $commands)
  90. {
  91. foreach (array_keys($commands) as $command) {
  92. $this->{"register{$command}Command"}();
  93. }
  94. $this->commands(array_values($commands));
  95. }
  96. /**
  97. * Register the command.
  98. *
  99. * @return void
  100. */
  101. protected function registerMigrateCommand()
  102. {
  103. $this->app->singleton('command.migrate', function ($app) {
  104. return new MigrateCommand($app['migrator']);
  105. });
  106. }
  107. /**
  108. * Register the command.
  109. *
  110. * @return void
  111. */
  112. protected function registerMigrateFreshCommand()
  113. {
  114. $this->app->singleton('command.migrate.fresh', function () {
  115. return new FreshCommand;
  116. });
  117. }
  118. /**
  119. * Register the command.
  120. *
  121. * @return void
  122. */
  123. protected function registerMigrateInstallCommand()
  124. {
  125. $this->app->singleton('command.migrate.install', function ($app) {
  126. return new InstallCommand($app['migration.repository']);
  127. });
  128. }
  129. /**
  130. * Register the command.
  131. *
  132. * @return void
  133. */
  134. protected function registerMigrateMakeCommand()
  135. {
  136. $this->app->singleton('command.migrate.make', function ($app) {
  137. // Once we have the migration creator registered, we will create the command
  138. // and inject the creator. The creator is responsible for the actual file
  139. // creation of the migrations, and may be extended by these developers.
  140. $creator = $app['migration.creator'];
  141. $composer = $app['composer'];
  142. return new MigrateMakeCommand($creator, $composer);
  143. });
  144. }
  145. /**
  146. * Register the command.
  147. *
  148. * @return void
  149. */
  150. protected function registerMigrateRefreshCommand()
  151. {
  152. $this->app->singleton('command.migrate.refresh', function () {
  153. return new RefreshCommand;
  154. });
  155. }
  156. /**
  157. * Register the command.
  158. *
  159. * @return void
  160. */
  161. protected function registerMigrateResetCommand()
  162. {
  163. $this->app->singleton('command.migrate.reset', 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('command.migrate.rollback', 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('command.migrate.status', 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. }