-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-devtb-cli.php
More file actions
545 lines (481 loc) · 14.7 KB
/
Copy pathclass-devtb-cli.php
File metadata and controls
545 lines (481 loc) · 14.7 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
/**
* DEVTB CLI Command Handler
*
* Routes and executes CLI commands for the Translation Bridge
*
* @package DevelopmentTranslation_Bridge
* @subpackage CLI
* @version 5.1.0
*/
class DEVTB_CLI {
/**
* Command line arguments
*
* @var array
*/
private array $args;
/**
* Parsed command
*
* @var string
*/
private string $command = 'help';
/**
* Command options
*
* @var array
*/
private array $options = array();
/**
* Command parameters
*
* @var array
*/
private array $params = array();
/**
* Supported frameworks (slug => display label).
*
* Populated in the constructor from DEVTB_Converter_Factory so the CLI
* stays in lockstep with the REST API and admin UI.
*
* @var array<string,string>
*/
private array $frameworks = array();
/**
* Logger instance
*
* @var DEVTB_Logger
*/
private DEVTB_Logger $logger;
/**
* File handler instance
*
* @var DEVTB_File_Handler
*/
private DEVTB_File_Handler $file_handler;
/**
* Constructor
*
* @param array $args Command line arguments.
*/
public function __construct( array $args ) {
$this->args = $args;
$this->logger = new DEVTB_Logger();
$this->file_handler = new DEVTB_File_Handler();
$this->frameworks = self::build_framework_labels();
$this->parse_arguments();
}
/**
* Build the slug => display label map from the converter factory.
*
* @return array<string,string>
*/
private static function build_framework_labels(): array {
$labels = array();
foreach ( \DEVTB\TranslationBridge\Core\DEVTB_Converter_Factory::get_framework_info() as $slug => $meta ) {
$labels[ $slug ] = trim( $meta['name'] . ' ' . $meta['cms_version'] );
}
return $labels;
}
/**
* Validate that a framework is supported
*
* @param string $framework Framework identifier.
* @param string $param_name Parameter name for error message.
* @return bool True if valid, false and outputs error if not.
*/
private function validate_framework( string $framework, string $param_name = 'framework' ): bool {
if ( isset( $this->frameworks[ $framework ] ) ) {
return true;
}
$this->error( "Unknown {$param_name} framework: {$framework}" );
$this->list_frameworks();
return false;
}
/**
* Validate that an input file exists
*
* @param string $file_path Path to the input file.
* @return bool True if exists, false and outputs error if not.
*/
private function validate_input_file( string $file_path ): bool {
if ( file_exists( $file_path ) ) {
return true;
}
$this->error( "Input file not found: {$file_path}" );
return false;
}
/**
* Get the current command name
*
* @return string The current command.
*/
public function get_command(): string {
return $this->command;
}
/**
* Get parsed parameters
*
* @return array The parameters array.
*/
public function get_params(): array {
return $this->params;
}
/**
* Get parsed options
*
* @return array The options array.
*/
public function get_options(): array {
return $this->options;
}
/**
* Parse command line arguments
*
* @return void
*/
private function parse_arguments(): void {
$positional = array();
$i = 0;
$count = count( $this->args );
while ( $i < $count ) {
$arg = $this->args[ $i ];
// Check if it's an option.
if ( 0 === strpos( $arg, '--' ) ) {
// Long option (--option or --option=value).
if ( false !== strpos( $arg, '=' ) ) {
list( $key, $value ) = explode( '=', substr( $arg, 2 ), 2 );
$this->options[ $key ] = $value;
} else {
$key = substr( $arg, 2 );
// Boolean flags never consume the next argument as a value;
// they're declared in self::BOOLEAN_FLAGS / BOOLEAN_SHORT_FLAGS.
if (
! in_array( $key, self::BOOLEAN_FLAGS, true )
&& $i + 1 < $count
&& 0 !== strpos( $this->args[ $i + 1 ], '-' )
) {
$this->options[ $key ] = $this->args[ $i + 1 ];
$i++;
} else {
$this->options[ $key ] = true;
}
}
} elseif ( 0 === strpos( $arg, '-' ) && 2 === strlen( $arg ) ) {
// Short option (-o or -o value).
// NB: short flags stay greedy to preserve `-d <dir>` (output-dir) usage;
// disambiguating short flags requires per-command schemas and is out of scope here.
$key = substr( $arg, 1 );
if ( $i + 1 < $count && 0 !== strpos( $this->args[ $i + 1 ], '-' ) ) {
$this->options[ $key ] = $this->args[ $i + 1 ];
$i++;
} else {
$this->options[ $key ] = true;
}
} else {
// Positional argument.
$positional[] = $arg;
}
$i++;
}
// First positional is the command.
$this->command = ! empty( $positional ) ? array_shift( $positional ) : 'help';
$this->params = $positional;
}
/**
* Long flags that never take a value (must be true/false only).
*
* Without this list the parser would greedily consume the next positional
* argument as the flag's value (e.g. `--dry-run divi` becoming
* `dry-run=divi`), breaking the mixed positional+options case.
*/
private const BOOLEAN_FLAGS = array(
'dry-run',
'debug',
'verbose',
'ai-ready',
'force',
'help',
'version',
'quiet',
'no-color',
'json-output',
);
/**
* Execute the CLI command
*
* @return int Exit code (0 = success, non-zero = error).
*/
public function execute(): int {
// Handle global options first.
if ( $this->has_option( 'version', 'v' ) ) {
return $this->show_version();
}
if ( $this->has_option( 'help', 'h' ) || 'help' === $this->command ) {
return $this->show_help();
}
// Route to command handler.
$method = 'command_' . str_replace( '-', '_', $this->command );
if ( method_exists( $this, $method ) ) {
return $this->$method();
}
$this->error( "Unknown command: {$this->command}" );
$this->info( "Run 'devtb help' to see available commands." );
return 1;
}
/**
* Removed in 5.1: 'translate' and 'translate-all' (deprecated since
* 4.14.0). Use 'devtb transform' / 'devtb transform-all'.
*
* @return int Exit code.
*/
private function command_translate(): int {
$this->error( "'translate' was removed in 5.1 — use 'devtb transform'." );
return 1;
}
private function command_translate_all(): int {
$this->error( "'translate-all' was removed in 5.1 — use 'devtb transform-all'." );
return 1;
}
/**
* Command: list-frameworks
*
* List all supported frameworks
*/
private function command_list_frameworks()
{
$count = count($this->frameworks);
$this->info("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
$this->info(" Supported Frameworks ({$count} Total)");
$this->info("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
echo PHP_EOL;
foreach ($this->frameworks as $key => $name) {
$this->info(" {$key}");
$this->dim(" {$name}");
}
echo PHP_EOL;
$pairs = $count * ($count - 1);
$this->info("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
$this->info("Translation Pairs: {$pairs} (any framework to any other)");
$this->info("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
echo PHP_EOL;
return 0;
}
/**
* Command: validate
*
* Validate a framework file.
*
* @return int Exit code.
*/
private function command_validate(): int {
if ( count( $this->params ) < 2 ) {
$this->error( "Insufficient arguments for 'validate' command" );
$this->info( 'Usage: devtb validate <framework> <input-file>' );
$this->info( 'Example: devtb validate bootstrap hero.html' );
return 1;
}
$framework = strtolower( $this->params[0] );
$input_file = $this->params[1];
// Validate framework using helper.
if ( ! $this->validate_framework( $framework ) ) {
return 1;
}
// Validate input file using helper.
if ( ! $this->validate_input_file( $input_file ) ) {
return 1;
}
$this->info("🔍 Validating {$this->frameworks[$framework]} file...");
$this->info("File: {$input_file}");
echo PHP_EOL;
try {
// Read and parse
$input_content = $this->file_handler->read_file($input_file, $framework);
require_once DEVTB_TRANSLATION_BRIDGE . '/core/class-parser-factory.php';
$parser = \DEVTB\TranslationBridge\Core\DEVTB_Parser_Factory::create($framework);
$components = $parser->parse($input_content);
if (empty($components)) {
$this->warning("⚠️ No components found in file");
return 1;
}
$this->success("✓ File is valid");
$this->info("Components found: " . count($components));
// Show component breakdown
if ($this->has_option('verbose', 'v')) {
echo PHP_EOL;
$this->info("Component Breakdown:");
$types = [];
foreach ($components as $component) {
$type = $component->type ?? 'unknown';
$types[$type] = ($types[$type] ?? 0) + 1;
}
foreach ($types as $type => $count) {
$this->info(" {$type}: {$count}");
}
}
echo PHP_EOL;
return 0;
} catch (Exception $e) {
$this->error("✗ Validation failed: " . $e->getMessage());
return 1;
}
}
/**
* Show version information
*/
private function show_version()
{
$framework_count = count($this->frameworks);
$translation_pairs = $framework_count * ($framework_count - 1);
echo $this->bold("DEVTB - DevelopmentTranslation Bridge") . PHP_EOL;
echo "Version: " . DEVTB_VERSION . PHP_EOL;
echo "Translation Bridge™ - Universal Framework Translator" . PHP_EOL;
echo PHP_EOL;
echo "Supported Frameworks: {$framework_count}" . PHP_EOL;
echo "Translation Pairs: {$translation_pairs}" . PHP_EOL;
echo PHP_EOL;
return 0;
}
/**
* Show help information
*/
private function show_help()
{
$command = !empty($this->params) ? $this->params[0] : null;
if ($command) {
return $this->show_command_help($command);
}
echo $this->bold("DEVTB - DevelopmentTranslation Bridge CLI") . PHP_EOL;
echo "Translation Bridge™ - Universal Framework Translator" . PHP_EOL;
echo PHP_EOL;
echo $this->bold("USAGE:") . PHP_EOL;
echo " devtb <command> [arguments] [options]" . PHP_EOL;
echo PHP_EOL;
echo $this->bold("COMMANDS:") . PHP_EOL;
echo " " . $this->bold("list-frameworks") . PHP_EOL;
echo " List all supported frameworks" . PHP_EOL;
echo PHP_EOL;
echo " " . $this->bold("validate") . " <framework> <file>" . PHP_EOL;
echo " Validate a framework file" . PHP_EOL;
echo PHP_EOL;
echo " " . $this->bold("help") . " [command]" . PHP_EOL;
echo " Show help for a specific command" . PHP_EOL;
echo PHP_EOL;
echo $this->bold("GLOBAL OPTIONS:") . PHP_EOL;
echo " -h, --help Show help information" . PHP_EOL;
echo " -v, --version Show version information" . PHP_EOL;
echo " -d, --debug Enable debug mode" . PHP_EOL;
echo " -q, --quiet Suppress non-error output" . PHP_EOL;
echo " -a, --ai-ready Add AI-friendly attributes to output" . PHP_EOL;
echo PHP_EOL;
echo $this->bold("EXAMPLES:") . PHP_EOL;
echo " # Validate a file" . PHP_EOL;
echo " devtb validate bootstrap hero.html" . PHP_EOL;
echo PHP_EOL;
echo "Conversions: use 'devtb transform' (Python engine)." . PHP_EOL;
echo "For more information: devtb help <command>" . PHP_EOL;
echo PHP_EOL;
return 0;
}
/**
* Show help for a specific command
*
* @param string $command Command name
*/
private function show_command_help($command)
{
// Command-specific help would go here
$this->info("Help for command: {$command}");
$this->info("(Detailed help coming soon)");
return 0;
}
/**
* List available frameworks
*/
private function list_frameworks()
{
$this->info("Available frameworks:");
foreach ($this->frameworks as $key => $name) {
$this->info(" - {$key} ({$name})");
}
}
/**
* Check if an option exists
*
* @param string $long Long option name
* @param string $short Short option name
* @return bool
*/
private function has_option($long, $short = null)
{
return isset($this->options[$long]) || ($short && isset($this->options[$short]));
}
/**
* Get option value
*
* @param string $long Long option name
* @param string $short Short option name
* @return mixed Option value or null
*/
private function get_option($long, $short = null)
{
if (isset($this->options[$long])) {
return $this->options[$long];
}
if ($short && isset($this->options[$short])) {
return $this->options[$short];
}
return null;
}
/**
* Format bytes to human readable
*
* @param int $bytes Bytes
* @return string Formatted string
*/
private function format_bytes($bytes)
{
$units = ['B', 'KB', 'MB', 'GB'];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
// Output formatting methods
private function success($message)
{
if (!$this->has_option('quiet', 'q')) {
echo "\033[32m{$message}\033[0m" . PHP_EOL;
}
}
private function error($message)
{
fwrite(STDERR, "\033[31m{$message}\033[0m" . PHP_EOL);
}
private function warning($message)
{
if (!$this->has_option('quiet', 'q')) {
echo "\033[33m{$message}\033[0m" . PHP_EOL;
}
}
private function info($message)
{
if (!$this->has_option('quiet', 'q')) {
echo $message . PHP_EOL;
}
}
private function dim($message)
{
if (!$this->has_option('quiet', 'q')) {
return "\033[2m{$message}\033[0m";
}
return $message;
}
private function bold($message)
{
return "\033[1m{$message}\033[0m";
}
}