Passed
Push — 5.2 ( 34572a...8f25db )
by
unknown
02:59
created

Command::getApp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\console;
14
15
use think\App;
16
use think\Console;
17
use think\console\input\Argument;
18
use think\console\input\Definition;
19
use think\console\input\Option;
20
21
class Command
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
22
{
23
24
    /** @var  Console */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
25
    private $console;
0 ignored issues
show
Coding Style introduced by
Private member variable "console" must be prefixed with an underscore
Loading history...
26
    private $name;
0 ignored issues
show
Coding Style introduced by
Private member variable "name" must be prefixed with an underscore
Loading history...
27
    private $aliases                         = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "aliases" must be prefixed with an underscore
Loading history...
28
    private $definition;
0 ignored issues
show
Coding Style introduced by
Private member variable "definition" must be prefixed with an underscore
Loading history...
29
    private $help;
0 ignored issues
show
Coding Style introduced by
Private member variable "help" must be prefixed with an underscore
Loading history...
30
    private $description;
0 ignored issues
show
Coding Style introduced by
Private member variable "description" must be prefixed with an underscore
Loading history...
31
    private $ignoreValidationErrors          = false;
0 ignored issues
show
Coding Style introduced by
Private member variable "ignoreValidationErrors" must be prefixed with an underscore
Loading history...
32
    private $consoleDefinitionMerged         = false;
0 ignored issues
show
Coding Style introduced by
Private member variable "consoleDefinitionMerged" must be prefixed with an underscore
Loading history...
33
    private $consoleDefinitionMergedWithArgs = false;
0 ignored issues
show
Coding Style introduced by
Private member variable "consoleDefinitionMergedWithArgs" must be prefixed with an underscore
Loading history...
34
    private $code;
0 ignored issues
show
Coding Style introduced by
Private member variable "code" must be prefixed with an underscore
Loading history...
35
    private $synopsis                        = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "synopsis" must be prefixed with an underscore
Loading history...
36
    private $usages                          = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "usages" must be prefixed with an underscore
Loading history...
37
38
    /** @var  Input */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
39
    protected $input;
40
41
    /** @var  Output */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
42
    protected $output;
43
44
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
45
    protected $app;
46
47
    /**
48
     * 构造方法
49
     * @param string|null $name 命令名称,如果没有设置则比如在 configure() 里设置
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
50
     * @throws \LogicException
51
     * @api
52
     */
53
    public function __construct($name = null)
54
    {
55
        $this->definition = new Definition();
56
57
        if (null !== $name) {
58
            $this->setName($name);
59
        }
60
61
        $this->configure();
62
63
        if (!$this->name) {
64
            throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
65
        }
66
    }
67
68
    /**
69
     * 忽略验证错误
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71
    public function ignoreValidationErrors(): void
72
    {
73
        $this->ignoreValidationErrors = true;
74
    }
75
76
    /**
77
     * 设置控制台
78
     * @param Console $console
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
80
    public function setConsole(Console $console = null): void
81
    {
82
        $this->console = $console;
83
    }
84
85
    /**
86
     * 获取控制台
87
     * @return Console
88
     * @api
89
     */
90
    public function getConsole(): Console
91
    {
92
        return $this->console;
93
    }
94
95
    /**
96
     * 设置app
97
     * @param App $app
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function setApp(App $app)
100
    {
101
        $this->app = $app;
102
    }
103
104
    /**
105
     * 获取app
106
     * @return App
107
     */
108
    public function getApp()
109
    {
110
        return $this->app;
111
    }
112
113
    /**
114
     * 是否有效
115
     * @return bool
116
     */
117
    public function isEnabled(): bool
118
    {
119
        return true;
120
    }
121
122
    /**
123
     * 配置指令
124
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
125
    protected function configure()
126
    {
127
    }
128
129
    /**
130
     * 执行指令
131
     * @param Input  $input
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
132
     * @param Output $output
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
133
     * @return null|int
134
     * @throws \LogicException
135
     * @see setCode()
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
136
     */
137
    protected function execute(Input $input, Output $output)
138
    {
139
        return $this->app->invoke([$this, 'handle']);
140
    }
141
142
    /**
143
     * 用户验证
144
     * @param Input  $input
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
145
     * @param Output $output
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
146
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
147
    protected function interact(Input $input, Output $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
    protected function interact(Input $input, /** @scrutinizer ignore-unused */ Output $output)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $input is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
    protected function interact(/** @scrutinizer ignore-unused */ Input $input, Output $output)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
    }
