1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Epesi\Core\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Artisan; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class EpesiCommand extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'epesi'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'List all epesi commands'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public static $logo = <<<LOGO |
29
|
|
|
____ ____ ____ ____ __ |
30
|
|
|
( __)( _ \( __)/ ___)( ) |
31
|
|
|
) _) ) __/ ) _) \___ \ )( |
32
|
|
|
(____)(__) (____)(____/(__) |
33
|
|
|
|
34
|
|
|
LOGO; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute the console command. |
38
|
|
|
*/ |
39
|
|
|
public function handle() |
40
|
|
|
{ |
41
|
|
|
$this->line(static::$logo); |
42
|
|
|
|
43
|
|
|
$this->comment(''); |
44
|
|
|
$this->comment('Available commands:'); |
45
|
|
|
|
46
|
|
|
$this->listEpesiCommands(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* List all epesi commands. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
protected function listEpesiCommands() |
55
|
|
|
{ |
56
|
|
|
$commands = collect(Artisan::all())->mapWithKeys(function ($command, $key) { |
57
|
|
|
if (Str::startsWith($key, 'epesi:')) { |
58
|
|
|
return [$key => $command]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return []; |
62
|
|
|
})->toArray(); |
63
|
|
|
|
64
|
|
|
$width = $this->getColumnWidth($commands); |
65
|
|
|
|
66
|
|
|
/** @var Command $command */ |
67
|
|
|
foreach ($commands as $command) { |
68
|
|
|
$this->line(sprintf(" %-{$width}s %s", $command->getName(), $command->getDescription())); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param (Command|string)[] $commands |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
|
|
private function getColumnWidth(array $commands) |
78
|
|
|
{ |
79
|
|
|
$widths = []; |
80
|
|
|
|
81
|
|
|
foreach ($commands as $command) { |
82
|
|
|
$widths[] = static::strlen($command->getName()); |
83
|
|
|
foreach ($command->getAliases() as $alias) { |
84
|
|
|
$widths[] = static::strlen($alias); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $widths ? max($widths) + 2 : 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the length of a string, using mb_strwidth if it is available. |
93
|
|
|
* |
94
|
|
|
* @param string $string The string to check its length |
95
|
|
|
* |
96
|
|
|
* @return int The length of the string |
97
|
|
|
*/ |
98
|
|
|
public static function strlen($string) |
99
|
|
|
{ |
100
|
|
|
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
101
|
|
|
return strlen($string); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return mb_strwidth($string, $encoding); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|