1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "elao/enum" package. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Elao |
7
|
|
|
* |
8
|
|
|
* @author Elao <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Elao\Enum\Bridge\Symfony\Console\Command; |
12
|
|
|
|
13
|
|
|
use Elao\Enum\JsDumper\JsDumper; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
20
|
|
|
|
21
|
|
|
class DumpJsEnumsCommand extends Command |
22
|
|
|
{ |
23
|
|
|
protected static $defaultName = 'elao:enum:dump-js'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array<class-string<EnumInterface>, string> Paths indexed by enum FQCN |
27
|
|
|
*/ |
28
|
|
|
private $enums; |
29
|
|
|
|
30
|
|
|
/** @var string|null */ |
31
|
|
|
private $baseDir; |
32
|
|
|
|
33
|
|
|
/** @var string|null */ |
34
|
|
|
private $libPath; |
35
|
|
|
|
36
|
|
|
/** @var JsDumper */ |
37
|
|
|
private $dumper; |
38
|
|
|
|
39
|
|
|
public function __construct(array $enums = [], string $baseDir = null, string $libPath = null) |
40
|
|
|
{ |
41
|
|
|
$this->enums = $enums; |
42
|
|
|
$this->baseDir = $baseDir; |
43
|
|
|
$this->libPath = $libPath; |
44
|
|
|
|
45
|
|
|
parent::__construct(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function configure() |
49
|
|
|
{ |
50
|
|
|
$this |
51
|
|
|
->setDescription('Generate javascript enums') |
52
|
|
|
->addArgument('enums', InputArgument::IS_ARRAY, 'The enums & paths of the files where to generate the javascript enums. Format: "enum FQCN:path"') |
53
|
|
|
->addOption('base-dir', null, InputOption::VALUE_REQUIRED, 'A prefixed dir used for relative paths supplied for each of the generated enums and library path', $this->baseDir) |
54
|
|
|
->addOption('lib-path', null, InputOption::VALUE_REQUIRED, 'The path of the file were to place the javascript library sources used by the dumped enums.', $this->libPath) |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$io = new SymfonyStyle($input, $output); |
61
|
|
|
|
62
|
|
|
$io->title('Elao Enums Javascript generator'); |
63
|
|
|
|
64
|
|
|
$io->note(<<<TXT |
65
|
|
|
This command is not meant to be used as part of an automatic process updating your code. |
66
|
|
|
There is no BC promise on the generated code. Once generated, the code belongs to you. |
67
|
|
|
TXT |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$libPath = $input->getOption('lib-path'); |
71
|
|
|
|
72
|
|
|
if (!$libPath) { |
73
|
|
|
throw new \InvalidArgumentException('Please provide the "--lib-path" option'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$enums = $this->enums; |
77
|
|
|
if ($enumArgs = $input->getArgument('enums')) { |
78
|
|
|
$enums = []; |
79
|
|
|
foreach ($enumArgs as $arg) { |
|
|
|
|
80
|
|
|
list($fqcn, $path) = explode(':', $arg, 2); |
81
|
|
|
$enums[$fqcn] = $path; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->dumper = new JsDumper($libPath, $input->getOption('base-dir')); |
86
|
|
|
|
87
|
|
|
$io->comment("Generating library sources at path <info>{$this->dumper->normalizePath($libPath)}</info>"); |
88
|
|
|
|
89
|
|
|
$this->dumper->dumpLibrarySources(); |
90
|
|
|
|
91
|
|
|
foreach ($enums as $fqcn => $path) { |
92
|
|
|
$shortName = (new \ReflectionClass($fqcn))->getShortName(); |
93
|
|
|
$io->comment("Generating <info>$shortName</info> enum at path <info>{$this->dumper->normalizePath($path)}</info>"); |
94
|
|
|
$this->dumper->dumpEnumToFile($fqcn, $path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return 0; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.