1
+ <?php
2
+
3
+ namespace App \Console \Commands ;
4
+
5
+ use App \Modules \ModuleManager ;
6
+ use Illuminate \Console \Command ;
7
+ use Illuminate \Support \Facades \File ;
8
+
9
+ class ModuleCommand extends Command
10
+ {
11
+ /**
12
+ * The name and signature of the console command.
13
+ */
14
+ protected $ signature = 'module {action} {name?} {--force} ' ;
15
+
16
+ /**
17
+ * The console command description.
18
+ */
19
+ protected $ description = 'Manage application modules ' ;
20
+
21
+ protected ModuleManager $ moduleManager ;
22
+
23
+ public function __construct (ModuleManager $ moduleManager )
24
+ {
25
+ parent ::__construct ();
26
+ $ this ->moduleManager = $ moduleManager ;
27
+ }
28
+
29
+ /**
30
+ * Execute the console command.
31
+ */
32
+ public function handle (): int
33
+ {
34
+ $ action = $ this ->argument ('action ' );
35
+ $ name = $ this ->argument ('name ' );
36
+
37
+ return match ($ action ) {
38
+ 'list ' => $ this ->listModules (),
39
+ 'enable ' => $ this ->enableModule ($ name ),
40
+ 'disable ' => $ this ->disableModule ($ name ),
41
+ 'install ' => $ this ->installModule ($ name ),
42
+ 'uninstall ' => $ this ->uninstallModule ($ name ),
43
+ 'create ' => $ this ->createModule ($ name ),
44
+ 'info ' => $ this ->showModuleInfo ($ name ),
45
+ default => $ this ->showHelp (),
46
+ };
47
+ }
48
+
49
+ /**
50
+ * List all modules.
51
+ */
52
+ protected function listModules (): int
53
+ {
54
+ $ modules = $ this ->moduleManager ->all ();
55
+
56
+ if ($ modules ->isEmpty ()) {
57
+ $ this ->info ('No modules found. ' );
58
+ return 0 ;
59
+ }
60
+
61
+ $ this ->table (
62
+ ['Name ' , 'Version ' , 'Status ' , 'Description ' ],
63
+ $ modules ->map (function ($ module ) {
64
+ return [
65
+ $ module ->getName (),
66
+ $ module ->getVersion (),
67
+ $ module ->isEnabled () ? '<fg=green>Enabled</> ' : '<fg=red>Disabled</> ' ,
68
+ $ module ->getDescription (),
69
+ ];
70
+ })->toArray ()
71
+ );
72
+
73
+ return 0 ;
74
+ }
75
+
76
+ /**
77
+ * Enable a module.
78
+ */
79
+ protected function enableModule (?string $ name ): int
80
+ {
81
+ if (!$ name ) {
82
+ $ this ->error ('Module name is required. ' );
83
+ return 1 ;
84
+ }
85
+
86
+ try {
87
+ if ($ this ->moduleManager ->enable ($ name )) {
88
+ $ this ->info ("Module ' {$ name }' has been enabled. " );
89
+ return 0 ;
90
+ }
91
+
92
+ $ this ->error ("Module ' {$ name }' not found. " );
93
+ return 1 ;
94
+ } catch (\Exception $ e ) {
95
+ $ this ->error ("Failed to enable module ' {$ name }': " . $ e ->getMessage ());
96
+ return 1 ;
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Disable a module.
102
+ */
103
+ protected function disableModule (?string $ name ): int
104
+ {
105
+ if (!$ name ) {
106
+ $ this ->error ('Module name is required. ' );
107
+ return 1 ;
108
+ }
109
+
110
+ try {
111
+ if ($ this ->moduleManager ->disable ($ name )) {
112
+ $ this ->info ("Module ' {$ name }' has been disabled. " );
113
+ return 0 ;
114
+ }
115
+
116
+ $ this ->error ("Module ' {$ name }' not found. " );
117
+ return 1 ;
118
+ } catch (\Exception $ e ) {
119
+ $ this ->error ("Failed to disable module ' {$ name }': " . $ e ->getMessage ());
120
+ return 1 ;
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Install a module.
126
+ */
127
+ protected function installModule (?string $ name ): int
128
+ {
129
+ if (!$ name ) {
130
+ $ this ->error ('Module name is required. ' );
131
+ return 1 ;
132
+ }
133
+
134
+ try {
135
+ if ($ this ->moduleManager ->install ($ name )) {
136
+ $ this ->info ("Module ' {$ name }' has been installed and enabled. " );
137
+ return 0 ;
138
+ }
139
+
140
+ $ this ->error ("Module ' {$ name }' not found. " );
141
+ return 1 ;
142
+ } catch (\Exception $ e ) {
143
+ $ this ->error ("Failed to install module ' {$ name }': " . $ e ->getMessage ());
144
+ return 1 ;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Uninstall a module.
150
+ */
151
+ protected function uninstallModule (?string $ name ): int
152
+ {
153
+ if (!$ name ) {
154
+ $ this ->error ('Module name is required. ' );
155
+ return 1 ;
156
+ }
157
+
158
+ if (!$ this ->option ('force ' )) {
159
+ if (!$ this ->confirm ("Are you sure you want to uninstall module ' {$ name }'? This action cannot be undone. " )) {
160
+ $ this ->info ('Operation cancelled. ' );
161
+ return 0 ;
162
+ }
163
+ }
164
+
165
+ try {
166
+ if ($ this ->moduleManager ->uninstall ($ name )) {
167
+ $ this ->info ("Module ' {$ name }' has been uninstalled. " );
168
+ return 0 ;
169
+ }
170
+
171
+ $ this ->error ("Module ' {$ name }' not found. " );
172
+ return 1 ;
173
+ } catch (\Exception $ e ) {
174
+ $ this ->error ("Failed to uninstall module ' {$ name }': " . $ e ->getMessage ());
175
+ return 1 ;
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Create a new module.
181
+ */
182
+ protected function createModule (?string $ name ): int
183
+ {
184
+ if (!$ name ) {
185
+ $ this ->error ('Module name is required. ' );
186
+ return 1 ;
187
+ }
188
+
189
+ $ modulePath = app_path ("Modules/ {$ name }" );
190
+
191
+ if (File::exists ($ modulePath )) {
192
+ $ this ->error ("Module ' {$ name }' already exists. " );
193
+ return 1 ;
194
+ }
195
+
196
+ $ this ->createModuleStructure ($ name , $ modulePath );
197
+ $ this ->info ("Module ' {$ name }' has been created successfully. " );
198
+
199
+ return 0 ;
200
+ }
201
+
202
+ /**
203
+ * Show module information.
204
+ */
205
+ protected function showModuleInfo (?string $ name ): int
206
+ {
207
+ if (!$ name ) {
208
+ $ this ->error ('Module name is required. ' );
209
+ return 1 ;
210
+ }
211
+
212
+ $ info = $ this ->moduleManager ->getModuleInfo ($ name );
213
+
214
+ if (empty ($ info )) {
215
+ $ this ->error ("Module ' {$ name }' not found. " );
216
+ return 1 ;
217
+ }
218
+
219
+ $ this ->info ("Module Information: " );
220
+ $ this ->line ("Name: {$ info ['name ' ]}" );
221
+ $ this ->line ("Version: {$ info ['version ' ]}" );
222
+ $ this ->line ("Description: {$ info ['description ' ]}" );
223
+ $ this ->line ("Status: " . ($ info ['enabled ' ] ? 'Enabled ' : 'Disabled ' ));
224
+
225
+ if (!empty ($ info ['dependencies ' ])) {
226
+ $ this ->line ("Dependencies: " . implode (', ' , $ info ['dependencies ' ]));
227
+ }
228
+
229
+ return 0 ;
230
+ }
231
+
232
+ /**
233
+ * Create module directory structure.
234
+ */
235
+ protected function createModuleStructure (string $ name , string $ modulePath ): void
236
+ {
237
+ // Create directories
238
+ $ directories = [
239
+ 'Providers ' ,
240
+ 'Http/Controllers ' ,
241
+ 'Http/Middleware ' ,
242
+ 'Models ' ,
243
+ 'Services ' ,
244
+ 'resources/views ' ,
245
+ 'resources/lang ' ,
246
+ 'resources/assets ' ,
247
+ 'routes ' ,
248
+ 'database/migrations ' ,
249
+ 'database/seeders ' ,
250
+ 'config ' ,
251
+ 'tests ' ,
252
+ ];
253
+
254
+ foreach ($ directories as $ directory ) {
255
+ File::makeDirectory ("{$ modulePath }/ {$ directory }" , 0755 , true );
256
+ }
257
+
258
+ // Create module.json
259
+ $ moduleInfo = [
260
+ 'name ' => $ name ,
261
+ 'version ' => '1.0.0 ' ,
262
+ 'description ' => "Custom {$ name } module " ,
263
+ 'dependencies ' => [],
264
+ 'config ' => [],
265
+ ];
266
+
267
+ File::put ("{$ modulePath }/module.json " , json_encode ($ moduleInfo , JSON_PRETTY_PRINT ));
268
+
269
+ // Create module class
270
+ $ moduleClass = $ this ->getModuleClassStub ($ name );
271
+ File::put ("{$ modulePath }/ {$ name }Module.php " , $ moduleClass );
272
+
273
+ // Create service provider
274
+ $ serviceProvider = $ this ->getServiceProviderStub ($ name );
275
+ File::put ("{$ modulePath }/Providers/ {$ name }ServiceProvider.php " , $ serviceProvider );
276
+
277
+ // Create routes files
278
+ File::put ("{$ modulePath }/routes/web.php " , "<?php \n\n// Web routes for {$ name } module \n" );
279
+ File::put ("{$ modulePath }/routes/api.php " , "<?php \n\n// API routes for {$ name } module \n" );
280
+ }
281
+
282
+ /**
283
+ * Get module class stub.
284
+ */
285
+ protected function getModuleClassStub (string $ name ): string
286
+ {
287
+ return "<?php
288
+
289
+ namespace App \\Modules \\{$ name };
290
+
291
+ use App \\Modules \\BaseModule;
292
+
293
+ class {$ name }Module extends BaseModule
294
+ {
295
+ protected function onEnable(): void
296
+ {
297
+ // Called when module is enabled
298
+ }
299
+
300
+ protected function onDisable(): void
301
+ {
302
+ // Called when module is disabled
303
+ }
304
+
305
+ protected function onInstall(): void
306
+ {
307
+ // Called when module is installed
308
+ }
309
+
310
+ protected function onUninstall(): void
311
+ {
312
+ // Called when module is uninstalled
313
+ }
314
+ }
315
+ " ;
316
+ }
317
+
318
+ /**
319
+ * Get service provider stub.
320
+ */
321
+ protected function getServiceProviderStub (string $ name ): string
322
+ {
323
+ return "<?php
324
+
325
+ namespace App \\Modules \\{$ name }\\Providers;
326
+
327
+ use Illuminate \\Support \\ServiceProvider;
328
+
329
+ class {$ name }ServiceProvider extends ServiceProvider
330
+ {
331
+ /**
332
+ * Register any application services.
333
+ */
334
+ public function register(): void
335
+ {
336
+ // Register module services
337
+ }
338
+
339
+ /**
340
+ * Bootstrap any application services.
341
+ */
342
+ public function boot(): void
343
+ {
344
+ // Boot module services
345
+ }
346
+ }
347
+ " ;
348
+ }
349
+
350
+ /**
351
+ * Show help information.
352
+ */
353
+ protected function showHelp (): int
354
+ {
355
+ $ this ->info ('Available actions: ' );
356
+ $ this ->line (' list List all modules ' );
357
+ $ this ->line (' enable <name> Enable a module ' );
358
+ $ this ->line (' disable <name> Disable a module ' );
359
+ $ this ->line (' install <name> Install a module ' );
360
+ $ this ->line (' uninstall <name> Uninstall a module ' );
361
+ $ this ->line (' create <name> Create a new module ' );
362
+ $ this ->line (' info <name> Show module information ' );
363
+
364
+ return 0 ;
365
+ }
366
+ }
0 commit comments