-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathConsole.php
652 lines (539 loc) · 18.4 KB
/
Console.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
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
<?php
declare(strict_types=1);
namespace Bow\Console;
use Bow\Configuration\Loader;
use Bow\Console\Exception\ConsoleException;
use Bow\Console\Traits\ConsoleTrait;
use Psy\Exception\FatalErrorException;
use RuntimeException;
/**
* @method static Console addCommand(string $command, callable $cb)
*/
class Console
{
use ConsoleTrait;
/**
* Define Bow Framework version.
*
* @var string
*/
private const VERSION = '5.0';
/**
* The Setting instance
*
* @var Setting
*/
private Setting $setting;
/**
* The COMMAND instance
*
* @var Command
*/
private Command $command;
/**
* The Loader instance
*
* @var Loader
*/
private Loader $kernel;
/**
* The custom command registers
*
* @var array
*/
private static array $registers = [];
/**
* Defines if console booted
*
* @var bool
*/
private bool $booted = false;
/**
* The Argument instance
*
* @return Argument
*/
private Argument $arg;
/**
* The console instance
*
* @var Console
*/
private static ?Console $instance = null;
/**
* The command list
*
* @var array
*/
private const COMMAND = [
'add', 'migration', 'migrate', 'run', 'generate', 'gen', 'seed', 'help', 'launch', 'clear', 'flush'
];
/**
* The action list
*
* @var array
*/
private const ADD_ACTION = [
'middleware', 'controller', 'model', 'validation',
'seeder', 'migration', 'configuration', 'service',
'exception', 'event', 'producer', 'command', 'listener'
];
/**
* Bow constructor.
*
* @param Setting $setting
*
* @return void
*/
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;
}
/**
* Bind kernel
*
* @param Loader $kernel
* @return void
*/
public function bind(Loader $kernel): void
{
$this->kernel = $kernel;
}
/**
* Launch Bow task runner
*
* @return mixed
* @throws
*/
public function run(): mixed
{
if ($this->booted) {
return false;
}
// 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) {
require $item;
}
// Get the argument command
$command = $this->arg->getCommand();
// Renaming the incomming command
if ($command == 'launch') {
$command = null;
}
if ($command == 'run') {
$command = 'launch';
}
try {
return $this->call($command);
} catch (\Exception $exception) {
echo Color::red($exception->getMessage());
echo Color::green($exception->getTraceAsString());
exit(1);
}
}
/**
* Calls a command
*
* @param string $command
* @return mixed
* @throws
*/
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
if (!in_array($command, static::COMMAND)) {
// Try to execute the custom command
if (array_key_exists($command, static::$registers)) {
return $this->executeCustomCommand($command);
}
$this->throwFailsCommand("The command '$command' not exists.", 'help');
}
$target = $this->arg->getTarget();
if (!$this->arg->getAction()) {
if ($target == 'help') {
$this->help($command);
exit(0);
}
}
try {
return call_user_func_array([$this, $command], [$target]);
} catch (\Exception $e) {
echo $e->getMessage();
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;
}
/**
* 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;
}
/**
* Execute the define custom command
*
* @param string $command
* @return mixed
*/
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);
}
}
/**
* 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');
}
$target = $this->arg->getTarget();
$this->command->call('migration', $action, $target);
}
/**
* 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', 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, $target);
}
/**
* Launch seeding
*
* @return void
* @throws
*/
private function seed(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['all', 'table'])) {
$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('seeder', $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('runner', $action, $this->arg->getTarget());
}
/**
* Allows generate a resource on a controller
*
* @return void
*/
private function generate(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['key', 'resource', 'session', 'cache'])) {
$this->throwFailsCommand('This action is not exists', 'help generate');
}
$this->command->call('generator', $action, $this->arg->getTarget());
}
/**
* Alias of generate
*
* @return void
*/
private function gen(): void
{
$this->generate();
}
/**
* Remove the caches
*
* @return void
* @throws \ErrorException
*/
private function clear(): void
{
$action = $this->arg->getAction();
$this->command->call('clear', "make", $action);
}
/**
* Flush the connections
*
* @return void
* @throws \ErrorException
*/
private function flush(): void
{
$action = $this->arg->getAction();
if (!in_array($action, ['worker'])) {
$this->throwFailsCommand('This action is not exists', 'help flush');
}
$this->command->call('flush', $action);
}
/**
* 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) {
$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 a new app key and resources
\033[0;33mgenerate:resource\033[00m Create new REST controller
\033[0;33mgenerate:table\033[00m For generate the preset table for session, cache, queue
\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:producer\033[00m Create a new producer
\033[0;33madd:command\033[00m Create a new bow console command
\033[0;32mMIGRATION\033[00m apply a migration in user model
\033[0;33mmigration:migrate\033[00m Make migration
\033[0;33mmigration:reset\033[00m Reset all migration
\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 for clear cache information
\033[0;33mclear:view\033[00m Clear view cached information
\033[0;33mclear:cache\033[00m Clear cache information
\033[0;33mclear:session\033[00m Clear session cache information
\033[0;33mclear:log\033[00m Clear logs cache information
\033[0;33mclear:all\033[00m Clear all cache information
\033[0;32mSEED\033[00m Make seeding
\033[0;33mseed:table\033[00m [name] Make seeding for one table
\033[0;33mseed:all\033[00m Make seeding for all
\033[0;32mRUN\033[00m Launch process
\033[0;33mrun:console\033[00m show psysh php REPL for debug you code.
\033[0;33mrun:server\033[00m run a local web server.
\033[0;33mrun:worker\033[00m run a consumer/worker for handle queue.
USAGE;
echo $usage;
return 0;
}
switch ($command) {
case 'help':
echo "\033[0;33mhelp\033[00m display command helper\n";
break;
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] For create a new controlleur
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:middleware name For create a new middleware
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:configuration name For create a new configuration
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:service name For create a new service
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:exception name For create a new exception
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:model name [option] For create a new model
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:validation name For create a new validation
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:seeder name [--seed=n] For create a new seeder
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:migration name For create a new migration
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:event name For create a new event listener
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:producer name For create a new queue producer
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:command name For create a new bow console command
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add help For display this
U;
break;
case 'generate':
case 'gen':
case 'generator':
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] For create a new REST controller
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:table For generate the table for session, cache, queue
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate:key For generate a new APP KEY
\033[0;33m$\033[00m php \033[0;34mbow\033[00m generate help For display this
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 Make migration
\033[0;33m$\033[00m php \033[0;34mbow\033[00m migration:reset Reset all migration
\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 For display this
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=5000] [--host=localhost] [--php-settings="display_errors=on"]
run:console [--include=filename.php] [--prompt=prompt_name]
run:worker [--queue=default] [--connexion=beanstalkd,sqs] [--retry-after=duration]
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:console\033[00m Show psysh php REPL
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:server\033[00m [option] Start local developpement server
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:worker\033[00m [option] Start worker/consumer for handle the producer
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:table\033[00m table_name Make seeding for one table
U;
break;
case 'flush':
echo <<<U
\n\033[0;32mMFlush 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);
}
/*
* Show bow framework version and current php version in console
*
* @return string
*/
private function getVersion()
{
$version = <<<USAGE
\033[0;33mConsole running for \033[00mBow Framework: \033[0;32m%s\033[00m - PHP Version: \033[0;32m%s\033[0;33m
USAGE;
echo sprintf($version, Console::VERSION, PHP_VERSION);
}
}