150
151
    /**
152
     * 初始化
153
     * @param Input  $input  An InputInterface instance
154
     * @param Output $output An OutputInterface instance
155
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
156
    protected function initialize(Input $input, Output $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
    protected function initialize(Input $input, /** @scrutinizer ignore-unused */ Output $output)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $input is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
    protected function initialize(/** @scrutinizer ignore-unused */ Input $input, Output $output)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158
    }
159
160
    /**
161
     * 执行
162
     * @param Input  $input
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
163
     * @param Output $output
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
164
     * @return int
165
     * @throws \Exception
166
     * @see setCode()
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
167
     * @see execute()
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
168
     */
169
    public function run(Input $input, Output $output): int
170
    {
171
        $this->input  = $input;
172
        $this->output = $output;
173
174
        $this->getSynopsis(true);
175
        $this->getSynopsis(false);
176
177
        $this->mergeConsoleDefinition();
178
179
        try {
180
            $input->bind($this->definition);
181
        } catch (\Exception $e) {
182
            if (!$this->ignoreValidationErrors) {
183
                throw $e;
184
            }
185
        }
186
187
        $this->initialize($input, $output);
188
189
        if ($input->isInteractive()) {
190
            $this->interact($input, $output);
191
        }
192
193
        $input->validate();
194
195
        if ($this->code) {
196
            $statusCode = call_user_func($this->code, $input, $output);
197
        } else {
198
            $statusCode = $this->execute($input, $output);
199
        }
200
201
        return is_numeric($statusCode) ? (int) $statusCode : 0;
202
    }
203
204
    /**
205
     * 设置执行代码
206
     * @param callable $code callable(InputInterface $input, OutputInterface $output)
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
207
     * @return Command
208
     * @throws \InvalidArgumentException
209
     * @see execute()
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
210
     */
211
    public function setCode(callable $code)
212
    {
213
        if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
214
            $r = new \ReflectionFunction($code);
215
            if (null === $r->getClosureThis()) {
216
                $code = \Closure::bind($code, $this);
217
            }
218
        }
219
220
        $this->code = $code;
221
222
        return $this;
223
    }
224
225
    /**
226
     * 合并参数定义
227
     * @param bool $mergeArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
228
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
229
    public function mergeConsoleDefinition(bool $mergeArgs = true)
230
    {
231
        if (null === $this->console
232
            || (true === $this->consoleDefinitionMerged
233
                && ($this->consoleDefinitionMergedWithArgs || !$mergeArgs))
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
234
        ) {
235
            return;
236
        }
237
238
        if ($mergeArgs) {
239
            $currentArguments = $this->definition->getArguments();
240
            $this->definition->setArguments($this->console->getDefinition()->getArguments());
241
            $this->definition->addArguments($currentArguments);
242
        }
243
244
        $this->definition->addOptions($this->console->getDefinition()->getOptions());
245
246
        $this->consoleDefinitionMerged = true;
247
        if ($mergeArgs) {
248
            $this->consoleDefinitionMergedWithArgs = true;
249
        }
250
    }
251
252
    /**
253
     * 设置参数定义
254
     * @param array|Definition $definition
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
255
     * @return Command
256
     * @api
257
     */
258
    public function setDefinition($definition)
259
    {
260
        if ($definition instanceof Definition) {
261
            $this->definition = $definition;
262
        } else {
263
            $this->definition->setDefinition($definition);
264
        }
265
266
        $this->consoleDefinitionMerged = false;
267
268
        return $this;
269
    }
270
271
    /**
272
     * 获取参数定义
273
     * @return Definition
274
     * @api
275
     */
276
    public function getDefinition(): Definition
277
    {
278
        return $this->definition;
279
    }
280
281
    /**
282
     * 获取当前指令的参数定义
283
     * @return Definition
284
     */
285
    public function getNativeDefinition(): Definition
286
    {
287
        return $this->getDefinition();
288
    }
289
290
    /**
291
     * 添加参数
292
     * @param string $name        名称
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
293
     * @param int    $mode        类型
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
294
     * @param string $description 描述
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
295
     * @param mixed  $default     默认值
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
296
     * @return Command
297
     */
298
    public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
299
    {
300
        $this->definition->addArgument(new Argument($name, $mode, $description, $default));
301
302
        return $this;
303
    }
