1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Processor; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Console\Question\Question; |
8
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CommandProcessor |
12
|
|
|
*/ |
13
|
|
|
class CommandProcessor |
14
|
|
|
{ |
15
|
|
|
const OUTPUT_LEVEL_SPACE = ' '; |
16
|
|
|
|
17
|
|
|
/** @var InputInterface */ |
18
|
|
|
private $input; |
19
|
|
|
/** @var OutputInterface */ |
20
|
|
|
private $output; |
21
|
|
|
/** @var QuestionHelper */ |
22
|
|
|
private $questionHelper; |
23
|
|
|
/** @var Template[] */ |
24
|
|
|
private $templateList; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param QuestionHelper $questionHelper |
28
|
|
|
* @param InputInterface $input |
29
|
|
|
* @param OutputInterface $output |
30
|
|
|
* @param Template[] $templateList |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
QuestionHelper $questionHelper, |
34
|
|
|
InputInterface $input, |
35
|
|
|
OutputInterface $output, |
36
|
|
|
array $templateList |
37
|
|
|
) { |
38
|
|
|
$this->input = $input; |
39
|
|
|
$this->output = $output; |
40
|
|
|
$this->questionHelper = $questionHelper; |
41
|
|
|
$this->templateList = $templateList; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Template[] |
46
|
|
|
*/ |
47
|
|
|
public function getTemplateList() |
48
|
|
|
{ |
49
|
|
|
return $this->templateList; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Template $template |
54
|
|
|
* @param string $currentType |
55
|
|
|
*/ |
56
|
|
|
public function displayHeader(Template $template, $currentType) |
57
|
|
|
{ |
58
|
|
|
if (null === $currentType || !preg_match(sprintf('#^%s\.#', preg_quote($currentType)), $template->getId())) { |
59
|
|
|
$header = ucwords($this->resolveCurrentType($template)); |
60
|
|
|
|
61
|
|
|
if ('Git' === $header) { |
62
|
|
|
$header = 'Git/Github'; |
63
|
|
|
} elseif ('Ci' === $header) { |
64
|
|
|
$header = 'Continuous integration'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->display(sprintf('<info>%s :</info>', $header), 1); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $message |
73
|
|
|
* @param int $level |
74
|
|
|
*/ |
75
|
|
|
public function display($message, $level = 0, $ln = true) |
76
|
|
|
{ |
77
|
|
|
$message = sprintf( |
78
|
|
|
'%s%s', |
79
|
|
|
str_repeat(self::OUTPUT_LEVEL_SPACE, $level), |
80
|
|
|
$message |
81
|
|
|
); |
82
|
|
|
if (true === $ln) { |
83
|
|
|
$this->output->writeln($message); |
84
|
|
|
} else { |
85
|
|
|
$this->output->write($message); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Template $template |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function resolveCurrentType(Template $template) |
95
|
|
|
{ |
96
|
|
|
$currentType = preg_replace('#^([^\.]+)\..*#', '\1', $template->getId()); |
97
|
|
|
|
98
|
|
|
return $currentType; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Question $question |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function ask(Question $question) |
107
|
|
|
{ |
108
|
|
|
return $this->questionHelper->ask($this->input, $this->output, $question); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|