ResourceCommand.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Devtool\Generator;
  12. use Hyperf\Command\Annotation\Command;
  13. use Hyperf\Stringable\Str;
  14. use Symfony\Component\Console\Input\InputOption;
  15. #[Command]
  16. class ResourceCommand extends GeneratorCommand
  17. {
  18. public function __construct()
  19. {
  20. parent::__construct('gen:resource');
  21. }
  22. public function configure()
  23. {
  24. parent::configure();
  25. $this->setDescription('create a new resource');
  26. $this->addOption('collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection');
  27. $this->addOption('grpc', null, InputOption::VALUE_NONE, 'Create a grpc resource');
  28. }
  29. protected function getStub(): string
  30. {
  31. return $this->isGrpc()
  32. ? __DIR__ . '/stubs/resource-grpc.stub'
  33. : ($this->isCollection()
  34. ? __DIR__ . '/stubs/resource-collection.stub'
  35. : __DIR__ . '/stubs/resource.stub');
  36. }
  37. protected function getDefaultNamespace(): string
  38. {
  39. return $this->getConfig()['namespace'] ?? 'App\\Resource';
  40. }
  41. protected function isCollection(): bool
  42. {
  43. return $this->input->getOption('collection')
  44. || Str::endsWith($this->input->getArgument('name'), 'Collection');
  45. }
  46. protected function isGrpc(): bool
  47. {
  48. return $this->input->getOption('grpc');
  49. }
  50. }