MigrationCreator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Dcat\Admin\Scaffold;
  3. use Illuminate\Database\Migrations\MigrationCreator as BaseMigrationCreator;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Support\Arr;
  6. class MigrationCreator extends BaseMigrationCreator
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $bluePrint = '';
  12. /**
  13. * Create a new migration creator instance.
  14. *
  15. * @param \Illuminate\Filesystem\Filesystem $files
  16. *
  17. * @return void
  18. */
  19. public function __construct(Filesystem $files)
  20. {
  21. $this->files = $files;
  22. }
  23. /**
  24. * Create a new model.
  25. *
  26. * @param string $name
  27. * @param string $path
  28. * @param null $table
  29. * @param bool|true $create
  30. *
  31. * @return string
  32. */
  33. public function create($name, $path, $table = null, $create = true)
  34. {
  35. $this->ensureMigrationDoesntAlreadyExist($name);
  36. $path = $this->getPath($name, $path);
  37. $stub = $this->files->get(__DIR__.'/stubs/create.stub');
  38. $this->files->put($path, $this->populateStub($name, $stub, $table));
  39. $this->files->chmod($path, 0777);
  40. $this->firePostCreateHooks($table);
  41. return $path;
  42. }
  43. /**
  44. * Populate stub.
  45. *
  46. * @param string $name
  47. * @param string $stub
  48. * @param string $table
  49. *
  50. * @return mixed
  51. */
  52. protected function populateStub($name, $stub, $table)
  53. {
  54. return str_replace(
  55. ['DummyClass', 'DummyTable', 'DummyStructure'],
  56. [$this->getClassName($name), $table, $this->bluePrint],
  57. $stub
  58. );
  59. }
  60. /**
  61. * Build the table blueprint.
  62. *
  63. * @param array $fields
  64. * @param string $keyName
  65. * @param bool|true $useTimestamps
  66. * @param bool|false $softDeletes
  67. *
  68. * @throws \Exception
  69. *
  70. * @return $this
  71. */
  72. public function buildBluePrint($fields = [], $keyName = 'id', $useTimestamps = true, $softDeletes = false)
  73. {
  74. $fields = array_filter($fields, function ($field) {
  75. return isset($field['name']) && ! empty($field['name']);
  76. });
  77. if (empty($fields)) {
  78. throw new \Exception('Table fields can\'t be empty');
  79. }
  80. $rows[] = "\$table->increments('$keyName');\n";
  81. foreach ($fields as $field) {
  82. $column = "\$table->{$field['type']}('{$field['name']}')";
  83. if ($field['key']) {
  84. $column .= "->{$field['key']}()";
  85. }
  86. $hasDefault = isset($field['default'])
  87. && ! is_null($field['default'])
  88. && $field['default'] !== '';
  89. if ($hasDefault) {
  90. $column .= "->default('{$field['default']}')";
  91. }
  92. if (Arr::get($field, 'nullable') == 'on') {
  93. $column .= '->nullable()';
  94. } elseif (! $hasDefault && $field['type'] === 'string') {
  95. $column .= "->default('')";
  96. }
  97. if (isset($field['comment']) && $field['comment']) {
  98. $column .= "->comment('{$field['comment']}')";
  99. }
  100. $rows[] = $column.";\n";
  101. }
  102. if ($useTimestamps) {
  103. $rows[] = "\$table->timestamps();\n";
  104. }
  105. if ($softDeletes) {
  106. $rows[] = "\$table->softDeletes();\n";
  107. }
  108. $this->bluePrint = trim(implode(str_repeat(' ', 12), $rows), "\n");
  109. return $this;
  110. }
  111. }