-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathConsole.php
More file actions
689 lines (575 loc) · 19.8 KB
/
Console.php
File metadata and controls
689 lines (575 loc) · 19.8 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<?php
declare(strict_types=1);
namespace Bow\Console;
use Bow\Configuration\Loader;
use Bow\Console\Exception\ConsoleException;
use Bow\Console\Traits\ConsoleTrait;
use ErrorException;
use Exception;
/**
* @method static Console addCommand(string $command, callable $cb)
*/
class Console
{
use ConsoleTrait;
/**
* Define Bow Framework version.
*
* @var string
*/
private const VERSION = '5.x';
/**
* The command list
*
* @var array
*/
private const COMMAND = [
'add',
'migration',
'migrate',
'run',
'generate',
'gen',
'seed',
'help',
'clear',
'flush',
'launch',
'serve',
];
/**
* The action list
*
* @var array
*/
private const ADD_ACTION = [
'middleware',
'controller',
'model',
'validation',
'seeder',
'migration',
'configuration',
'service',
'exception',
'event',
'job',
'command',
'listener',
'message'
];
/**
* The custom command registers
*
* @var array
*/
private static array $registers = [];
/**
* The console instance
*
* @var ?Console
*/
private static ?Console $instance = null;
/**
* The Setting instance
*
* @var Setting
*/
private Setting $setting;
/**
* The Command instance
*
* @var Command
*/
private Command $command;
/**
* The Loader instance
*
* @var Loader
*/
private Loader $kernel;
/**
* Define if console booted
*
* @var bool
*/
private bool $booted = false;
/**
* The Argument instance
*
* @var Argument
*/
private Argument $arg;
/**
* Bow constructor.
*
* @param Setting $setting
*/
public function __construct(Setting $setting)
{
$this->arg = new Argument();
if ($this->arg->hasTrash()) {
$this->throwFailsCommand('Bad command usage', 'help');
}
$this->setting = $setting;
$this->command = new Command($setting, $this->arg);
static::$instance = $this;
}
/**
* Get the console instance
*
* @return ?Console
* @throws ConsoleException
*/
public static function getInstance(): ?Console
{
if (is_null(static::$instance)) {
throw new ConsoleException("The console is not instantiated");
}
return static::$instance;
}
/**
* Add a custom order to the store from the web env
* This method work on web and cli env
*
* @param string $command
* @param callable|string $cb
* @return void
*/
public static function register(string $command, callable|string $cb): void
{
static::$registers[$command] = $cb;
}
/**
* Bind kernel
*
* @param Loader $kernel
* @return void
*/
public function bind(Loader $kernel): void
{
$this->kernel = $kernel;
}
/**
* Launch Bow task runner
*
* @throws
*/
public function run()
{
if ($this->booted) {
exit(0);
}
// Boot kernel and console
$this->kernel->withoutSession();
try {
$this->kernel->boot();
} catch (Exception $exception) {
echo Color::red($exception->getMessage());
echo Color::green($exception->getTraceAsString());
exit(1);
}
$this->booted = true;
// Run all bootstraps files
foreach ($this->setting->getBootstrap() as $item) {
include $item;
}
// Get the argument command
$command = $this->arg->getCommand();
if ($command == 'run') {
$command = 'launch';
}
try {
$this->call($command);
exit(0);
} catch (Exception $exception) {
echo Color::red($exception->getMessage());
echo Color::green($exception->getTraceAsString());
exit(1);
}
}
/**
* Calls a command
*
* @param string|null $command
* @return mixed
* @throws ErrorException
* @throws Exception
*/
public function call(?string $command): mixed
{
// Display of the help menu if no command defined.
if (!isset($command)) {
$this->help();
exit(0);
}
// The built-in commands have priority
$commands = $this->command->getCommands();
if (!in_array($command, array_keys($commands))) {
// Try to execute the custom command
if (array_key_exists($this->arg->getRawCommand(), static::$registers) || array_key_exists($command, static::$registers)) {
return $this->executeCustomCommand($this->arg->getRawCommand() ?? $command);
}
}
if (!in_array($command, static::COMMAND)) {
$this->throwFailsCommand("The command '$command' not exists.", 'help');
}
$target = $this->arg->getTarget();
if (!$this->arg->getAction()) {
if ($target == 'help') {
return $this->help($command);
}
}
try {
return call_user_func_array([$this, $command], [$this->arg->getRawCommand()]);
} catch (Exception $e) {
echo Color::red(sprintf("$command command failed with: %s\n", $e->getMessage()));
exit(1);
}
}
/**
* Execute the define custom command
*
* @param string $command
* @return mixed
* @throws Exception
*/
private function executeCustomCommand(string $command): mixed
{
try {
$classname = static::$registers[$command];
if (is_callable($classname)) {
return $classname($this->arg, $this->setting);
}
// Create the command instance
$instance = new $classname($this->setting, $this->arg);
return call_user_func_array([$instance, "process"], []);
} catch (Exception $exception) {
if (php_sapi_name() !== "cli") {
throw $exception;
}
echo Color::red($exception->getMessage());
echo Color::green($exception->getTraceAsString());
exit(1);
}
}
/**
* Add a custom order to the store
* The method work only on cli env
*
* @param string $command
* @param callable|string $cb
* @return Console
*/
public function addCommand(string $command, callable|string $cb): Console
{
static::$registers[$command] = $cb;
return $this;
}
/**
* Launch a migration
*
* @return void
* @throws ErrorException
*/
private function migration(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['migrate', 'rollback', 'reset'])) {
$this->throwFailsCommand('This action is not exists!', 'help migration');
}
$this->command->call("migration:{$action}", $action, $action);
}
/**
* Launch a migration
*
* @return void
* @throws ErrorException
*/
private function migrate(): void
{
$action = $this->arg->getAction();
if (!is_null($action)) {
$this->throwFailsCommand('This action is not allow!', 'help migration');
}
$this->command->call('migration:migrate', 'migrate', null);
}
/**
* Create files
*
* @return void
* @throws ErrorException
*/
private function add(): void
{
$action = $this->arg->getAction();
if (!in_array($action, static::ADD_ACTION)) {
$this->throwFailsCommand('This action is not exists', 'help add');
}
$target = $this->arg->getTarget();
if (is_null($target)) {
$this->throwFailsCommand('Please provide the filename', 'help add');
}
$this->command->call("add:{$action}", $action, $target);
}
/**
* Launch seeding
*
* @return void
* @throws
*/
private function seed(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['all', 'file'])) {
$this->throwFailsCommand('This action is not exists', 'help seed');
}
$target = $this->arg->getTarget();
if ($action == 'all') {
if ($target != null) {
$this->throwFailsCommand(
'Bad command usage target is not allow in this case',
'help seed'
);
}
}
$this->command->call("seed:{$action}", $action, $target);
}
/**
* Launch process
*
* @throws ErrorException
*/
private function launch(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['server', 'console', 'worker'])) {
$this->throwFailsCommand('Bad command usage', 'help run');
}
$this->command->call("run:{$action}", $action, $this->arg->getTarget());
}
/**
* Alias of run:server
*
* @return void
* @throws ErrorException
*/
private function serve(): void
{
$this->command->call("run:server", 'server', $this->arg->getTarget());
}
/**
* Alias of generate
*
* @return void
* @throws ErrorException
*/
private function gen(): void
{
$this->generate();
}
/**
* Allows to generate a resource on a controller
*
* @return void
* @throws ErrorException
*/
private function generate(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['key', 'resource', 'notification-table', 'session-table', 'cache-table', 'queue-table'])) {
$this->throwFailsCommand('This action is not exists', 'help generate');
}
$this->command->call("generate:{$action}", $action, $this->arg->getTarget());
}
/**
* Remove the caches
*
* @return void
* @throws ErrorException
*/
private function clear(): void
{
$action = $this->arg->getAction();
$this->command->call('clear', $action, $action);
}
/**
* Flush the connections
*
* @return void
* @throws ErrorException
*/
private function flush(): void
{
$action = $this->arg->getAction();
if ($action != 'worker') {
$this->throwFailsCommand('This action is not exists', 'help flush');
}
$this->command->call('flush:worker', $action);
}
/**
* Show bow framework version and current php version in console
*
* @return void
*/
private function getVersion(): void
{
$version = <<<USAGE
Console running for Bow Framework: \033[0;32m%s\033[00m - PHP Version: \033[0;32m%s\033[0;33m
USAGE;
echo sprintf($version, Console::VERSION, PHP_VERSION);
}
/**
* Display global help or helper command.
*
* @param string|null $command
* @return int
*/
private function help(?string $command = null): int
{
// Display the framework and php version
$this->getVersion();
if ($command === null || $command == 'help') {
$usage = <<<USAGE
Bow task runner usage: php bow command:action [name] --option
\033[0;32mCOMMAND\033[00m:
\033[0;33mhelp\033[00m Display command helper
\033[0;32mGENERATE\033[00m Create new app key and resources
\033[0;33mgenerate:resource\033[00m Create new REST controller
\033[0;33mgenerate:session-table\033[00m Generate preset table for session
\033[0;33mgenerate:cache-table\033[00m Generate preset table for cache
\033[0;33mgenerate:queue-table\033[00m Generate preset table for queue
\033[0;33mgenerate:notification-table\033[00m Generate preset table for notification
\033[0;33mgenerate:key\033[00m Create new app key
\033[0;33mflush:worker\033[00m Flush all queues
\033[0;32mADD\033[00m Create a user class
\033[0;33madd:middleware\033[00m Create new middleware
\033[0;33madd:configuration\033[00m Create new configuration
\033[0;33madd:service\033[00m Create new service
\033[0;33madd:exception\033[00m Create new exception
\033[0;33madd:controller\033[00m Create new controller
\033[0;33madd:model\033[00m Create new model
\033[0;33madd:validation\033[00m Create new validation
\033[0;33madd:seeder\033[00m Create new table fake seeder
\033[0;33madd:migration\033[00m Create a new migration
\033[0;33madd:event\033[00m Create a new event
\033[0;33madd:listener\033[00m Create a new event listener
\033[0;33madd:job\033[00m Create a new job
\033[0;33madd:command\033[00m Create a new console command
\033[0;33madd:message\033[00m Create a new messaging handler
\033[0;32mMIGRATION\033[00m Apply migration to database
\033[0;33mmigration:migrate\033[00m Run migrations
\033[0;33mmigration:reset\033[00m Reset all migrations
\033[0;33mmigration:rollback\033[00m Rollback to previous migration
\033[0;33mmigrate\033[00m Alias of \033[0;33mmigration:migrate\033[00m
\033[0;32mCLEAR\033[00m Clear cache information
\033[0;33mclear:view\033[00m Clear view cached files
\033[0;33mclear:cache\033[00m Clear cache files
\033[0;33mclear:session\033[00m Clear session cache files
\033[0;33mclear:log\033[00m Clear log files
\033[0;33mclear:all\033[00m Clear all cache files
\033[0;32mSEED\033[00m Run database seeders
\033[0;33mseed:file\033[00m [class_name] Run specific seeder file
\033[0;33mseed:all\033[00m Run all seeders
\033[0;32mRUN\033[00m Launch development tools
\033[0;33mrun:console\033[00m Show PsySH PHP REPL for debugging code
\033[0;33mrun:server\033[00m Start local development server
\033[0;33mrun:worker\033[00m Start consumer/worker to handle queue jobs
USAGE;
echo $usage;
return 0;
}
switch ($command) {
case 'add':
echo <<<U
\n\033[0;32mcreate\033[00m create a user class\n
[option]
--no-plain Create a plain controller [available in add:controller]
-m Create a migration [available in add:model]
--create Create a migration for create table [available in add:migration]
--table Create a migration for alter table [available in add:migration]
* you can use --no-plain --with-model in same command
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:controller name [option] Create a new controller
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:middleware name Create a new middleware
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:configuration name Create a new configuration
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:service name Create a new service
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:exception name Create a new exception
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:model name [option] Create a new model
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:validation name Create a new validation
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:seeder name [--seed=n] Create a new seeder
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:migration name Create a new migration
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:event name Create a new event listener
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:job name Create a new queue job
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:command name Create a new console command
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:message name Create a new messaging handler
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add help Display this help
U;
break;
case 'generate':
case 'gen':
echo <<<U
\n\033[0;32mgenerate\033[00m create a resource and app key
[option]
--model=[model_name] Define the usable model
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:resource name [option] Create a new REST controller
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:session-table Generate the table for session
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:cache-table Generate the table for cache
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:queue-table Generate the table for queue
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:notification-table Generate the table for notification
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:key Generate a new APP KEY
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate help Display this help
\033[0;33mgen\033[00m Alias of \033[0;33mgenerate\033[00m
U;
break;
case 'migration':
echo <<<U
\n\033[0;32mmigration\033[00m apply a migration in user model\n
\033[0;33m$\033[00m php \033[0;34mbow\033[00m migration:migrate Run migrations
\033[0;33m$\033[00m php \033[0;34mbow\033[00m migration:reset Reset all migrations
\033[0;33m$\033[00m php \033[0;34mbow\033[00m migration:rollback Rollback to previous migration
\033[0;33m$\033[00m php \033[0;34mbow\033[00m migrate Alias of \033[0;33mmigration:migrate\033[00m
\033[0;33m$\033[00m php \033[0;34mbow\033[00m migration help Display this help
U;
break;
case 'run': // phpcs:disable
echo <<<U
\n\033[0;32mrun\033[00m for launch repl and local server\n
[option]
run:server [--port=8080] [--host=localhost] [--php-settings="display_errors=on"]
run:console [--include=filename.php] [--prompt=prompt_name]
run:worker [--queue=default] [--connexion=beanstalkd,sqs,redis,database] [--tries=duration] [--sleep=duration] [--timeout=duration]
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:console Show PsySH PHP REPL for debugging code
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:server [option] Start local development server
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:worker [option] Start worker to handle queue jobs
U; // phpcs:enable
break;
case 'clear':
echo <<<U
\n\033[0;32mclear\033[00m for clear cache information\n
\033[0;33m$\033[00m php \033[0;34mbow\033[00m clear:view Clear view cached information
\033[0;33m$\033[00m php \033[0;34mbow\033[00m clear:cache\033[00m Clear cache information
\033[0;33m$\033[00m php \033[0;34mbow\033[00m clear:all\033[00m Clear all cache information
U;
break;
case 'seed':
echo <<<U
\n\033[0;32mMake table seeding\033[00m\n
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:all\033[00m Make seeding for all
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:file\033[00m class_name Make seeding for one file
U;
break;
case 'flush':
echo <<<U
\n\033[0;32mFlush all queues content\033[00m\n
[option]
flush:worker [connection] [--queue=queue_name]
\033[0;33m$\033[00m php \033[0;34mbow\033[00m flush:worker\033[00m Flush all queues
U;
break;
default:
$this->throwFailsCommand("Please make php bow help for show whole docs !");
exit(1);
}
exit(0);
}
}