1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* spiral |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
namespace Spiral\Console\Traits; |
8
|
|
|
|
9
|
|
|
use Spiral\Console\Helpers\AskHelper; |
10
|
|
|
use Symfony\Component\Console\Helper\Table; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Table and AskHelper shortcuts. |
14
|
|
|
*/ |
15
|
|
|
trait HelpersTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* OutputInterface is the interface implemented by all Output classes. Only exists when command |
19
|
|
|
* are being executed. |
20
|
|
|
* |
21
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
22
|
|
|
*/ |
23
|
|
|
protected $output = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* InputInterface is the interface implemented by all input classes. Only exists when command |
27
|
|
|
* are being executed. |
28
|
|
|
* |
29
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
30
|
|
|
*/ |
31
|
|
|
protected $input = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Writes a message to the output. |
35
|
|
|
* |
36
|
|
|
* @param string|array $messages The message as an array of lines or a single string |
37
|
|
|
* @param bool $newline Whether to add a newline |
38
|
|
|
* |
39
|
|
|
* @throws \InvalidArgumentException When unknown output type is given |
40
|
|
|
*/ |
41
|
|
|
protected function write($messages, bool $newline = false) |
42
|
|
|
{ |
43
|
|
|
return $this->output->write($messages, $newline); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Writes a message to the output and adds a newline at the end. |
48
|
|
|
* |
49
|
|
|
* @param string|array $messages The message as an array of lines of a single string |
50
|
|
|
* |
51
|
|
|
* @throws \InvalidArgumentException When unknown output type is given |
52
|
|
|
*/ |
53
|
|
|
protected function writeln($messages) |
54
|
|
|
{ |
55
|
|
|
return $this->output->writeln($messages); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Input option. |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
protected function option(string $name) |
66
|
|
|
{ |
67
|
|
|
return $this->input->getOption($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Input argument. |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
protected function argument(string $name) |
78
|
|
|
{ |
79
|
|
|
return $this->input->getArgument($name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Table helper instance with configured header and pre-defined set of rows. |
84
|
|
|
* |
85
|
|
|
* @param array $headers |
86
|
|
|
* @param array $rows |
87
|
|
|
* @param string $style |
88
|
|
|
* |
89
|
|
|
* @return Table |
90
|
|
|
*/ |
91
|
|
|
protected function table(array $headers, array $rows = [], string $style = 'default'): Table |
92
|
|
|
{ |
93
|
|
|
$table = new Table($this->output); |
94
|
|
|
|
95
|
|
|
return $table->setHeaders($headers)->setRows($rows)->setStyle($style); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Interop\Container\ContainerInterface |
100
|
|
|
*/ |
101
|
|
|
abstract protected function iocContainer(); |
102
|
|
|
} |