RepositoryCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Str;
  6. use Prettus\Repository\Generators\FileAlreadyExistsException;
  7. use Prettus\Repository\Generators\MigrationGenerator;
  8. use Prettus\Repository\Generators\ModelGenerator;
  9. use Prettus\Repository\Generators\RepositoryEloquentGenerator;
  10. use Prettus\Repository\Generators\RepositoryInterfaceGenerator;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. /**
  14. * Class RepositoryCommand
  15. * @package Prettus\Repository\Generators\Commands
  16. * @author Anderson Andrade <contato@andersonandra.de>
  17. */
  18. class RepositoryCommand extends Command
  19. {
  20. /**
  21. * The name of command.
  22. *
  23. * @var string
  24. */
  25. protected $name = 'make:repository';
  26. /**
  27. * The description of command.
  28. *
  29. * @var string
  30. */
  31. protected $description = 'Create a new repository.';
  32. /**
  33. * The type of class being generated.
  34. *
  35. * @var string
  36. */
  37. protected $type = 'Repository';
  38. /**
  39. * @var Collection
  40. */
  41. protected $generators = null;
  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. $this->generators = new Collection();
  60. // $migrationGenerator = new MigrationGenerator([
  61. // 'name' => 'create_' . Str::snake(Str::plural($this->argument('name'))) . '_table',
  62. // 'fields' => $this->option('fillable'),
  63. // 'force' => $this->option('force'),
  64. // ]);
  65. // if (!$this->option('skip-migration')) {
  66. // $this->generators->push($migrationGenerator);
  67. // }
  68. $modelGenerator = new ModelGenerator([
  69. 'name' => $this->argument('name'),
  70. 'fillable' => $this->option('fillable'),
  71. 'force' => $this->option('force')
  72. ]);
  73. if (!$this->option('skip-model')) {
  74. $this->generators->push($modelGenerator);
  75. }
  76. $this->generators->push(new RepositoryInterfaceGenerator([
  77. 'name' => $this->argument('name'),
  78. 'force' => $this->option('force'),
  79. ]));
  80. foreach ($this->generators as $generator) {
  81. $generator->run();
  82. }
  83. $model = $modelGenerator->getRootNamespace() . '\\' . $modelGenerator->getName();
  84. $model = str_replace([
  85. "\\",
  86. '/'
  87. ], '\\', $model);
  88. try {
  89. (new RepositoryEloquentGenerator([
  90. 'name' => $this->argument('name'),
  91. 'rules' => $this->option('rules'),
  92. 'validator' => $this->option('validator'),
  93. 'force' => $this->option('force'),
  94. 'model' => $model
  95. ]))->run();
  96. $this->info("Repository created successfully.");
  97. } catch (FileAlreadyExistsException $e) {
  98. $this->error($this->type . ' already exists!');
  99. return false;
  100. }
  101. }
  102. /**
  103. * The array of command arguments.
  104. *
  105. * @return array
  106. */
  107. public function getArguments()
  108. {
  109. return [
  110. [
  111. 'name',
  112. InputArgument::REQUIRED,
  113. 'The name of class being generated.',
  114. null
  115. ],
  116. ];
  117. }
  118. /**
  119. * The array of command options.
  120. *
  121. * @return array
  122. */
  123. public function getOptions()
  124. {
  125. return [
  126. [
  127. 'fillable',
  128. null,
  129. InputOption::VALUE_OPTIONAL,
  130. 'The fillable attributes.',
  131. null
  132. ],
  133. [
  134. 'rules',
  135. null,
  136. InputOption::VALUE_OPTIONAL,
  137. 'The rules of validation attributes.',
  138. null
  139. ],
  140. [
  141. 'validator',
  142. null,
  143. InputOption::VALUE_OPTIONAL,
  144. 'Adds validator reference to the repository.',
  145. null
  146. ],
  147. [
  148. 'force',
  149. 'f',
  150. InputOption::VALUE_NONE,
  151. 'Force the creation if file already exists.',
  152. null
  153. ],
  154. [
  155. 'skip-migration',
  156. null,
  157. InputOption::VALUE_NONE,
  158. 'Skip the creation of a migration file.',
  159. null,
  160. ],
  161. [
  162. 'skip-model',
  163. null,
  164. InputOption::VALUE_NONE,
  165. 'Skip the creation of a model.',
  166. null,
  167. ],
  168. ];
  169. }
  170. }