Skip to content

Commit

Permalink
Added option type for gen:constant which you can be used to gener…
Browse files Browse the repository at this point in the history
…ate files with `const` or `enum`. (#6681)



Co-authored-by: 李铭昕 <715557344@qq.com>
  • Loading branch information
zds-s and limingxinleo committed Apr 13, 2024
1 parent 6b1cae3 commit cdce9f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
25 changes: 23 additions & 2 deletions src/Generator/ConstantCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace Hyperf\Devtool\Generator;

use Hyperf\Command\Annotation\Command;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;

#[Command]
class ConstantCommand extends GeneratorCommand
Expand All @@ -25,13 +27,32 @@ public function __construct()
public function configure()
{
$this->setDescription('Create a new constant class');

$this->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Constant type, const or enum', '');
parent::configure();
}

public function getType(): string
{
return (string) $this->input->getOption('type');
}

protected function getStub(): string
{
return $this->getConfig()['stub'] ?? __DIR__ . '/stubs/constant.stub';
$type = $this->getType();
if (! $type) {
return $this->getConfig()['stub'] ?? __DIR__ . '/stubs/constant_enum.stub';
}

$stubs = array_merge(
['const' => __DIR__ . '/stubs/constant.stub', 'enum' => __DIR__ . '/stubs/constant_enum.stub'],
$this->getConfig()['stubs'] ?? []
);

if (! isset($stubs[$type])) {
throw new InvalidArgumentException('The type of constant is not exists.');
}

return $stubs[$type];
}

protected function getDefaultNamespace(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use Hyperf\Constants\Annotation\Message;
use Hyperf\Constants\EnumConstantsTrait;

#[Constants]
enum ErrorCode: int
enum %CLASS%: int
{
use EnumConstantsTrait;

#[Message('Server Error')]
#[Message("Server Error!")]
case SERVER_ERROR = 500;
}

0 comments on commit cdce9f2

Please sign in to comment.