ScaffoldController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Encore\Admin\Helpers\Controllers;
  3. use Encore\Admin\Facades\Admin;
  4. use Encore\Admin\Helpers\Scaffold\ControllerCreator;
  5. use Encore\Admin\Helpers\Scaffold\MigrationCreator;
  6. use Encore\Admin\Helpers\Scaffold\ModelCreator;
  7. use Encore\Admin\Layout\Content;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Routing\Controller;
  10. use Illuminate\Support\Facades\Artisan;
  11. use Illuminate\Support\Facades\URL;
  12. use Illuminate\Support\MessageBag;
  13. class ScaffoldController extends Controller
  14. {
  15. public function index()
  16. {
  17. return Admin::content(function (Content $content) {
  18. $content->header('Scaffold');
  19. $dbTypes = [
  20. 'string', 'integer', 'text', 'float', 'double', 'decimal', 'boolean', 'date', 'time',
  21. 'dateTime', 'timestamp', 'char', 'mediumText', 'longText', 'tinyInteger', 'smallInteger',
  22. 'mediumInteger', 'bigInteger', 'unsignedTinyInteger', 'unsignedSmallInteger', 'unsignedMediumInteger',
  23. 'unsignedInteger', 'unsignedBigInteger', 'enum', 'json', 'jsonb', 'dateTimeTz', 'timeTz',
  24. 'timestampTz', 'nullableTimestamps', 'binary', 'ipAddress', 'macAddress',
  25. ];
  26. $action = URL::current();
  27. $content->row(view('laravel-admin-helpers::scaffold', compact('dbTypes', 'action')));
  28. });
  29. }
  30. public function store(Request $request)
  31. {
  32. $paths = [];
  33. $message = '';
  34. try {
  35. // 1. Create model.
  36. if (in_array('model', $request->get('create'))) {
  37. $modelCreator = new ModelCreator($request->get('table_name'), $request->get('model_name'));
  38. $paths['model'] = $modelCreator->create(
  39. $request->get('primary_key'),
  40. $request->get('timestamps') == 'on',
  41. $request->get('soft_deletes') == 'on'
  42. );
  43. }
  44. // 2. Create controller.
  45. if (in_array('controller', $request->get('create'))) {
  46. $paths['controller'] = (new ControllerCreator($request->get('controller_name')))
  47. ->create($request->get('model_name'));
  48. }
  49. // 3. Create migration.
  50. if (in_array('migration', $request->get('create'))) {
  51. $migrationName = 'create_'.$request->get('table_name').'_table';
  52. $paths['migration'] = (new MigrationCreator(app('files')))->buildBluePrint(
  53. $request->get('fields'),
  54. $request->get('primary_key', 'id'),
  55. $request->get('timestamps') == 'on',
  56. $request->get('soft_deletes') == 'on'
  57. )->create($migrationName, database_path('migrations'), $request->get('table_name'));
  58. }
  59. // 4. Run migrate.
  60. if (in_array('migrate', $request->get('create'))) {
  61. Artisan::call('migrate');
  62. $message = Artisan::output();
  63. }
  64. } catch (\Exception $exception) {
  65. // Delete generated files if exception thrown.
  66. app('files')->delete($paths);
  67. return $this->backWithException($exception);
  68. }
  69. return $this->backWithSuccess($paths, $message);
  70. }
  71. protected function backWithException(\Exception $exception)
  72. {
  73. $error = new MessageBag([
  74. 'title' => 'Error',
  75. 'message' => $exception->getMessage(),
  76. ]);
  77. return back()->withInput()->with(compact('error'));
  78. }
  79. protected function backWithSuccess($paths, $message)
  80. {
  81. $messages = [];
  82. foreach ($paths as $name => $path) {
  83. $messages[] = ucfirst($name).": $path";
  84. }
  85. $messages[] = "<br />$message";
  86. $success = new MessageBag([
  87. 'title' => 'Success',
  88. 'message' => implode('<br />', $messages),
  89. ]);
  90. return back()->with(compact('success'));
  91. }
  92. }