1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Command; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Command\Command; |
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Yoanm\DefaultPhpRepository\Factory\TemplateListFactory; |
10
|
|
|
use Yoanm\DefaultPhpRepository\Factory\VarFactory; |
11
|
|
|
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper; |
12
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
13
|
|
|
use Yoanm\DefaultPhpRepository\Processor\InitCommandProcessor; |
14
|
|
|
use Yoanm\DefaultPhpRepository\Processor\ListCommandProcessor; |
15
|
|
|
use Yoanm\DefaultPhpRepository\Resolver\NamespaceResolver; |
16
|
|
|
|
17
|
|
|
class InitCommand extends Command |
18
|
|
|
{ |
19
|
|
|
const TYPE_INIT = 'template.init'; |
20
|
|
|
const TYPE_GIT = 'template.git'; |
21
|
|
|
const TYPE_COMPOSER = 'template.composer'; |
22
|
|
|
const TYPE_TEST = 'template.test'; |
23
|
|
|
const TYPE_CI = 'template.ci'; |
24
|
|
|
|
25
|
|
|
const OUTPUT_LEVEL_SPACE = ' '; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this->setName('init') |
33
|
|
|
->setDescription('Will init the current github repository with default file templates') |
34
|
|
|
->addArgument( |
35
|
|
|
'type', |
36
|
|
|
InputArgument::OPTIONAL, |
37
|
|
|
'Repository type (library/project)', |
38
|
|
|
RepositoryType::PROJECT |
39
|
|
|
) |
40
|
|
|
->addOption( |
41
|
|
|
'symfony', |
42
|
|
|
null, |
43
|
|
|
InputOption::VALUE_NONE, |
44
|
|
|
'If symfony sub type' |
45
|
|
|
) |
46
|
|
|
->addOption('list', 'l', InputOption::VALUE_NONE, 'List template ids') |
47
|
|
|
->addOption( |
48
|
|
|
'id', |
49
|
|
|
null, |
50
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
51
|
|
|
'process only given templates ids' |
52
|
|
|
) |
53
|
|
|
->addOption( |
54
|
|
|
'ask-before-override', |
55
|
|
|
'a', |
56
|
|
|
InputOption::VALUE_NONE, |
57
|
|
|
'Will ask before overriding an existing file' |
58
|
|
|
) |
59
|
|
|
->addOption('force-override', 'f', InputOption::VALUE_NONE, 'Override existing files by default') |
60
|
|
|
; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
|
$repositoryType = $input->getArgument('type'); |
69
|
|
|
$repositorySubType = true === $input->getOption('symfony') |
70
|
|
|
? RepositorySubType::SYMFONY |
71
|
|
|
: RepositorySubType::PHP |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
if (!$this->validateRepositoryType($output, $repositoryType)) { |
75
|
|
|
return 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$templateList = (new TemplateListFactory())->create($repositoryType); |
79
|
|
|
(new NamespaceResolver())->resolve($templateList, $repositoryType, $repositorySubType); |
80
|
|
|
|
81
|
|
|
$this->getProcessor($input, $output, $templateList, $repositoryType)->process(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param OutputInterface $output |
86
|
|
|
* @param string $repositoryType |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
protected function validateRepositoryType(OutputInterface $output, $repositoryType) |
91
|
|
|
{ |
92
|
|
|
// Check type |
93
|
|
|
$availableTypeList = [RepositoryType::LIBRARY, RepositoryType::PROJECT]; |
94
|
|
|
if (!in_array($repositoryType, $availableTypeList)) { |
95
|
|
|
$output->writeln(sprintf('<error>Unexpected type "%s" !</error>', $repositoryType)); |
96
|
|
|
$output->writeln(sprintf( |
97
|
|
|
'<info>Allowed type : %s </info>', |
98
|
|
|
implode( |
99
|
|
|
' / ', |
100
|
|
|
array_map( |
101
|
|
|
function ($availableMode) { |
102
|
|
|
return sprintf('<comment>%s</comment>', $availableMode); |
103
|
|
|
}, |
104
|
|
|
$availableTypeList |
105
|
|
|
) |
106
|
|
|
) |
107
|
|
|
)); |
108
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param InputInterface $input |
117
|
|
|
* @param OutputInterface $output |
118
|
|
|
* @param Template[] $templateList |
119
|
|
|
* @param string $repositoryType |
120
|
|
|
* |
121
|
|
|
* @return InitCommandProcessor|ListCommandProcessor |
122
|
|
|
* |
123
|
|
|
* @throws \Twig_Error_Loader |
124
|
|
|
*/ |
125
|
|
|
protected function getProcessor(InputInterface $input, OutputInterface $output, array $templateList, $repositoryType) |
126
|
|
|
{ |
127
|
|
|
// Default processor |
128
|
|
|
$processor = new ListCommandProcessor($this->getHelper('question'), $input, $output, $templateList); |
|
|
|
|
129
|
|
|
|
130
|
|
|
if (false === $input->getOption('list')) { |
131
|
|
|
|
132
|
|
|
$forceOverride = $input->getOption('force-override'); |
133
|
|
|
$skipExisting = true === $forceOverride |
134
|
|
|
? false |
135
|
|
|
: false === $input->getOption('ask-before-override') |
136
|
|
|
; |
137
|
|
|
$templateHelper = new TemplateHelper( |
138
|
|
|
new \Twig_Environment(null, ['autoescape' => false]), |
139
|
|
|
(new VarFactory())->create($repositoryType) |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$processor = new InitCommandProcessor( |
143
|
|
|
$this->getHelper('question'), |
144
|
|
|
$input, |
145
|
|
|
$output, |
146
|
|
|
$templateHelper, |
147
|
|
|
$templateList, |
148
|
|
|
$skipExisting, |
149
|
|
|
$forceOverride, |
150
|
|
|
$input->getOption('id') |
|
|
|
|
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $processor; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.