Completed
Push — master ( 5ca9ad...782d0f )
by Song
03:18
created

AdminCommand::strlen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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