Completed
Push — master ( 178356...89490d )
by Jitendra
10s
created

OutputHelper::showHelp()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 10
nop 4
dl 0
loc 23
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHP-CLI package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Cli\Helper;
13
14
use Ahc\Cli\Exception;
15
use Ahc\Cli\Input\Argument;
16
use Ahc\Cli\Input\Command;
17
use Ahc\Cli\Input\Option;
18
use Ahc\Cli\Input\Parameter;
19
use Ahc\Cli\Output\Writer;
20
21
/**
22
 * This helper helps you by showing you help information :).
23
 *
24
 * @author  Jitendra Adhikari <[email protected]>
25
 * @license MIT
26
 *
27
 * @link    https://github.com/adhocore/cli
28
 */
29
class OutputHelper
30
{
31
    /** @var Writer */
32
    protected $writer;
33
34
    /** @var int Max width of command name */
35
    protected $maxCmdName;
36
37
    public function __construct(Writer $writer = null)
38
    {
39
        $this->writer = $writer ?? new Writer;
40
    }
41
42
    /**
43
     * Print stack trace and error msg of an exception.
44
     *
45
     * @param \Throwable $e
46
     *
47
     * @return void
48
     */
49
    public function printTrace(\Throwable $e)
50
    {
51
        $eClass = \get_class($e);
52
53
        $this->writer->colors(
54
            "{$eClass} <red>{$e->getMessage()}</end><eol/>" .
55
            "(thrown in <yellow>{$e->getFile()}</end><white>:{$e->getLine()})</end>"
56
        );
57
58
        // @codeCoverageIgnoreStart
59
        if ($e instanceof Exception) {
60
            // Internal exception traces are not printed.
61
            return;
62
        }
63
        // @codeCoverageIgnoreEnd
64
65
        $traceStr = '<eol/><eol/><bold>Stack Trace:</end><eol/><eol/>';
66
67
        foreach ($e->getTrace() as $i => $trace) {
68
            $trace += ['class' => '', 'type' => '', 'function' => '', 'file' => '', 'line' => '', 'args' => []];
69
            $symbol = $trace['class'] . $trace['type'] . $trace['function'];
70
            $args   = $this->stringifyArgs($trace['args']);
71
72
            $traceStr .= "  <comment>$i)</end> <red>$symbol</end><comment>($args)</end>";
73
            if ('' !== $trace['file']) {
74
                $file      = \realpath($trace['file']);
75
                $traceStr .= "<eol/>     <yellow>at $file</end><white>:{$trace['line']}</end><eol/>";
76
            }
77
        }
78
79
        $this->writer->colors($traceStr);
80
    }
81
82
    protected function stringifyArgs(array $args)
83
    {
84
        $holder = [];
85
86
        foreach ($args as $arg) {
87
            $holder[] = $this->stringifyArg($arg);
88
        }
89
90
        return \implode(', ', $holder);
91
    }
92
93
    protected function stringifyArg($arg)
94
    {
95
        if (\is_scalar($arg)) {
96
            return \var_export($arg, true);
97
        }
98
99
        if (\is_object($arg)) {
100
            return \method_exists($arg, '__toString') ? (string) $arg : \get_class($arg);
101
        }
102
103
        if (\is_array($arg)) {
104
            return '[' . $this->stringifyArgs($arg) . ']';
105
        }
106
107
        return \gettype($arg);
108
    }
109
110
    /**
111
     * @param Argument[] $arguments
112
     * @param string     $header
113
     * @param string     $footer
114
     *
115
     * @return self
116
     */
117
    public function showArgumentsHelp(array $arguments, string $header = '', string $footer = ''): self
118
    {
119
        $this->showHelp('Arguments', $arguments, $header, $footer);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param Option[] $options
126
     * @param string   $header
127
     * @param string   $footer
128
     *
129
     * @return self
130
     */
131
    public function showOptionsHelp(array $options, string $header = '', string $footer = ''): self
132
    {
133
        $this->showHelp('Options', $options, $header, $footer);
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param Command[] $commands
140
     * @param string    $header
141
     * @param string    $footer
142
     *
143
     * @return self
144
     */
145
    public function showCommandsHelp(array $commands, string $header = '', string $footer = ''): self
146
    {
147
        $this->maxCmdName = $commands ? \max(\array_map(function (Command $cmd) {
148
            return \strlen($cmd->name());
149
        }, $commands)) : 0;
150
151
        $this->showHelp('Commands', $commands, $header, $footer);
152
153
        return $this;
154
    }
155
156
    /**
157
     * Show help with headers and footers.
158
     *
159
     * @param string $for
160
     * @param array  $items
161
     * @param string $header
162
     * @param string $footer
163
     *
164
     * @return void
165
     */
166
    protected function showHelp(string $for, array $items, string $header = '', string $footer = '')
167
    {
168
        if ($header) {
169
            $this->writer->bold($header, true);
170
        }
171
172
        $this->writer->eol()->boldGreen($for . ':', true);
173
174
        if (empty($items)) {
175
            $this->writer->bold('  (n/a)', true);
176
177
            return;
178
        }
179
180
        $space = 4;
181
        foreach ($this->sortItems($items, $padLen) as $item) {
182
            $name = $this->getName($item);
183
            $this->writer->bold('  ' . \str_pad($name, $padLen + $space));
184
            $this->writer->comment($item->desc(), true);
185
        }
186
187
        if ($footer) {
188
            $this->writer->eol()->yellow($footer, true);
189
        }
190
    }
191
192
    /**
193
     * Sort items by name. As a side effect sets max length of all names.
194
     *
195
     * @param Parameter[]|Command[] $items
196
     * @param int                   $max
197
     *
198
     * @return array
199
     */
200
    protected function sortItems(array $items, &$max = 0): array
201
    {
202
        $max = \max(\array_map(function ($item) {
203
            return \strlen($this->getName($item));
204
        }, $items));
205
206
        \uasort($items, function ($a, $b) {
207
            /* @var Parameter $b */
208
            /* @var Parameter $a */
209
            return $a->name() <=> $b->name();
210
        });
211
212
        return $items;
213
    }
214
215
    /**
216
     * Prepare name for different items.
217
     *
218
     * @param Parameter|Command $item
219
     *
220
     * @return string
221
     */
222
    protected function getName($item): string
223
    {
224
        $name = $item->name();
225
226
        if ($item instanceof Command) {
227
            return \trim(\str_pad($name, $this->maxCmdName) . ' ' . $item->alias());
228
        }
229
230
        return $this->label($item);
231
    }
232
233
    /**
234
     * Get parameter label for humans.
235
     *
236
     * @param Parameter $item
237
     *
238
     * @return string
239
     */
240
    protected function label(Parameter $item)
241
    {
242
        $name = $item->name();
243
244
        if ($item instanceof Option) {
245
            $name = $item->short() . '|' . $item->long();
246
        }
247
248
        $variad = $item->variadic() ? '...' : '';
249
250
        if ($item->required()) {
251
            return "<$name$variad>";
252
        }
253
254
        return "[$name$variad]";
255
    }
256
}
257