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\PathHelper; |
10
|
|
|
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper; |
11
|
|
|
use Yoanm\DefaultPhpRepository\Model\FolderTemplate; |
12
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class InitCommandProcessor |
16
|
|
|
*/ |
17
|
|
|
class InitCommandProcessor extends CommandProcessor |
18
|
|
|
{ |
19
|
|
|
/** @var TemplateHelper */ |
20
|
|
|
private $templateHelper; |
21
|
|
|
/** @var Filesystem */ |
22
|
|
|
private $fileSystem; |
23
|
|
|
/** @var string */ |
24
|
|
|
private $rootPath; |
25
|
|
|
/** @var bool */ |
26
|
|
|
private $skipExisting; |
27
|
|
|
/** @var bool */ |
28
|
|
|
private $forceOverride; |
29
|
|
|
/** @var array */ |
30
|
|
|
private $idList = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param QuestionHelper $questionHelper |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* @param TemplateHelper $templateHelper |
37
|
|
|
* @param Template[] $templateList |
38
|
|
|
* @param bool $skipExisting |
39
|
|
|
* @param bool $forceOverride |
40
|
|
|
* @param array $idList |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
QuestionHelper $questionHelper, |
44
|
|
|
InputInterface $input, |
45
|
|
|
OutputInterface $output, |
46
|
|
|
TemplateHelper $templateHelper, |
47
|
|
|
array $templateList, |
48
|
|
|
$skipExisting = true, |
49
|
|
|
$forceOverride = false, |
50
|
|
|
$idList = [], |
51
|
|
|
$rootPath = '.' |
52
|
|
|
) { |
53
|
|
|
parent::__construct($questionHelper, $input, $output, $templateList); |
54
|
|
|
|
55
|
|
|
$this->templateHelper = $templateHelper; |
56
|
|
|
$this->rootPath = realpath($rootPath); |
57
|
|
|
$this->fileSystem = new Filesystem(); |
58
|
|
|
|
59
|
|
|
$this->skipExisting = $skipExisting; |
60
|
|
|
$this->forceOverride = $forceOverride; |
61
|
|
|
$this->idList = $idList; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function process() |
65
|
|
|
{ |
66
|
|
|
$currentType = null; |
67
|
|
|
foreach ($this->getTemplateList() as $template) { |
68
|
|
|
if (count($this->idList) && !in_array($template->getId(), $this->idList)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
$this->displayHeader($template, $currentType); |
72
|
|
|
|
73
|
|
|
$currentType = $this->resolveCurrentType($template); |
74
|
|
|
$fileExist = false; |
75
|
|
|
if ($template instanceof FolderTemplate) { |
76
|
|
|
$this->displayTemplate($template); |
77
|
|
|
foreach ($template->getFileList() as $subTemplate) { |
78
|
|
|
$targetPath = sprintf('%s/%s', $this->rootPath, $subTemplate->getTarget()); |
79
|
|
|
list($fileExist, $process) = $this->validateDump($targetPath); |
80
|
|
|
if (false === $process) { |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
if (true === $process) { |
|
|
|
|
85
|
|
|
foreach ($template->getFileList() as $subTemplate) { |
86
|
|
|
$targetPath = sprintf('%s/%s', $this->rootPath, $subTemplate->getTarget()); |
87
|
|
|
$this->dumpTemplate($subTemplate, $targetPath); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
$this->displayTemplate($template); |
92
|
|
|
$targetPath = sprintf('%s/%s', $this->rootPath, $template->getTarget()); |
93
|
|
|
list($fileExist, $process) = $this->validateDump($targetPath); |
94
|
|
|
if (true === $process) { |
95
|
|
|
$this->dumpTemplate($template, $targetPath); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
if (false === $fileExist) { |
99
|
|
|
$this->display('<info>Done</info>'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Template $template |
106
|
|
|
*/ |
107
|
|
|
protected function displayTemplate(Template $template) |
108
|
|
|
{ |
109
|
|
|
$this->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false); |
110
|
|
|
$targetRelativePath = sprintf('./%s', $template->getTarget()); |
111
|
|
|
$this->display(sprintf(' - <info>%s</info> : ', $targetRelativePath), 0, false); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Template $template |
116
|
|
|
* @param string $path |
117
|
|
|
*/ |
118
|
|
|
protected function dumpTemplate(Template $template, $path) |
119
|
|
|
{ |
120
|
|
|
$templateId = $template->getSource(); |
121
|
|
|
if (null !== $template->getNamespace()) { |
122
|
|
|
$templateId = sprintf('@%s/%s', $template->getNamespace(), $template->getSource()); |
123
|
|
|
} |
124
|
|
|
$this->templateHelper->dumpTemplate($templateId, $path); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $target |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
protected function validateDump($target) |
132
|
|
|
{ |
133
|
|
|
$fileExist = $this->fileSystem->exists($target); |
134
|
|
|
$process = true; |
135
|
|
|
if ($fileExist) { |
136
|
|
|
if (false === $this->forceOverride && true === $this->skipExisting) { |
137
|
|
|
$this->display('<comment>Skipped !</comment>'); |
138
|
|
|
$process = false; |
139
|
|
|
} else { |
140
|
|
|
$process = false; |
141
|
|
|
if (true === $this->forceOverride) { |
142
|
|
|
$this->display('<comment>Overriden !</comment>'); |
143
|
|
|
$process = true; |
144
|
|
|
} elseif ($this->doOverwrite()) { |
145
|
|
|
$process = true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return [$fileExist, $process]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/**@return bool |
154
|
|
|
*/ |
155
|
|
|
protected function doOverwrite() |
156
|
|
|
{ |
157
|
|
|
return $this->ask(new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: