-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModuleDisableCommand.php
More file actions
601 lines (528 loc) · 19 KB
/
ModuleDisableCommand.php
File metadata and controls
601 lines (528 loc) · 19 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?php
declare(strict_types=1);
namespace PanicDevs\Modules\Console;
use Illuminate\Console\Command;
use PanicDevs\Modules\ManifestBuilder;
use Exception;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\select;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\info;
use function Laravel\Prompts\warning;
use function Laravel\Prompts\error;
/**
* ModuleDisableCommand
*
* Console command for disabling modules in the application.
* Provides both direct module disabling by name and interactive selection.
* Prevents disabling of protected module types (like foundation modules).
*
* Usage:
* - php artisan modules:disable # Interactive selection
* - php artisan modules:disable User # Disable User module (default type: modules)
* - php artisan modules:disable User --type=modules # Disable User module from modules type
*
* @package PanicDevs\Modules\Console
* @author PanicDevs
* @since 1.0.0
*/
class ModuleDisableCommand extends Command
{
protected $signature = 'modules:disable {module?} {--type= : Module type from configured paths} {--all : Disable all modules of specified type} {--force : Force disable protected modules}';
protected $description = 'Disable a module or multiple modules';
/**
* Execute the console command.
*
* Handles both direct module disabling and interactive selection based on provided arguments.
*/
public function handle(): void
{
$moduleName = $this->argument('module');
$type = $this->option('type');
$all = $this->option('all');
$force = $this->option('force');
// Handle --all flag
if ($all)
{
$this->disableAllModules($type, $force);
return;
}
// If no module specified, show interactive selection
if (!$moduleName)
{
$this->showInteractiveSelection();
return;
}
// If no type specified, try to infer from available types
if (!$type)
{
$type = $this->getDefaultModuleType();
if (!$type)
{
error('❌ No module type specified and no default type available.');
info('Available types: '.implode(', ', array_keys($this->getConfigPaths())));
return;
}
}
$this->disableModule($moduleName, $type, $force);
}
/**
* Show interactive module selection for disabling.
*
* Uses Laravel Prompts to provide a beautiful interactive selection experience.
* Protected modules are included when --force is used.
*/
protected function showInteractiveSelection(): void
{
info('🔍 Discovering available modules...');
$force = $this->option('force');
$statuses = $this->getStatuses();
$choices = [];
$moduleInfo = [];
foreach ($statuses as $type => $modules)
{
foreach ($modules as $module => $status)
{
if ($status && ($force || !$this->isModuleTypeProtected($type))) // Include protected modules when forced
{$key = "$type/$module";
$icon = $this->getModuleTypeIcon($type);
$protectedLabel = $this->isModuleTypeProtected($type) ? ' [PROTECTED]' : '';
$choices[$key] = "{$icon} {$module} ({$type}){$protectedLabel}";
$moduleInfo[$key] = ['name' => $module, 'type' => $type];
}
}
}
if (empty($choices))
{
if ($force)
{
warning('No modules available to disable!');
info('💡 All modules are already disabled.');
} else
{
warning('No modules available to disable!');
info('💡 Note: Protected modules cannot be disabled without --force flag.');
}
return;
}
// Ask user if they want to disable single or multiple modules
$mode = select(
label: 'How would you like to disable modules?',
options: [
'single' => '📦 Disable a single module',
'multiple' => '📦📦 Disable multiple modules',
],
hint: $force ? 'Force mode active - can disable protected modules' : 'Normal mode - protected modules excluded'
);
if ('multiple' === $mode)
{
$this->handleMultipleDisable($choices, $moduleInfo, $force);
} else
{
$this->handleSingleDisable($choices, $moduleInfo, $force);
}
}
/**
* Handle single module disable selection.
*/
protected function handleSingleDisable(array $choices, array $moduleInfo, bool $force): void
{
$hintText = $force
? 'Protected modules marked with [PROTECTED] will be force disabled!'
: 'Select a module to disable';
$selected = select(
label: 'Which module would you like to disable?',
options: $choices,
hint: $hintText
);
$moduleData = $moduleInfo[$selected];
// Enhanced confirmation prompt
$isProtected = $this->isModuleTypeProtected($moduleData['type']);
$forceText = ($force && $isProtected) ? ' (FORCE DISABLING PROTECTED MODULE)' : '';
$warningText = ($force && $isProtected) ? 'This will force disable a protected module!' : 'This action will immediately disable the module';
$confirmed = confirm(
label: "Are you sure you want to disable the '{$moduleData['name']}' module?{$forceText}",
default: false,
yes: 'Yes, disable it',
no: 'No, keep it enabled',
hint: $warningText
);
if ($confirmed)
{
$this->disableModule($moduleData['name'], $moduleData['type'], $force);
} else
{
info('👍 Module remains enabled.');
}
}
/**
* Handle multiple module disable selection.
*/
protected function handleMultipleDisable(array $choices, array $moduleInfo, bool $force): void
{
$hintText = $force
? 'Select modules to disable (including protected ones with [PROTECTED] label)'
: 'Select multiple modules to disable';
$selectedKeys = multiselect(
label: 'Which modules would you like to disable?',
options: $choices,
hint: $hintText
);
if (empty($selectedKeys))
{
info('👍 No modules selected.');
return;
}
// Gather selected modules info
$selectedModules = [];
$protectedCount = 0;
foreach ($selectedKeys as $key)
{
$moduleData = $moduleInfo[$key];
$selectedModules[] = $moduleData;
if ($this->isModuleTypeProtected($moduleData['type']))
{
$protectedCount++;
}
}
// Show summary and confirmation
$totalCount = count($selectedModules);
warning("⚠️ You are about to disable {$totalCount} modules:");
foreach ($selectedModules as $module)
{
$icon = $this->getModuleTypeIcon($module['type']);
$protectedLabel = $this->isModuleTypeProtected($module['type']) ? ' [PROTECTED]' : '';
$this->line(" {$icon} {$module['name']} ({$module['type']}){$protectedLabel}");
}
if ($protectedCount > 0)
{
warning(" 🛡️ {$protectedCount} protected modules will be force disabled!");
}
$confirmed = confirm(
label: "Are you sure you want to disable these {$totalCount} modules?",
default: false,
yes: 'Yes, disable all selected',
no: 'No, cancel',
hint: $protectedCount > 0 ? 'This includes protected modules!' : 'This will disable multiple modules'
);
if (!$confirmed)
{
info('👍 Operation cancelled.');
return;
}
// Disable modules one by one with progress
$successful = 0;
$failed = 0;
foreach ($selectedModules as $module)
{
try
{
$this->disableModule($module['name'], $module['type'], $force, true); // Skip individual confirmations and messages
$successful++;
$icon = $this->getModuleTypeIcon($module['type']);
info(" {$icon} Disabled: {$module['name']}");
} catch (Exception $e)
{
error(" ❌ Failed: {$module['name']} - {$e->getMessage()}");
$failed++;
}
}
// Clear cache once at the end
if ($successful > 0)
{
$builder = new ManifestBuilder();
$builder->clear();
}
// Summary
if ($successful > 0)
{
info("✅ Successfully disabled {$successful} modules.");
}
if ($failed > 0)
{
warning("⚠️ {$failed} modules could not be disabled.");
}
if ($successful > 0)
{
warning('💡 Remember to clear your application cache if needed.');
}
}
/**
* Disable a specific module.
*
* Validates the module exists and is not protected before disabling.
* Updates the modules_statuses.json file and clears the manifest cache.
*/
protected function disableModule(string $moduleName, string $type, bool $force = false, bool $skipMessages = false): void
{
// Check if this module type is protected (unless forced)
if (!$force && $this->isModuleTypeProtected($type))
{
error("🚫 Modules of type '{$type}' cannot be disabled (protected).");
warning('Protected modules are essential for system operation.');
warning('💡 Use --force to override protection (use with caution).');
return;
}
if ($force && $this->isModuleTypeProtected($type))
{
warning("⚠️ Force disabling protected module '{$moduleName}' of type '{$type}'!");
}
$statusFile = base_path('modules_statuses.json');
$statuses = $this->getStatuses();
$availableTypes = array_keys($this->getConfigPaths());
if (!isset($statuses[$type]))
{
error("❌ Module type '{$type}' not found.");
info('Available types: '.implode(', ', $availableTypes));
return;
}
if (!$this->moduleExists($moduleName, $type))
{
error("❌ Module '{$moduleName}' not found in '{$type}' directory.");
return;
}
if (!($statuses[$type][$moduleName] ?? false))
{
warning("⚠️ Module '{$moduleName}' is already disabled.");
return;
}
$statuses[$type][$moduleName] = false;
file_put_contents($statusFile, json_encode($statuses, JSON_PRETTY_PRINT));
// Clear cache to force rebuild (only once per operation)
if (!$skipMessages)
{
$builder = new ManifestBuilder();
$builder->clear();
}
if (!$skipMessages)
{
info("✅ Module '{$moduleName}' has been successfully disabled!");
warning('💡 Remember to clear your application cache if needed.');
}
}
/**
* Disable all modules of a specific type.
*/
protected function disableAllModules(?string $type, bool $force): void
{
// If no type specified, let user choose or use all types
if (!$type)
{
$availableTypes = array_keys($this->getConfigPaths());
if (1 === count($availableTypes))
{
$type = $availableTypes[0];
info("🎯 Using the only available type: {$type}");
} else
{
$type = select(
label: 'Which module type would you like to disable all modules for?',
options: array_combine($availableTypes, $availableTypes),
hint: 'This will disable ALL modules of the selected type'
);
}
}
$statuses = $this->getStatuses();
if (!isset($statuses[$type]))
{
error("❌ Module type '{$type}' not found.");
info('Available types: '.implode(', ', array_keys($this->getConfigPaths())));
return;
}
$modulesToDisable = [];
foreach ($statuses[$type] as $moduleName => $status)
{
if ($status) // Only enabled modules
{// Check if we can disable this module
if ($force || !$this->isModuleTypeProtected($type))
{
$modulesToDisable[] = $moduleName;
}
}
}
if (empty($modulesToDisable))
{
if ($this->isModuleTypeProtected($type) && !$force)
{
warning("⚠️ No modules to disable. Type '{$type}' is protected.");
info('💡 Use --force to override protection.');
} else
{
info("✅ All modules of type '{$type}' are already disabled.");
}
return;
}
// Show confirmation with detailed info
$protectedWarning = $this->isModuleTypeProtected($type) ? ' (PROTECTED TYPE)' : '';
$forceText = $force ? ' with --force' : '';
warning("⚠️ You are about to disable ".count($modulesToDisable)." modules of type '{$type}'{$protectedWarning}{$forceText}:");
foreach ($modulesToDisable as $module)
{
$this->line(" 📦 {$module}");
}
$confirmed = confirm(
label: 'Are you sure you want to disable all these modules?',
default: false,
yes: 'Yes, disable all',
no: 'No, cancel',
hint: $force ? 'This will force disable protected modules!' : 'This action will disable multiple modules'
);
if (!$confirmed)
{
info('👍 Operation cancelled.');
return;
}
// Disable modules with progress
$successful = 0;
$failed = 0;
foreach ($modulesToDisable as $moduleName)
{
try
{
if ($this->moduleExists($moduleName, $type))
{
$statuses[$type][$moduleName] = false;
$successful++;
$icon = $this->getModuleTypeIcon($type);
info(" {$icon} Disabled: {$moduleName}");
} else
{
warning(" ⚠️ Skipped: {$moduleName} (not found)");
$failed++;
}
} catch (Exception $e)
{
error(" ❌ Failed: {$moduleName} - {$e->getMessage()}");
$failed++;
}
}
// Save the updated statuses
if ($successful > 0)
{
$statusFile = base_path('modules_statuses.json');
file_put_contents($statusFile, json_encode($statuses, JSON_PRETTY_PRINT));
// Clear cache
$builder = new ManifestBuilder();
$builder->clear();
}
// Summary
if ($successful > 0)
{
info("✅ Successfully disabled {$successful} modules of type '{$type}'.");
}
if ($failed > 0)
{
warning("⚠️ {$failed} modules could not be disabled.");
}
if ($successful > 0)
{
warning('💡 Remember to clear your application cache if needed.');
}
}
/**
* Check if a module exists in the specified type directory.
*
* Verifies the existence of module.json file in the expected location.
*/
protected function moduleExists(string $moduleName, string $type): bool
{
$paths = $this->getConfigPaths();
if (!isset($paths[$type]))
{
return false;
}
$modulePath = base_path($paths[$type]['path'].'/'.$moduleName.'/module.json');
return file_exists($modulePath);
}
/**
* Get configured module paths with fallback to hardcoded defaults.
*
* Retrieves module path configuration from config or provides sensible defaults
* if configuration is not available.
*/
protected function getConfigPaths(): array
{
$paths = config('modules.paths', []);
// Fallback to hardcoded paths if config is empty
if (empty($paths))
{
$paths = [
'foundation' => [
'path' => 'foundation',
'namespace' => 'Foundation',
],
'modules' => [
'path' => 'modules',
'namespace' => 'Modules',
],
];
}
return $paths;
}
/**
* Get the icon for a module type from configuration.
*/
protected function getModuleTypeIcon(string $type): string
{
$paths = $this->getConfigPaths();
return $paths[$type]['icon'] ?? '📦';
}
/**
* Check if a module type is protected from being disabled.
*/
protected function isModuleTypeProtected(string $type): bool
{
$paths = $this->getConfigPaths();
return $paths[$type]['protected'] ?? false;
}
/**
* Get the default module type (first non-protected type).
*/
protected function getDefaultModuleType(): ?string
{
$paths = $this->getConfigPaths();
foreach ($paths as $type => $config)
{
if (!($config['protected'] ?? false))
{
return $type;
}
}
return null;
}
/**
* Get current module status configuration.
*
* Reads the modules_statuses.json file or creates default structure
* based on configured module types.
*/
protected function getStatuses(): array
{
$statusFile = base_path('modules_statuses.json');
if (!file_exists($statusFile))
{
// Create default structure based on config
$paths = $this->getConfigPaths();
$defaultStatuses = [];
foreach (array_keys($paths) as $type)
{
$defaultStatuses[$type] = [];
}
return $defaultStatuses;
}
$content = file_get_contents($statusFile);
$statuses = json_decode($content, true);
if (!is_array($statuses))
{
// Create default structure based on config
$paths = $this->getConfigPaths();
$defaultStatuses = [];
foreach (array_keys($paths) as $type)
{
$defaultStatuses[$type] = [];
}
return $defaultStatuses;
}
return $statuses;
}
}