1
|
|
|
<?php namespace Limoncello\Commands\Wrappers; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Commands\IoInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Commands |
25
|
|
|
*/ |
26
|
|
|
class ConsoleIoWrapper implements IoInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var InputInterface |
30
|
|
|
*/ |
31
|
|
|
private $input; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var OutputInterface |
35
|
|
|
*/ |
36
|
|
|
private $output; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
4 |
|
$this->input = $input; |
45
|
4 |
|
$this->output = $output; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
1 |
|
public function hasArgument(string $name): bool |
52
|
|
|
{ |
53
|
1 |
|
return $this->getInput()->hasArgument($name); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
2 |
|
public function getArgument(string $name) |
60
|
|
|
{ |
61
|
2 |
|
return $this->getInput()->getArgument($name); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
1 |
|
public function getArguments(): array |
68
|
|
|
{ |
69
|
1 |
|
return $this->getInput()->getArguments(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
1 |
|
public function hasOption(string $name): bool |
76
|
|
|
{ |
77
|
1 |
|
return $this->getInput()->hasOption($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
1 |
|
public function getOption(string $name) |
84
|
|
|
{ |
85
|
1 |
|
return $this->getInput()->getOption($name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
1 |
|
public function getOptions(): array |
92
|
|
|
{ |
93
|
1 |
|
return $this->getInput()->getOptions(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function writeInfo(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$isNewLine = false; |
102
|
|
|
$options = $this->convertVerbosityLevel($verbosity); |
103
|
|
|
$this->getOutput()->write("<info>$message</info>", $isNewLine, $options); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function writeWarning(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$isNewLine = false; |
114
|
|
|
$options = $this->convertVerbosityLevel($verbosity); |
115
|
|
|
$this->getOutput()->write("<comment>$message</comment>", $isNewLine, $options); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function writeError(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$isNewLine = false; |
126
|
|
|
$options = $this->convertVerbosityLevel($verbosity); |
127
|
|
|
$this->getOutput()->write("<error>$message</error>", $isNewLine, $options); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return OutputInterface |
134
|
|
|
*/ |
135
|
|
|
protected function getOutput(): OutputInterface |
136
|
|
|
{ |
137
|
|
|
return $this->output; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return InputInterface |
142
|
|
|
*/ |
143
|
3 |
|
protected function getInput(): InputInterface |
144
|
|
|
{ |
145
|
3 |
|
return $this->input; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param int $verbosity |
150
|
|
|
* |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
|
|
protected function convertVerbosityLevel(int $verbosity): int |
154
|
|
|
{ |
155
|
|
|
switch ($verbosity) { |
156
|
|
|
case static::VERBOSITY_QUIET: |
157
|
|
|
$result = OutputInterface::VERBOSITY_QUIET; |
158
|
|
|
break; |
159
|
|
|
case static::VERBOSITY_NORMAL: |
160
|
|
|
$result = OutputInterface::VERBOSITY_NORMAL; |
161
|
|
|
break; |
162
|
|
|
case static::VERBOSITY_VERBOSE: |
163
|
|
|
$result = OutputInterface::VERBOSITY_VERBOSE; |
164
|
|
|
break; |
165
|
|
|
case static::VERBOSITY_VERY_VERBOSE: |
166
|
|
|
$result = OutputInterface::VERBOSITY_VERY_VERBOSE; |
167
|
|
|
break; |
168
|
|
|
default: |
169
|
|
|
$result = OutputInterface::VERBOSITY_NORMAL; |
170
|
|
|
break; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $result; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.