BindingsCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Facades\File;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Collection;
  6. use Prettus\Repository\Generators\BindingsGenerator;
  7. use Prettus\Repository\Generators\FileAlreadyExistsException;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputOption;
  10. /**
  11. * Class BindingsCommand
  12. * @package Prettus\Repository\Generators\Commands
  13. * @author Anderson Andrade <contato@andersonandra.de>
  14. */
  15. class BindingsCommand extends Command
  16. {
  17. /**
  18. * The name of command.
  19. *
  20. * @var string
  21. */
  22. protected $name = 'make:bindings';
  23. /**
  24. * The description of command.
  25. *
  26. * @var string
  27. */
  28. protected $description = 'Add repository bindings to service provider.';
  29. /**
  30. * The type of class being generated.
  31. *
  32. * @var string
  33. */
  34. protected $type = 'Bindings';
  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. $bindingGenerator = new \App\Console\Commands\BindingsGenerator([
  54. 'name' => $this->argument('name'),
  55. 'force' => $this->option('force'),
  56. ]);
  57. // generate repository service provider
  58. if (!file_exists($bindingGenerator->getPath())) {
  59. $this->call('make:provider', [
  60. 'name' => $bindingGenerator->getConfigGeneratorClassPath($bindingGenerator->getPathConfigNode()),
  61. ]);
  62. // placeholder to mark the place in file where to prepend repository bindings
  63. $provider = File::get($bindingGenerator->getPath());
  64. File::put($bindingGenerator->getPath(), vsprintf(str_replace('//', '%s', $provider), [
  65. '//',
  66. $bindingGenerator->bindPlaceholder
  67. ]));
  68. }
  69. $bindingGenerator->run();
  70. $this->info($this->type . ' created successfully.');
  71. } catch (FileAlreadyExistsException $e) {
  72. $this->error($this->type . ' already exists!');
  73. return false;
  74. }
  75. }
  76. /**
  77. * The array of command arguments.
  78. *
  79. * @return array
  80. */
  81. public function getArguments()
  82. {
  83. return [
  84. [
  85. 'name',
  86. InputArgument::REQUIRED,
  87. 'The name of model for which the controller is being generated.',
  88. null
  89. ],
  90. ];
  91. }
  92. /**
  93. * The array of command options.
  94. *
  95. * @return array
  96. */
  97. public function getOptions()
  98. {
  99. return [
  100. [
  101. 'force',
  102. 'f',
  103. InputOption::VALUE_NONE,
  104. 'Force the creation if file already exists.',
  105. null
  106. ],
  107. ];
  108. }
  109. }