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
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
$this->setName('init') |
25
|
|
|
->setDescription('Will init the current github repository with default file templates') |
26
|
|
|
->addArgument( |
27
|
|
|
'type', |
28
|
|
|
InputArgument::OPTIONAL, |
29
|
|
|
'Repository type (library/project)', |
30
|
|
|
RepositoryType::PROJECT |
31
|
|
|
) |
32
|
|
|
->addOption( |
33
|
|
|
'symfony', |
34
|
|
|
null, |
35
|
|
|
InputOption::VALUE_NONE, |
36
|
|
|
'If symfony sub type' |
37
|
|
|
) |
38
|
|
|
->addOption('list', 'l', InputOption::VALUE_NONE, 'List template ids') |
39
|
|
|
->addOption( |
40
|
|
|
'id', |
41
|
|
|
null, |
42
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
43
|
|
|
'process only given templates ids' |
44
|
|
|
) |
45
|
|
|
->addOption( |
46
|
|
|
'ask-before-override', |
47
|
|
|
'a', |
48
|
|
|
InputOption::VALUE_NONE, |
49
|
|
|
'Will ask before overriding an existing file' |
50
|
|
|
) |
51
|
|
|
->addOption('force-override', 'f', InputOption::VALUE_NONE, 'Override existing files by default') |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$repositoryType = $input->getArgument('type'); |
61
|
|
|
$repositorySubType = true === $input->getOption('symfony') |
62
|
|
|
? RepositorySubType::SYMFONY |
63
|
|
|
: RepositorySubType::PHP |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
if (!$this->validateRepositoryType($output, $repositoryType)) { |
67
|
|
|
return 1; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$templateList = (new TemplateListFactory())->create($repositoryType); |
71
|
|
|
(new NamespaceResolver())->resolve($templateList, $repositoryType, $repositorySubType); |
72
|
|
|
|
73
|
|
|
$this->getProcessor($input, $output, $templateList, $repositoryType)->process(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param OutputInterface $output |
78
|
|
|
* @param string $repositoryType |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function validateRepositoryType(OutputInterface $output, $repositoryType) |
83
|
|
|
{ |
84
|
|
|
$availableTypeList = RepositoryType::all(); |
85
|
|
|
if (!in_array($repositoryType, $availableTypeList)) { |
86
|
|
|
$output->writeln(sprintf('<error>Unexpected type "%s" !</error>', $repositoryType)); |
87
|
|
|
$output->writeln(sprintf( |
88
|
|
|
'<info>Allowed type : %s </info>', |
89
|
|
|
implode(' / ', array_map(function ($availableMode) { |
90
|
|
|
return sprintf('<comment>%s</comment>', $availableMode); |
91
|
|
|
}, |
92
|
|
|
$availableTypeList |
93
|
|
|
) |
94
|
|
|
) |
95
|
|
|
)); |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param InputInterface $input |
105
|
|
|
* @param OutputInterface $output |
106
|
|
|
* @param Template[] $templateList |
107
|
|
|
* @param string $repositoryType |
108
|
|
|
* |
109
|
|
|
* @return InitCommandProcessor|ListCommandProcessor |
110
|
|
|
* |
111
|
|
|
* @throws \Twig_Error_Loader |
112
|
|
|
*/ |
113
|
|
|
protected function getProcessor(InputInterface $input, OutputInterface $output, array $templateList, $repositoryType) |
114
|
|
|
{ |
115
|
|
|
$forceOverride = $input->getOption('force-override'); |
116
|
|
|
$skipExisting = true === $forceOverride |
117
|
|
|
? false |
118
|
|
|
: false === $input->getOption('ask-before-override') |
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
if (true === $input->getOption('list')) { |
122
|
|
|
$processor = new ListCommandProcessor($this->getHelper('question'), $input, $output, $templateList); |
|
|
|
|
123
|
|
|
} else { |
124
|
|
|
$processor = new InitCommandProcessor( |
125
|
|
|
$this->getHelper('question'), |
126
|
|
|
$input, |
127
|
|
|
$output, |
128
|
|
|
new TemplateHelper( |
129
|
|
|
new \Twig_Environment(null, ['autoescape' => false]), |
130
|
|
|
(new VarFactory())->create(RepositoryType::PROJECT === $repositoryType) |
131
|
|
|
), |
132
|
|
|
$templateList, |
133
|
|
|
$skipExisting, |
134
|
|
|
$forceOverride, |
135
|
|
|
$input->getOption('id') |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $processor; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.