MigrationCreator.php 5.0KB

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