Repository navigation
Expand file tree
/
Copy pathConstantCommand.php
More file actions
62 lines (51 loc) · 1.56 KB
/
Copy pathConstantCommand.php
File metadata and controls
62 lines (51 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Devtool\Generator;
use Hyperf\Command\Annotation\Command;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;
#[Command]
class ConstantCommand extends GeneratorCommand
{
public function __construct()
{
parent::__construct('gen:constant');
}
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
{
$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
{
return $this->getConfig()['namespace'] ?? 'App\Constants';
}
}