MigrationCreator.php 3.4 KB

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