304
305
    /**
306
     * 添加选项
307
     * @param string $name        选项名称
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
308
     * @param string $shortcut    别名
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
309
     * @param int    $mode        类型
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
310
     * @param string $description 描述
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
311
     * @param mixed  $default     默认值
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
312
     * @return Command
313
     */
314
    public function addOption(string $name, string $shortcut = null, int $mode = null, string $description = '', $default = null)
315
    {
316
        $this->definition->addOption(new Option($name, $shortcut, $mode, $description, $default));
317
318
        return $this;
319
    }
320
321
    /**
322
     * 设置指令名称
323
     * @param string $name
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
324
     * @return Command
325
     * @throws \InvalidArgumentException
326
     */
327
    public function setName(string $name)
328
    {
329
        $this->validateName($name);
330
331
        $this->name = $name;
332
333
        return $this;
334
    }
335
336
    /**
337
     * 获取指令名称
338
     * @return string
339
     */
340
    public function getName(): string
341
    {
342
        return $this->name ?: '';
343
    }
344
345
    /**
346
     * 设置描述
347
     * @param string $description
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
348
     * @return Command
349
     */
350
    public function setDescription(string $description)
351
    {
352
        $this->description = $description;
353
354
        return $this;
355
    }
356
357
    /**
358
     *  获取描述
359
     * @return string
360
     */
361
    public function getDescription(): string
362
    {
363
        return $this->description ?: '';
364
    }
365
366
    /**
367
     * 设置帮助信息
368
     * @param string $help
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
369
     * @return Command
370
     */
371
    public function setHelp(string $help)
372
    {
373
        $this->help = $help;
374
375
        return $this;
376
    }
377
378
    /**
379
     * 获取帮助信息
380
     * @return string
381
     */
382
    public function getHelp(): string
383
    {
384
        return $this->help ?: '';
385
    }
386
387
    /**
388
     * 描述信息
389
     * @return string
390
     */
391
    public function getProcessedHelp(): string
392
    {
393
        $name = $this->name;
394
395
        $placeholders = [
396
            '%command.name%',
397
            '%command.full_name%',
398
        ];
399
        $replacements = [
400
            $name,
401
            $_SERVER['PHP_SELF'] . ' ' . $name,
402
        ];
403
404
        return str_replace($placeholders, $replacements, $this->getHelp());
405
    }
406
407
    /**
408
     * 设置别名
409
     * @param string[] $aliases
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
410
     * @return Command
411
     * @throws \InvalidArgumentException
412
     */
413
    public function setAliases(iterable $aliases)
414
    {
415
        foreach ($aliases as $alias) {
416
            $this->validateName($alias);
417
        }
418
419
        $this->aliases = $aliases;
420
421
        return $this;
422
    }
423
424
    /**
425
     * 获取别名
426
     * @return array
427
     */
428
    public function getAliases(): array
429
    {
430
        return $this->aliases;
431
    }
432
433
    /**
434
     * 获取简介
435
     * @param bool $short 是否简单的
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
436
     * @return string
437
     */
438
    public function getSynopsis(bool $short = false): string
439
    {
440
        $key = $short ? 'short' : 'long';
441
442
        if (!isset($this->synopsis[$key])) {
443
            $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short)));
444
        }
445
446
        return $this->synopsis[$key];
447
    }
448
449
    /**
450
     * 添加用法介绍
451
     * @param string $usage
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
452
     * @return $this
453
     */
454
    public function addUsage(string $usage)
455
    {
456
        if (0 !== strpos($usage, $this->name)) {
457
            $usage = sprintf('%s %s', $this->name, $usage);
458
        }
459
460
        $this->usages[] = $usage;
461
462
        return $this;
463
    }
464
465
    /**
466
     * 获取用法介绍
467
     * @return array
468
     */
469
    public function getUsages(): array
470
    {
471
        return $this->usages;
472
    }
473
474
    /**
475
     * 验证指令名称
476
     * @param string $name
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
477
     * @throws \InvalidArgumentException
478
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
479
    private function validateName(string $name)
0 ignored issues
show
Coding Style introduced by
Private method name "Command::validateName" must be prefixed with an underscore
Loading history...
480
    {
481
        if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
482
            throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
483
        }
484
    }
485
486
    /**
487
     * 输出表格
488
     * @param Table $table
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
489
     * @return string
490
     */
491
    protected function table(Table $table): string
492
    {
493
        $content = $table->render();
494
        $this->output->writeln($content);
495
        return $content;
496
    }
497
}
498