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\ConfirmationQuestion; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper; |
10
|
|
|
use Yoanm\DefaultPhpRepository\Model\FolderTemplate; |
11
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class InitCommandProcessor |
15
|
|
|
*/ |
16
|
|
|
class InitCommandProcessor extends CommandProcessor |
17
|
|
|
{ |
18
|
|
|
/** @var TemplateHelper */ |
19
|
|
|
private $templateHelper; |
20
|
|
|
/** @var Filesystem */ |
21
|
|
|
private $fileSystem; |
22
|
|
|
/** @var string */ |
23
|
|
|
private $rootPath; |
24
|
|
|
/** @var bool */ |
25
|
|
|
private $skipExisting; |
26
|
|
|
/** @var bool */ |
27
|
|
|
private $forceOverride; |
28
|
|
|
/** @var array */ |
29
|
|
|
private $idList = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param QuestionHelper $questionHelper |
33
|
|
|
* @param InputInterface $input |
34
|
|
|
* @param OutputInterface $output |
35
|
|
|
* @param TemplateHelper $templateHelper |
36
|
|
|
* @param Template[] $templateList |
37
|
|
|
* @param bool $skipExisting |
38
|
|
|
* @param bool $forceOverride |
39
|
|
|
* @param array $idList |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
QuestionHelper $questionHelper, |
43
|
|
|
InputInterface $input, |
44
|
|
|
OutputInterface $output, |
45
|
|
|
TemplateHelper $templateHelper, |
46
|
|
|
array $templateList, |
47
|
|
|
$skipExisting = true, |
48
|
|
|
$forceOverride = false, |
49
|
|
|
$idList = [], |
50
|
|
|
$rootPath = '.' |
51
|
|
|
) { |
52
|
|
|
parent::__construct($questionHelper, $input, $output, $templateList); |
53
|
|
|
|
54
|
|
|
$this->templateHelper = $templateHelper; |
55
|
|
|
$this->rootPath = realpath($rootPath); |
56
|
|
|
$this->fileSystem = new Filesystem(); |
57
|
|
|
|
58
|
|
|
$this->skipExisting = $skipExisting; |
59
|
|
|
$this->forceOverride = $forceOverride; |
60
|
|
|
$this->idList = $idList; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function process() |
64
|
|
|
{ |
65
|
|
|
$currentType = null; |
66
|
|
|
foreach ($this->getTemplateList() as $template) { |
67
|
|
|
if (count($this->idList) && !in_array($template->getId(), $this->idList)) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
$this->displayHeader($template, $currentType); |
71
|
|
|
|
72
|
|
|
$currentType = $this->resolveCurrentType($template); |
73
|
|
|
$this->displayTemplate($template); |
74
|
|
|
$fileExist = false; |
75
|
|
|
if ($template instanceof FolderTemplate) { |
76
|
|
|
$process = false; |
77
|
|
|
foreach ($template->getFileList() as $subTemplate) { |
78
|
|
|
list($fileExist, $process) = $this->validateDump($subTemplate); |
79
|
|
|
|
80
|
|
|
if (false === $process) { |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
if (true === $process) { |
85
|
|
|
foreach ($template->getFileList() as $subTemplate) { |
86
|
|
|
$this->dumpTemplate($subTemplate); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
|
|
list($fileExist, $process) = $this->validateDump($template); |
91
|
|
|
|
92
|
|
|
if (true === $process) { |
93
|
|
|
$this->dumpTemplate($template); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
if (false === $fileExist) { |
97
|
|
|
$this->display('<info>Done</info>'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Template $template |
104
|
|
|
*/ |
105
|
|
|
protected function displayTemplate(Template $template) |
106
|
|
|
{ |
107
|
|
|
$this->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false); |
108
|
|
|
$targetRelativePath = sprintf('./%s', $template->getTarget()); |
109
|
|
|
$this->display(sprintf(' - <info>%s</info> : ', $targetRelativePath), 0, false); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Template $template |
114
|
|
|
* @param string $path |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
protected function dumpTemplate(Template $template) |
117
|
|
|
{ |
118
|
|
|
$templateId = $template->getSource(); |
119
|
|
|
if (null !== $template->getNamespace()) { |
120
|
|
|
$templateId = sprintf('@%s/%s', $template->getNamespace(), $template->getSource()); |
121
|
|
|
} |
122
|
|
|
$this->templateHelper->dumpTemplate($templateId, $this->resolveTargetPath($template)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $target |
|
|
|
|
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function validateDump(Template $template) |
130
|
|
|
{ |
131
|
|
|
$fileExist = $this->fileSystem->exists($this->resolveTargetPath($template)); |
132
|
|
|
$process = true; |
133
|
|
|
if ($fileExist) { |
134
|
|
|
if (false === $this->forceOverride && true === $this->skipExisting) { |
135
|
|
|
$this->display('<comment>Skipped !</comment>'); |
136
|
|
|
$process = false; |
137
|
|
|
} else { |
138
|
|
|
$process = false; |
139
|
|
|
if (true === $this->forceOverride) { |
140
|
|
|
$this->display('<comment>Overriden !</comment>'); |
141
|
|
|
$process = true; |
142
|
|
|
} elseif ($this->doOverwrite()) { |
143
|
|
|
$process = true; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return [$fileExist, $process]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/**@return bool |
152
|
|
|
*/ |
153
|
|
|
protected function doOverwrite() |
154
|
|
|
{ |
155
|
|
|
return $this->ask(new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param Template $template |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function resolveTargetPath(Template $template) |
164
|
|
|
{ |
165
|
|
|
return sprintf('%s/%s', $this->rootPath, $template->getTarget()); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.