-
Notifications
You must be signed in to change notification settings - Fork 83
/
CKEditorInstallerCommand.php
348 lines (271 loc) · 10.5 KB
/
CKEditorInstallerCommand.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/*
* This file is part of the FOSCKEditor Bundle.
*
* (c) 2018 - present Friends of Symfony
* (c) 2009 - 2017 Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\CKEditorBundle\Command;
use FOS\CKEditorBundle\Installer\CKEditorInstaller;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
/**
* @author GeLo <geloen.eric@gmail.com>
*/
final class CKEditorInstallerCommand extends Command
{
/**
* @var CKEditorInstaller
*/
private $installer;
public function __construct(CKEditorInstaller $installer)
{
parent::__construct();
$this->installer = $installer;
}
protected function configure(): void
{
$this
->setName('ckeditor:install')
->setDescription('Install CKEditor')
->addArgument('path', InputArgument::OPTIONAL, 'Where to install CKEditor')
->addOption(
'release',
null,
InputOption::VALUE_OPTIONAL,
'CKEditor release (basic, standard, full or custom)'
)
->addOption(
'custom-build-id',
null,
InputOption::VALUE_OPTIONAL,
'CKEditor custom build ID'
)
->addOption('tag', null, InputOption::VALUE_OPTIONAL, 'CKEditor tag (x.y.z or latest)')
->addOption(
'clear',
null,
InputOption::VALUE_OPTIONAL,
'How to clear previous CKEditor installation (drop, keep or skip)'
)
->addOption(
'exclude',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'Path to exclude when extracting CKEditor'
)
->addOption(
'no-progress-bar',
'nobar',
InputOption::VALUE_NONE,
'Hide the progress bars?'
)
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command install CKEditor in your application:
<info>php %command.full_name%</info>
You can install it at a specific path (absolute):
<info>php %command.full_name% path</info>
You can install a specific release (basic, standard or full):
<info>php %command.full_name% --release=full</info>
You can install a specific version:
<info>php %command.full_name% --tag=4.7.0</info>
You can install custom build generated on https://ckeditor.com/cke4/builder:
<info>php %command.full_name% --release=custom --custom-build-id=574a82a0d3e9226d94b0e91d10eaa372</info>
If there is a previous CKEditor installation detected,
you can control how it should be handled in non-interactive mode:
<info>php %command.full_name% --clear=drop</info>
<info>php %command.full_name% --clear=keep</info>
<info>php %command.full_name% --clear=skip</info>
You can exclude path(s) when extracting CKEditor:
<info>php %command.full_name% --exclude=samples --exclude=adapters</info>
EOF
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
{
$this->title($output);
$success = $this->installer->install($this->createOptions($input, $output));
if ($success) {
$this->success('CKEditor has been successfully installed...', $output);
} else {
$this->info('CKEditor installation has been skipped...', $output);
}
}
private function createOptions(InputInterface $input, OutputInterface $output): array
{
$options = ['notifier' => $this->createNotifier($input, $output)];
if ($input->hasArgument('path')) {
$options['path'] = $input->getArgument('path');
}
if ($input->hasOption('release')) {
$options['release'] = $input->getOption('release');
}
if ($input->hasOption('custom-build-id')) {
$options['custom_build_id'] = $input->getOption('custom-build-id');
}
if ($input->hasOption('tag')) {
$options['version'] = $input->getOption('tag');
}
if ($input->hasOption('exclude')) {
$options['excludes'] = $input->getOption('exclude');
}
if ($input->hasOption('clear')) {
$options['clear'] = $input->getOption('clear');
}
return array_filter($options);
}
private function createNotifier(InputInterface $input, OutputInterface $output): \Closure
{
$barOutput = $input->getOption('no-progress-bar') ? new NullOutput() : $output;
$clear = new ProgressBar($barOutput);
$download = new ProgressBar($barOutput);
$extract = new ProgressBar($barOutput);
return function ($type, $data) use ($input, $output, $barOutput, $clear, $download, $extract) {
switch ($type) {
case CKEditorInstaller::NOTIFY_CLEAR:
$result = $this->choice(
[
sprintf('CKEditor is already installed in "%s"...', $data),
'',
'What do you want to do?',
],
$choices = [
CKEditorInstaller::CLEAR_DROP => 'Drop the directory & reinstall CKEditor',
CKEditorInstaller::CLEAR_KEEP => 'Keep the directory & reinstall CKEditor by overriding files',
CKEditorInstaller::CLEAR_SKIP => 'Skip installation',
],
CKEditorInstaller::CLEAR_DROP,
$input,
$output
);
if (false !== ($key = array_search($result, $choices, true))) {
$result = $key;
}
if (CKEditorInstaller::CLEAR_DROP === $result) {
$this->comment(sprintf('Dropping CKEditor from "%s"', $data), $output);
}
return $result;
case CKEditorInstaller::NOTIFY_CLEAR_ARCHIVE:
$this->comment(sprintf('Dropping CKEditor ZIP archive "%s"', $data), $output);
break;
case CKEditorInstaller::NOTIFY_CLEAR_COMPLETE:
$this->finishProgressBar($clear, $barOutput);
break;
case CKEditorInstaller::NOTIFY_CLEAR_PROGRESS:
$clear->advance();
break;
case CKEditorInstaller::NOTIFY_CLEAR_SIZE:
$clear->start($data);
break;
case CKEditorInstaller::NOTIFY_DOWNLOAD:
$this->comment(sprintf('Downloading CKEditor ZIP archive from "%s"', $data), $output);
break;
case CKEditorInstaller::NOTIFY_DOWNLOAD_COMPLETE:
$this->finishProgressBar($download, $barOutput);
break;
case CKEditorInstaller::NOTIFY_DOWNLOAD_PROGRESS:
$download->setProgress($data);
break;
case CKEditorInstaller::NOTIFY_DOWNLOAD_SIZE:
$download->start($data);
break;
case CKEditorInstaller::NOTIFY_EXTRACT:
$this->comment(sprintf('Extracting CKEditor ZIP archive to "%s"', $data), $output);
break;
case CKEditorInstaller::NOTIFY_EXTRACT_COMPLETE:
$this->finishProgressBar($extract, $barOutput);
break;
case CKEditorInstaller::NOTIFY_EXTRACT_PROGRESS:
$extract->advance();
break;
case CKEditorInstaller::NOTIFY_EXTRACT_SIZE:
$extract->start($data);
break;
}
};
}
private function title(OutputInterface $output): void
{
$output->writeln(
[
'----------------------',
'| CKEditor Installer |',
'----------------------',
'',
]
);
}
private function comment(string $message, OutputInterface $output): void
{
$output->writeln(' // '.$message);
$output->writeln('');
}
private function success(string $message, OutputInterface $output): void
{
$this->block('[OK] - '.$message, $output, 'green', 'black');
}
private function info(string $message, OutputInterface $output): void
{
$this->block('[INFO] - '.$message, $output, 'yellow', 'black');
}
private function block(
string $message,
OutputInterface $output,
string $background = null,
string $font = null
): void {
$options = [];
if (null !== $background) {
$options[] = 'bg='.$background;
}
if (null !== $font) {
$options[] = 'fg='.$font;
}
$pattern = ' %s ';
if (!empty($options)) {
$pattern = '<'.implode(';', $options).'>'.$pattern.'</>';
}
$output->writeln($block = sprintf($pattern, str_repeat(' ', strlen($message))));
$output->writeln(sprintf($pattern, $message));
$output->writeln($block);
}
/**
* @param string[] $question
* @param string[] $choices
*/
private function choice(
array $question,
array $choices,
string $default,
InputInterface $input,
OutputInterface $output
): ?string {
$helper = new QuestionHelper();
if (is_array($question)) {
$question = implode("\n", $question);
}
$result = $helper->ask(
$input,
$output,
new ChoiceQuestion($question, $choices, $default)
);
$output->writeln('');
return $result;
}
private function finishProgressBar(ProgressBar $progress, OutputInterface $output): void
{
$progress->finish();
$output->writeln(['', '']);
}
}