You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrationCreator.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Illuminate\Database\Migrations;
  3. use Closure;
  4. use Illuminate\Support\Str;
  5. use InvalidArgumentException;
  6. use Illuminate\Filesystem\Filesystem;
  7. class MigrationCreator
  8. {
  9. /**
  10. * The filesystem instance.
  11. *
  12. * @var \Illuminate\Filesystem\Filesystem
  13. */
  14. protected $files;
  15. /**
  16. * The registered post create hooks.
  17. *
  18. * @var array
  19. */
  20. protected $postCreate = [];
  21. /**
  22. * Create a new migration creator instance.
  23. *
  24. * @param \Illuminate\Filesystem\Filesystem $files
  25. * @return void
  26. */
  27. public function __construct(Filesystem $files)
  28. {
  29. $this->files = $files;
  30. }
  31. /**
  32. * Create a new migration at the given path.
  33. *
  34. * @param string $name
  35. * @param string $path
  36. * @param string $table
  37. * @param bool $create
  38. * @return string
  39. *
  40. * @throws \Exception
  41. */
  42. public function create($name, $path, $table = null, $create = false)
  43. {
  44. $this->ensureMigrationDoesntAlreadyExist($name);
  45. // First we will get the stub file for the migration, which serves as a type
  46. // of template for the migration. Once we have those we will populate the
  47. // various place-holders, save the file, and run the post create event.
  48. $stub = $this->getStub($table, $create);
  49. $this->files->put(
  50. $path = $this->getPath($name, $path),
  51. $this->populateStub($name, $stub, $table)
  52. );
  53. // Next, we will fire any hooks that are supposed to fire after a migration is
  54. // created. Once that is done we'll be ready to return the full path to the
  55. // migration file so it can be used however it's needed by the developer.
  56. $this->firePostCreateHooks($table);
  57. return $path;
  58. }
  59. /**
  60. * Ensure that a migration with the given name doesn't already exist.
  61. *
  62. * @param string $name
  63. * @return void
  64. *
  65. * @throws \InvalidArgumentException
  66. */
  67. protected function ensureMigrationDoesntAlreadyExist($name)
  68. {
  69. if (class_exists($className = $this->getClassName($name))) {
  70. throw new InvalidArgumentException("A {$className} class already exists.");
  71. }
  72. }
  73. /**
  74. * Get the migration stub file.
  75. *
  76. * @param string $table
  77. * @param bool $create
  78. * @return string
  79. */
  80. protected function getStub($table, $create)
  81. {
  82. if (is_null($table)) {
  83. return $this->files->get($this->stubPath().'/blank.stub');
  84. }
  85. // We also have stubs for creating new tables and modifying existing tables
  86. // to save the developer some typing when they are creating a new tables
  87. // or modifying existing tables. We'll grab the appropriate stub here.
  88. $stub = $create ? 'create.stub' : 'update.stub';
  89. return $this->files->get($this->stubPath()."/{$stub}");
  90. }
  91. /**
  92. * Populate the place-holders in the migration stub.
  93. *
  94. * @param string $name
  95. * @param string $stub
  96. * @param string $table
  97. * @return string
  98. */
  99. protected function populateStub($name, $stub, $table)
  100. {
  101. $stub = str_replace('DummyClass', $this->getClassName($name), $stub);
  102. // Here we will replace the table place-holders with the table specified by
  103. // the developer, which is useful for quickly creating a tables creation
  104. // or update migration from the console instead of typing it manually.
  105. if (! is_null($table)) {
  106. $stub = str_replace('DummyTable', $table, $stub);
  107. }
  108. return $stub;
  109. }
  110. /**
  111. * Get the class name of a migration name.
  112. *
  113. * @param string $name
  114. * @return string
  115. */
  116. protected function getClassName($name)
  117. {
  118. return Str::studly($name);
  119. }
  120. /**
  121. * Get the full path to the migration.
  122. *
  123. * @param string $name
  124. * @param string $path
  125. * @return string
  126. */
  127. protected function getPath($name, $path)
  128. {
  129. return $path.'/'.$this->getDatePrefix().'_'.$name.'.php';
  130. }
  131. /**
  132. * Fire the registered post create hooks.
  133. *
  134. * @param string $table
  135. * @return void
  136. */
  137. protected function firePostCreateHooks($table)
  138. {
  139. foreach ($this->postCreate as $callback) {
  140. call_user_func($callback, $table);
  141. }
  142. }
  143. /**
  144. * Register a post migration create hook.
  145. *
  146. * @param \Closure $callback
  147. * @return void
  148. */
  149. public function afterCreate(Closure $callback)
  150. {
  151. $this->postCreate[] = $callback;
  152. }
  153. /**
  154. * Get the date prefix for the migration.
  155. *
  156. * @return string
  157. */
  158. protected function getDatePrefix()
  159. {
  160. return date('Y_m_d_His');
  161. }
  162. /**
  163. * Get the path to the stubs.
  164. *
  165. * @return string
  166. */
  167. public function stubPath()
  168. {
  169. return __DIR__.'/stubs';
  170. }
  171. /**
  172. * Get the filesystem instance.
  173. *
  174. * @return \Illuminate\Filesystem\Filesystem
  175. */
  176. public function getFilesystem()
  177. {
  178. return $this->files;
  179. }
  180. }