1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSSPrites\Commands; |
4
|
|
|
|
5
|
|
|
use League\Container\ContainerAwareInterface; |
6
|
|
|
use League\Container\ContainerAwareTrait; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
|
9
|
|
|
abstract class AbstractBaseCommand extends Command implements ContainerAwareInterface |
10
|
|
|
{ |
11
|
|
|
use ContainerAwareTrait; |
12
|
|
|
|
13
|
|
|
protected $input; |
14
|
|
|
protected $output; |
15
|
|
|
protected $dialog; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $option |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
protected function getOptionOrAsk($option, $question, $default = null) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$value = $this->input->getOption($option); |
25
|
|
|
if (is_null($value)) { |
26
|
|
|
$this->output->writeln(''); |
27
|
|
|
$value = $this->dialog->ask($this->output, '<question>'.$question.' ?</question> ', $default); |
28
|
|
|
$this->output->writeln('<comment>'.$question.' is now "'.$value.'"</comment>'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $value; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $option |
36
|
|
|
*/ |
37
|
|
|
protected function getOptionOrSelect($option, $question, array $choices, $default = null) |
38
|
|
|
{ |
39
|
|
|
$value = $this->input->getOption($option); |
40
|
|
|
if (is_null($value)) { |
41
|
|
|
$this->output->writeln(''); |
42
|
|
|
$value = $this->dialog->select($this->output, '<question>'.$question.' ?</question> ', $choices, $default); |
43
|
|
|
$value = $choices[$value]; |
44
|
|
|
$this->output->writeln('<comment>'.$question.' is now "'.$value.'"</comment>'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!in_array($value, $choices)) { |
48
|
|
|
throw new \Exception('Invalid '.$question.' '.$value.'.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $option |
56
|
|
|
*/ |
57
|
|
|
protected function getOptionOrAskConfirmation($option, $question, $default = null) |
58
|
|
|
{ |
59
|
|
|
$value = $this->input->getOption($option); |
60
|
|
|
if (is_null($value)) { |
61
|
|
|
$this->output->writeln(''); |
62
|
|
|
$value = $this->dialog->askConfirmation($this->output, '<question>'.$question.' ?</question> ', $default); |
63
|
|
|
$this->output->writeln('<comment>'.$question.' is now "'.($value ? 'enabled' : 'disabled').'"</comment>'); |
64
|
|
|
|
65
|
|
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$value = ($value === 'true' || $value === 'yes' || $value === 'y' || $value === '1'); |
69
|
|
|
|
70
|
|
|
return $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $option |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
protected function getOptionOrAskAndValidate($option, $question, \Closure $callback, $default = null) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$value = $this->input->getOption($option); |
81
|
|
|
if (is_null($value)) { |
82
|
|
|
$this->output->writeln(''); |
83
|
|
|
$value = $this->dialog->askAndValidate( |
84
|
|
|
$this->output, |
85
|
|
|
'<question>'.$question.' ?</question> ', |
86
|
|
|
$callback, |
87
|
|
|
$default |
88
|
|
|
); |
89
|
|
|
$this->output->writeln('<comment>'.$question.' is now "'.$value.'"</comment>'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $value; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.