PresenterCommand.php 2.8 KB

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