1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Command\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\ConfirmationQuestion; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Yoanm\DefaultPhpRepository\Exception\TargetFileExistsException; |
10
|
|
|
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CommandTemplateHelper |
14
|
|
|
*/ |
15
|
|
|
class CommandTemplateHelper extends TemplateHelper |
16
|
|
|
{ |
17
|
|
|
/** @var InputInterface */ |
18
|
|
|
private $input; |
19
|
|
|
/** @var OutputInterface */ |
20
|
|
|
private $output; |
21
|
|
|
/** @var Filesystem */ |
22
|
|
|
private $fs; |
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $skipExisting; |
25
|
|
|
/** @var bool */ |
26
|
|
|
private $forceOverride; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param QuestionHelper $questionHelper |
30
|
|
|
* @param InputInterface $input |
31
|
|
|
* @param OutputInterface $output |
32
|
|
|
* @param array $variableList |
33
|
|
|
* @param bool $skipExisting |
34
|
|
|
* @param bool $forceOverride |
35
|
|
|
* @param array $extraTemplatePath |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
QuestionHelper $questionHelper, |
39
|
|
|
InputInterface $input, |
40
|
|
|
OutputInterface $output, |
41
|
|
|
array $variableList, |
42
|
|
|
$skipExisting = true, |
43
|
|
|
$forceOverride = false, |
44
|
|
|
array $extraTemplatePath = [] |
45
|
|
|
) { |
46
|
|
|
parent::__construct($variableList, $extraTemplatePath); |
47
|
|
|
|
48
|
|
|
$this->questionHelper = $questionHelper; |
|
|
|
|
49
|
|
|
$this->fs = new Filesystem(); |
50
|
|
|
$this->input = $input; |
51
|
|
|
$this->output = $output; |
52
|
|
|
|
53
|
|
|
$this->skipExisting = $skipExisting; |
54
|
|
|
$this->forceOverride = $forceOverride; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $templateFilePath |
59
|
|
|
* @param string $outputFilePath |
60
|
|
|
* @param bool $overwrite overwrite even if present |
61
|
|
|
* |
62
|
|
|
* @throws TargetFileExistsException |
63
|
|
|
*/ |
64
|
|
|
public function dumpTemplate($templateFilePath, $outputFilePath, $overwrite = false) |
65
|
|
|
{ |
66
|
|
|
$this->output->write(" <info>$outputFilePath</info> : "); |
67
|
|
|
$fileExist = $this->fs->exists($outputFilePath); |
68
|
|
|
$process = true; |
69
|
|
|
if ($fileExist) { |
70
|
|
|
if (false === $this->forceOverride && true === $this->skipExisting) { |
71
|
|
|
$this->output->writeln('<comment>Skipped !</comment>'); |
72
|
|
|
$process = false; |
73
|
|
|
} else { |
74
|
|
|
$process = false; |
75
|
|
|
if (true === $this->forceOverride) { |
76
|
|
|
$process = true; |
77
|
|
|
$this->output->writeln('<comment>Overriden !</comment>'); |
78
|
|
|
} elseif ($this->doOverwrite()) { |
79
|
|
|
$process = true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if (true === $process) { |
84
|
|
|
parent::dumpTemplate($templateFilePath, $outputFilePath); |
85
|
|
|
if (false === $fileExist) { |
86
|
|
|
$this->output->writeln('<info>Done</info>'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/**@return bool |
92
|
|
|
*/ |
93
|
|
|
protected function doOverwrite() |
94
|
|
|
{ |
95
|
|
|
$question = new ConfirmationQuestion( |
96
|
|
|
'<question>Overwrite (y/n) ? [n]</question>', |
97
|
|
|
false |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $this->questionHelper->ask($this->input, $this->output, $question); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|