TransformerCommand.php 2.2 KB

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