1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Processor; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
use Yoanm\DefaultPhpRepository\Helper\CommandHelper; |
7
|
|
|
use Yoanm\DefaultPhpRepository\Model\FolderTemplate; |
8
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class InitRepositoryProcessor |
12
|
|
|
*/ |
13
|
|
|
class InitRepositoryProcessor |
14
|
|
|
{ |
15
|
|
|
/** @var CommandHelper */ |
16
|
|
|
private $helper; |
17
|
|
|
/** @var Filesystem */ |
18
|
|
|
private $fileSystem; |
19
|
|
|
/** @var string */ |
20
|
|
|
private $rootPath; |
21
|
|
|
/** @var bool */ |
22
|
|
|
private $skipExisting; |
23
|
|
|
/** @var bool */ |
24
|
|
|
private $forceOverride; |
25
|
|
|
/** @var array */ |
26
|
|
|
private $idList = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param CommandHelper $helper |
30
|
|
|
* @param bool $skipExisting |
31
|
|
|
* @param bool $forceOverride |
32
|
|
|
* @param array $idList |
33
|
|
|
* @param string $rootPath |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
CommandHelper $helper, |
37
|
|
|
$skipExisting = true, |
38
|
|
|
$forceOverride = false, |
39
|
|
|
$idList = [], |
40
|
|
|
$rootPath = '.' |
41
|
|
|
) { |
42
|
|
|
$this->helper = $helper; |
43
|
|
|
|
44
|
|
|
$this->rootPath = realpath($rootPath); |
45
|
|
|
$this->fileSystem = new Filesystem(); |
46
|
|
|
|
47
|
|
|
$this->skipExisting = $skipExisting; |
48
|
|
|
$this->forceOverride = $forceOverride; |
49
|
|
|
$this->idList = $idList; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function process() |
53
|
|
|
{ |
54
|
|
|
$currentType = null; |
55
|
|
|
foreach ($this->helper->getTemplateList() as $template) { |
56
|
|
|
if (count($this->idList) && !in_array($template->getId(), $this->idList)) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
$this->helper->displayHeader($template, $currentType); |
60
|
|
|
|
61
|
|
|
$currentType = $this->helper->resolveCurrentType($template); |
62
|
|
|
|
63
|
|
|
$this->displayTemplate($template); |
64
|
|
|
$fileExist = false; |
65
|
|
|
if ($template instanceof FolderTemplate) { |
66
|
|
|
$process = false; |
67
|
|
|
foreach ($template->getFileList() as $subTemplate) { |
68
|
|
|
list($fileExist, $process) = $this->checkBeforeDump($subTemplate); |
69
|
|
|
|
70
|
|
|
if (false === $process) { |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
if (true === $process) { |
75
|
|
|
foreach ($template->getFileList() as $subTemplate) { |
76
|
|
|
$this->helper->dump($subTemplate); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
list($fileExist, $process) = $this->checkBeforeDump($template); |
81
|
|
|
|
82
|
|
|
if (true === $process) { |
83
|
|
|
$this->helper->dump($template); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
if (false === $fileExist) { |
87
|
|
|
$this->helper->display('<info>Done</info>'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Template $template |
94
|
|
|
*/ |
95
|
|
|
protected function displayTemplate(Template $template) |
96
|
|
|
{ |
97
|
|
|
$this->helper->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false); |
98
|
|
|
$this->helper->display(sprintf(' - <info>./%s</info> : ', $template->getTarget()), 0, false); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Template $template |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function checkBeforeDump(Template $template) |
107
|
|
|
{ |
108
|
|
|
$fileExist = $this->fileSystem->exists($this->resolveTargetPath($template)); |
109
|
|
|
$process = true; |
110
|
|
|
if ($fileExist) { |
111
|
|
|
if (false === $this->forceOverride && true === $this->skipExisting) { |
112
|
|
|
$this->helper->display('<comment>Skipped !</comment>'); |
113
|
|
|
$process = false; |
114
|
|
|
} else { |
115
|
|
|
$process = false; |
116
|
|
|
if (true === $this->forceOverride) { |
117
|
|
|
$process = true; |
118
|
|
|
$this->helper->display('<comment>Overriden !</comment>'); |
119
|
|
|
} elseif ( |
120
|
|
|
$this->helper->ask( |
121
|
|
|
new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false) |
122
|
|
|
) |
123
|
|
|
) { |
124
|
|
|
$process = true; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return [$fileExist, $process]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Template $template |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function resolveTargetPath(Template $template) |
138
|
|
|
{ |
139
|
|
|
return sprintf('%s/%s', $this->rootPath, $template->getTarget()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|