|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Command; |
|
13
|
|
|
|
|
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\Finder\Finder; |
|
20
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle; |
|
22
|
|
|
use Twig\Loader\FilesystemLoader; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
|
|
|
|
|
25
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
26
|
|
|
*/ |
|
|
|
|
|
|
27
|
|
|
class CheckGeneratorCommand extends Command |
|
28
|
|
|
{ |
|
29
|
|
|
const TPL_DIR = __DIR__.'/../Resources/generator/Check'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @var \Twig\Environment |
|
33
|
|
|
*/ |
|
34
|
|
|
private $twig; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
|
|
|
|
|
37
|
|
|
* @var SplFileInfo[] |
|
38
|
|
|
*/ |
|
39
|
|
|
private $tpls; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public function __construct(\Twig\Environment $twig, string $name = null) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct($name); |
|
44
|
|
|
$this->twig = $twig; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function configure() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this |
|
50
|
|
|
->setName( |
|
51
|
|
|
'tvi:monitor:check:generator' |
|
52
|
|
|
) |
|
53
|
|
|
->setDescription( |
|
54
|
|
|
'Generate health check skeleton' |
|
55
|
|
|
) |
|
56
|
|
|
->addArgument('check', |
|
|
|
|
|
|
57
|
|
|
InputArgument::REQUIRED, 'Check name' |
|
58
|
|
|
) |
|
59
|
|
|
->addOption( |
|
60
|
|
|
'check-space', |
|
61
|
|
|
's', |
|
62
|
|
|
InputOption::VALUE_REQUIRED, |
|
63
|
|
|
'Check space', |
|
64
|
|
|
'core' |
|
65
|
|
|
) |
|
66
|
|
|
->addOption( |
|
67
|
|
|
'group', |
|
68
|
|
|
'g', |
|
69
|
|
|
InputOption::VALUE_OPTIONAL, |
|
70
|
|
|
'Check group') |
|
|
|
|
|
|
71
|
|
|
->addOption( |
|
72
|
|
|
'no-backup', |
|
73
|
|
|
'b', |
|
74
|
|
|
InputOption::VALUE_NONE, |
|
75
|
|
|
'Do not backup existing check files.' |
|
76
|
|
|
) |
|
77
|
|
|
->setHelp( |
|
78
|
|
|
<<<"EOT" |
|
79
|
|
|
The <info>%command.name%</info> command generates check classes |
|
80
|
|
|
from tvi monitor template: |
|
81
|
|
|
|
|
82
|
|
|
* Generate a check skeleton: |
|
83
|
|
|
|
|
84
|
|
|
By default, the unmodified version of check is backed up and saved |
|
85
|
|
|
To prevent this task from creating the backup file, |
|
86
|
|
|
pass the <comment>--no-backup</comment> option: |
|
87
|
|
|
|
|
88
|
|
|
<info>php %command.full_name% "TviMonitorBundle:Check\Example" [--check-space=...] [--group=...] [--no-backup]</info> |
|
89
|
|
|
<info>php %command.full_name% ":Check\Example"</info> |
|
90
|
|
|
<info>php %command.full_name% "Check\Example"</info> |
|
91
|
|
|
|
|
92
|
|
|
EOT |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$this->twig->setLoader(new FilesystemLoader([self::TPL_DIR])); |
|
99
|
|
|
|
|
100
|
|
|
$fn = Finder::create(); |
|
101
|
|
|
$tpls = $fn->in(self::TPL_DIR)->files()->getIterator(); |
|
102
|
|
|
$this->tpls = iterator_to_array($tpls); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$this->generate($input, $output); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function getNamespace(string $bundleNamespace, string $checkNamespace, string $checkName): string |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$NAMESPACE = sprintf('%s\%s\%s', $bundleNamespace, $checkNamespace, $checkName); |
|
113
|
|
|
|
|
114
|
|
|
return preg_replace('#\\\$#', '', $NAMESPACE); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function getServicePrefix(string $bundleNamespace): string |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$SERVICE_PREFIX = preg_replace('#\\\#', '_', $bundleNamespace); |
|
120
|
|
|
$SERVICE_PREFIX = preg_replace('#bundle$#i', '', $SERVICE_PREFIX); |
|
121
|
|
|
|
|
122
|
|
|
return mb_strtolower($SERVICE_PREFIX); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function getCheckName(string $checkName): string |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
return $checkName; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected function getCheckAlias(string $checkName): string |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$CHECK_ALIAS = preg_replace('#([A-Z])#', '_\1', $checkName); |
|
133
|
|
|
$CHECK_ALIAS = preg_replace('#^_*#', '', $CHECK_ALIAS); |
|
134
|
|
|
|
|
135
|
|
|
return mb_strtolower($CHECK_ALIAS); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function getCheckGroup(InputInterface $input, string $checkAlias): string |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$group = $input->getOption('group'); |
|
141
|
|
|
|
|
142
|
|
|
return $group ? $group : $checkAlias; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
protected function getCheckSpace(InputInterface $input, OutputInterface $output): string |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$checkSpace = $input->getOption('check-space'); |
|
148
|
|
|
$checkSpace = trim($checkSpace); |
|
|
|
|
|
|
149
|
|
|
if (preg_match('/[^\w:]/', $checkSpace, $m)) { |
|
150
|
|
|
$output->writeln('<error>Bad check-space foramt</error>, check-space has to be like [:\w+]+.'); |
|
151
|
|
|
exit(1); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
if ('' === $checkSpace) { |
|
154
|
|
|
$output->writeln('<error>Check-space requeaerd</error>. Use --check-space=... to set it'); |
|
155
|
|
|
exit(1); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
$checkSpace = preg_replace('/:+/', ':', $checkSpace); |
|
158
|
|
|
|
|
159
|
|
|
return trim($checkSpace, ':').':'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function getBundle(OutputInterface $output, string $bundleName): Bundle |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
/* @var $bundle Bundle */ |
|
165
|
|
|
if (!$bundleName) { |
|
166
|
|
|
$defaultBundle = 'TviMonitorBundle'; |
|
167
|
|
|
$bundle = $this->getApplication()->getKernel()->getBundle($defaultBundle); |
|
|
|
|
|
|
168
|
|
|
$output->writeln(sprintf('<info>Use default bundle <comment>%s</comment></info>', $bundle->getNamespace())); |
|
169
|
|
|
} else { |
|
170
|
|
|
try { |
|
171
|
|
|
$bundle = $this->getApplication()->getKernel()->getBundle($bundleName); |
|
172
|
|
|
} catch (\InvalidArgumentException $e) { |
|
173
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
|
174
|
|
|
exit(1); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $bundle; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
|
|
|
|
|
182
|
|
|
* @throws \Twig_Error_Loader |
|
183
|
|
|
* @throws \Twig_Error_Runtime |
|
184
|
|
|
* @throws \Twig_Error_Syntax |
|
185
|
|
|
*/ |
|
|
|
|
|
|
186
|
|
|
protected function generate(InputInterface $input, OutputInterface $output): void |
|
187
|
|
|
{ |
|
188
|
|
|
$check = $input->getArgument('check'); |
|
189
|
|
|
$noBackup = !$input->getOption('no-backup'); |
|
190
|
|
|
|
|
191
|
|
|
$r = explode(':', $check); |
|
|
|
|
|
|
192
|
|
|
@list($bundleName, $checkPath) = (1 === \count($r)) ? [null, current($r)] : $r; |
|
193
|
|
|
|
|
194
|
|
|
$bundle = $this->getBundle($output, $bundleName); |
|
195
|
|
|
|
|
196
|
|
|
preg_match('#^(.*?)(\w+)$#', $checkPath, $m); |
|
197
|
|
|
list($checkNamespace, $checkName) = [$m[1], $m[2]]; |
|
198
|
|
|
|
|
199
|
|
|
$checkNamespace = preg_replace('#\\\$#', '', $checkNamespace); |
|
200
|
|
|
$bundleNamespace = $bundle->getNamespace(); |
|
201
|
|
|
|
|
202
|
|
|
$NAMESPACE = $this->getNamespace($bundleNamespace, $checkNamespace, $checkName); |
|
203
|
|
|
$SERVICE_PREFIX = $this->getServicePrefix($bundleNamespace); |
|
204
|
|
|
$CHECK_NAME = $this->getCheckName($checkName); |
|
205
|
|
|
$CHECK_ALIAS = $this->getCheckAlias($checkName); |
|
206
|
|
|
$CHECK_GROUP = $this->getCheckGroup($input, $CHECK_ALIAS); |
|
207
|
|
|
$CHECK_SPACE = $this->getCheckSpace($input, $output); |
|
208
|
|
|
|
|
209
|
|
|
$checkPath = sprintf('%s%s%s', $bundle->getPath(), \DIRECTORY_SEPARATOR, $checkPath); |
|
210
|
|
|
$checkPath = str_replace('\\', \DIRECTORY_SEPARATOR, $checkPath); |
|
211
|
|
|
|
|
212
|
|
|
if (is_dir($checkPath)) { |
|
213
|
|
|
if ($noBackup && is_dir($checkPath)) { |
|
214
|
|
|
$output->writeln(sprintf('<info><error>Check %s exist</error>. Use --no-backup flag to rewrite</info>', $NAMESPACE)); |
|
215
|
|
|
exit(1); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
$output->writeln(sprintf('<info>Check <comment>%s</comment> exist rewrite them</info>', $NAMESPACE)); |
|
218
|
|
|
} else { |
|
219
|
|
|
@mkdir($checkPath, 0775, true) && !is_dir($checkPath); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
foreach ($this->tpls as $f) { |
|
223
|
|
|
|
|
224
|
|
|
/* @var SplFileInfo $f */ |
|
225
|
|
|
$fName = $f->getBasename('.twig'); |
|
226
|
|
|
|
|
227
|
|
|
$path = sprintf('%s%s%s', $checkPath, \DIRECTORY_SEPARATOR, $fName); |
|
228
|
|
|
|
|
229
|
|
|
$tplData = [ |
|
230
|
|
|
'NAMESPACE' => $NAMESPACE, |
|
231
|
|
|
'SERVICE_REPFIX' => $SERVICE_PREFIX, |
|
232
|
|
|
'CHECK_NAME' => $CHECK_NAME, |
|
233
|
|
|
'CHECK_ALIAS' => $CHECK_ALIAS, |
|
234
|
|
|
'CHECK_SPACE' => $CHECK_SPACE, |
|
235
|
|
|
'CHECK_GROUP' => $CHECK_GROUP, |
|
236
|
|
|
]; |
|
237
|
|
|
$res = $this->twig->render($f->getRelativePathname(), $tplData); |
|
238
|
|
|
file_put_contents($path, $res); |
|
239
|
|
|
|
|
240
|
|
|
$output->writeln(sprintf('File <info>%s</info> generated.', $path)); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|