ControllerCommand.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Collection;
  5. use Prettus\Repository\Generators\ControllerGenerator;
  6. use Prettus\Repository\Generators\FileAlreadyExistsException;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. use Symfony\Component\Console\Input\InputOption;
  9. /**
  10. * Class ControllerCommand
  11. * @package Prettus\Repository\Generators\Commands
  12. * @author Anderson Andrade <contato@andersonandra.de>
  13. */
  14. class ControllerCommand extends Command
  15. {
  16. /**
  17. * The name of command.
  18. *
  19. * @var string
  20. */
  21. protected $name = 'make:resource';
  22. /**
  23. * The description of command.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Create a new RESTful controller.';
  28. /**
  29. * The type of class being generated.
  30. *
  31. * @var string
  32. */
  33. protected $type = 'Controller';
  34. /**
  35. * ControllerCommand constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->name = ((float)app()->version() >= 5.5 ? 'make:rest-controller' : 'make:resource');
  40. parent::__construct();
  41. }
  42. /**
  43. * Execute the command.
  44. *
  45. * @return void
  46. * @see fire()
  47. */
  48. public function handle()
  49. {
  50. $this->laravel->call([$this, 'fire'], func_get_args());
  51. }
  52. /**
  53. * Execute the command.
  54. *
  55. * @return void
  56. */
  57. public function fire()
  58. {
  59. try {
  60. // Generate create request for controller
  61. $this->call('make:request', [
  62. 'name' => $this->argument('name') . 'CreateRequest'
  63. ]);
  64. // Generate update request for controller
  65. $this->call('make:request', [
  66. 'name' => $this->argument('name') . 'UpdateRequest'
  67. ]);
  68. (new ControllerGenerator([
  69. 'name' => $this->argument('name'),
  70. 'force' => $this->option('force'),
  71. ]))->run();
  72. $this->info($this->type . ' created successfully.');
  73. } catch (FileAlreadyExistsException $e) {
  74. $this->error($this->type . ' already exists!');
  75. return false;
  76. }
  77. }
  78. /**
  79. * The array of command arguments.
  80. *
  81. * @return array
  82. */
  83. public function getArguments()
  84. {
  85. return [
  86. [
  87. 'name',
  88. InputArgument::REQUIRED,
  89. 'The name of model for which the controller is being generated.',
  90. null
  91. ],
  92. ];
  93. }
  94. /**
  95. * The array of command options.
  96. *
  97. * @return array
  98. */
  99. public function getOptions()
  100. {
  101. return [
  102. [
  103. 'force',
  104. 'f',
  105. InputOption::VALUE_NONE,
  106. 'Force the creation if file already exists.',
  107. null
  108. ],
  109. ];
  110. }
  111. }