1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Helper; |
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 Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CommandHelper |
13
|
|
|
*/ |
14
|
|
|
class CommandHelper |
15
|
|
|
{ |
16
|
|
|
const OUTPUT_LEVEL_SPACE = ' '; |
17
|
|
|
|
18
|
|
|
/** @var InputInterface */ |
19
|
|
|
private $input; |
20
|
|
|
/** @var OutputInterface */ |
21
|
|
|
private $output; |
22
|
|
|
/** @var QuestionHelper */ |
23
|
|
|
private $questionHelper; |
24
|
|
|
/** @var TemplateHelper */ |
25
|
|
|
private $templateHelper; |
26
|
|
|
/** @var Filesystem */ |
27
|
|
|
private $fileSystem; |
28
|
|
|
/** @var Template[] */ |
29
|
|
|
private $templateList; |
30
|
|
|
/** @var string */ |
31
|
|
|
private $rootPath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* @param TemplateHelper $templateHelper |
37
|
|
|
* @param QuestionHelper $questionHelper |
38
|
|
|
* @param Template[] $templateList |
39
|
|
|
* @param string $rootPath |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
InputInterface $input, |
43
|
|
|
OutputInterface $output, |
44
|
|
|
TemplateHelper $templateHelper, |
45
|
|
|
QuestionHelper $questionHelper, |
46
|
|
|
array $templateList, |
47
|
|
|
$rootPath = '.' |
48
|
|
|
) { |
49
|
|
|
$this->input = $input; |
50
|
|
|
$this->output = $output; |
51
|
|
|
|
52
|
|
|
$this->questionHelper = $questionHelper; |
53
|
|
|
$this->templateHelper = $templateHelper; |
54
|
|
|
|
55
|
|
|
$this->templateList = $templateList; |
56
|
|
|
$this->rootPath = realpath($rootPath); |
57
|
|
|
|
58
|
|
|
$this->fileSystem = new Filesystem(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Template[] |
63
|
|
|
*/ |
64
|
|
|
public function getTemplateList() |
65
|
|
|
{ |
66
|
|
|
return $this->templateList; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Template $template |
71
|
|
|
* @param string $currentType |
72
|
|
|
*/ |
73
|
|
|
public function displayHeader(Template $template, $currentType) |
74
|
|
|
{ |
75
|
|
|
if (null === $currentType || !preg_match(sprintf('#^%s\.#', preg_quote($currentType)), $template->getId())) { |
76
|
|
|
$header = ucwords($this->resolveCurrentType($template)); |
77
|
|
|
|
78
|
|
|
if ('Git' === $header) { |
79
|
|
|
$header = 'Git/Github'; |
80
|
|
|
} elseif ('Ci' === $header) { |
81
|
|
|
$header = 'Continuous integration'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->display(sprintf('<info>%s :</info>', $header), 1); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $message |
90
|
|
|
* @param int $level |
91
|
|
|
*/ |
92
|
|
|
public function display($message, $level = 0, $ln = true) |
93
|
|
|
{ |
94
|
|
|
$message = sprintf( |
95
|
|
|
'%s%s', |
96
|
|
|
str_repeat(self::OUTPUT_LEVEL_SPACE, $level), |
97
|
|
|
$message |
98
|
|
|
); |
99
|
|
|
if (true === $ln) { |
100
|
|
|
$this->output->writeln($message); |
101
|
|
|
} else { |
102
|
|
|
$this->output->write($message); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Template $template |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function resolveCurrentType(Template $template) |
112
|
|
|
{ |
113
|
|
|
$currentType = preg_replace('#^([^\.]+)\..*#', '\1', $template->getId()); |
114
|
|
|
|
115
|
|
|
return $currentType; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Template $template |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function resolveTargetPath(Template $template) |
126
|
|
|
{ |
127
|
|
|
return sprintf('%s/%s', $this->rootPath, $template->getTarget()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Question $question |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function ask(Question $question) |
136
|
|
|
{ |
137
|
|
|
return $this->questionHelper->ask($this->input, $this->output, $question); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Template $template |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function targetExist(Template $template) |
146
|
|
|
{ |
147
|
|
|
return $this->fileSystem->exists($this->resolveTargetPath($template)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Template $template |
152
|
|
|
*/ |
153
|
|
|
public function dump(Template $template) |
154
|
|
|
{ |
155
|
|
|
$templateId = $template->getSource(); |
156
|
|
|
if (null !== $template->getNamespace()) { |
157
|
|
|
$templateId = sprintf('@%s/%s', $template->getNamespace(), $template->getSource()); |
158
|
|
|
} |
159
|
|
|
$this->templateHelper->dump($templateId, $this->resolveTargetPath($template)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|