ConstantCommand.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 InvalidArgumentException;
  14. use Symfony\Component\Console\Input\InputOption;
  15. #[Command]
  16. class ConstantCommand extends GeneratorCommand
  17. {
  18. public function __construct()
  19. {
  20. parent::__construct('gen:constant');
  21. }
  22. public function configure()
  23. {
  24. $this->setDescription('Create a new constant class');
  25. $this->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Constant type, const or enum', '');
  26. parent::configure();
  27. }
  28. public function getType(): string
  29. {
  30. return (string) $this->input->getOption('type');
  31. }
  32. protected function getStub(): string
  33. {
  34. $type = $this->getType();
  35. if (! $type) {
  36. return $this->getConfig()['stub'] ?? __DIR__ . '/stubs/constant_enum.stub';
  37. }
  38. $stubs = array_merge(
  39. ['const' => __DIR__ . '/stubs/constant.stub', 'enum' => __DIR__ . '/stubs/constant_enum.stub'],
  40. $this->getConfig()['stubs'] ?? []
  41. );
  42. if (! isset($stubs[$type])) {
  43. throw new InvalidArgumentException('The type of constant is not exists.');
  44. }
  45. return $stubs[$type];
  46. }
  47. protected function getDefaultNamespace(): string
  48. {
  49. return $this->getConfig()['namespace'] ?? 'App\Constants';
  50. }
  51. }