1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\ComposerConfigManager\Infrastructure\Command; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
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\ComposerConfigManager\Application\Loader\ConfigurationFileLoaderInterface; |
10
|
|
|
use Yoanm\ComposerConfigManager\Application\UpdateConfigurationFileList; |
11
|
|
|
use Yoanm\ComposerConfigManager\Application\UpdateConfigurationFileListRequest; |
12
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile; |
13
|
|
|
use Yoanm\ComposerConfigManager\Infrastructure\Command\Transformer\InputTransformer; |
14
|
|
|
|
15
|
|
|
class CreateConfigurationCommand extends AbstractTemplatableCommand |
16
|
|
|
{ |
17
|
|
|
const NAME = 'create'; |
18
|
|
|
const ARGUMENT_CONFIGURATION_DEST_FOLDER = 'destination'; |
19
|
|
|
|
20
|
|
|
/** @var InputTransformer */ |
21
|
|
|
private $inputTransformer; |
22
|
|
|
/** @var UpdateConfigurationFileList */ |
23
|
|
|
private $updateConfigurationFile; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param InputTransformer $inputTransformer |
27
|
|
|
* @param UpdateConfigurationFileList $updateConfigurationFile |
28
|
|
|
* @param ConfigurationFileLoaderInterface $configurationLoader |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
InputTransformer $inputTransformer, |
32
|
|
|
UpdateConfigurationFileList $updateConfigurationFile, |
33
|
|
|
ConfigurationFileLoaderInterface $configurationLoader |
34
|
|
|
) { |
35
|
|
|
parent::__construct($configurationLoader); |
36
|
|
|
|
37
|
|
|
$this->inputTransformer = $inputTransformer; |
38
|
|
|
$this->updateConfigurationFile = $updateConfigurationFile; |
39
|
|
|
} |
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function configure() |
44
|
|
|
{ |
45
|
|
|
$this |
46
|
|
|
->setName(self::NAME) |
47
|
|
|
->setDescription('Will create a composer configuration file') |
48
|
|
|
->addArgument( |
49
|
|
|
self::ARGUMENT_CONFIGURATION_DEST_FOLDER, |
50
|
|
|
InputArgument::OPTIONAL, |
51
|
|
|
'Configuration file destination folder', |
52
|
|
|
'.' |
53
|
|
|
) |
54
|
|
|
->addArgument( |
55
|
|
|
InputTransformer::KEY_PACKAGE_NAME, |
56
|
|
|
InputArgument::OPTIONAL, |
57
|
|
|
'Name for the composer package. Optionnal if a template containing package-name is given.' |
58
|
|
|
) |
59
|
|
|
->addOption( |
60
|
|
|
InputTransformer::KEY_TYPE, |
61
|
|
|
null, |
62
|
|
|
InputOption::VALUE_REQUIRED, |
63
|
|
|
'Package type. Ex : "library" / "project"' |
64
|
|
|
) |
65
|
|
|
->addOption( |
66
|
|
|
InputTransformer::KEY_LICENSE, |
67
|
|
|
null, |
68
|
|
|
InputOption::VALUE_REQUIRED, |
69
|
|
|
'Package license type' |
70
|
|
|
) |
71
|
|
|
->addOption( |
72
|
|
|
InputTransformer::KEY_PACKAGE_VERSION, |
73
|
|
|
null, |
74
|
|
|
InputOption::VALUE_REQUIRED, |
75
|
|
|
'Package version number. Ex : "X.Y.Z"' |
76
|
|
|
) |
77
|
|
|
->addOption( |
78
|
|
|
InputTransformer::KEY_DESCRIPTION, |
79
|
|
|
null, |
80
|
|
|
InputOption::VALUE_REQUIRED, |
81
|
|
|
'Package description' |
82
|
|
|
) |
83
|
|
|
->addOption( |
84
|
|
|
InputTransformer::KEY_KEYWORD, |
85
|
|
|
null, |
86
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
87
|
|
|
'Package keywords' |
88
|
|
|
) |
89
|
|
|
->addOption( |
90
|
|
|
InputTransformer::KEY_AUTHOR, |
91
|
|
|
null, |
92
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
93
|
|
|
'Package authors. Format "name[#email[#role]]' |
94
|
|
|
) |
95
|
|
|
->addOption( |
96
|
|
|
InputTransformer::KEY_PROVIDED_PACKAGE, |
97
|
|
|
null, |
98
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
99
|
|
|
'List of packages provided by this one. Ex : "package-name#version"' |
100
|
|
|
) |
101
|
|
|
->addOption( |
102
|
|
|
InputTransformer::KEY_SUGGESTED_PACKAGE, |
103
|
|
|
null, |
104
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
105
|
|
|
'List of packages suggested by this one. Ex : "package-name#description"' |
106
|
|
|
) |
107
|
|
|
->addOption( |
108
|
|
|
InputTransformer::KEY_SUPPORT, |
109
|
|
|
null, |
110
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
111
|
|
|
'List of package support urls. Ex : "type#url"' |
112
|
|
|
) |
113
|
|
|
->addOption( |
114
|
|
|
InputTransformer::KEY_AUTOLOAD_PSR0, |
115
|
|
|
null, |
116
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
117
|
|
|
'List of PSR-0 autoload. Ex : "namespace#path"' |
118
|
|
|
) |
119
|
|
|
->addOption( |
120
|
|
|
InputTransformer::KEY_AUTOLOAD_PSR4, |
121
|
|
|
null, |
122
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
123
|
|
|
'List of PSR-4 autoload. Ex : "namespace#path"' |
124
|
|
|
) |
125
|
|
|
->addOption( |
126
|
|
|
InputTransformer::KEY_AUTOLOAD_DEV_PSR0, |
127
|
|
|
null, |
128
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
129
|
|
|
'List of PSR-0 dev autoload. Ex : "namespace#path"' |
130
|
|
|
) |
131
|
|
|
->addOption( |
132
|
|
|
InputTransformer::KEY_AUTOLOAD_DEV_PSR4, |
133
|
|
|
null, |
134
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
135
|
|
|
'List of PSR-4 dev autoload. Ex : "namespace#path"' |
136
|
|
|
) |
137
|
|
|
->addOption( |
138
|
|
|
InputTransformer::KEY_REQUIRE, |
139
|
|
|
null, |
140
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
141
|
|
|
'List of required packages. Ex "vendor/package-name#~x.y"' |
142
|
|
|
) |
143
|
|
|
->addOption( |
144
|
|
|
InputTransformer::KEY_REQUIRE_DEV, |
145
|
|
|
null, |
146
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
147
|
|
|
'List of required dev packages. Ex "vendor/package-name#~x.y"' |
148
|
|
|
) |
149
|
|
|
->addOption( |
150
|
|
|
InputTransformer::KEY_SCRIPT, |
151
|
|
|
null, |
152
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
153
|
|
|
'List of scripts for the package. Ex : "script-name#command"' |
154
|
|
|
) |
155
|
|
|
; |
156
|
|
|
parent::configure(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
163
|
|
|
{ |
164
|
|
|
$configurationFileList = []; |
165
|
|
|
$templateConfigurationFile = $this->loadTemplateConfigurationFile($input); |
166
|
|
|
if ($templateConfigurationFile) { |
167
|
|
|
$configurationFileList[] = $templateConfigurationFile; |
168
|
|
|
} |
169
|
|
|
if ($newConfigurationFile = $this->loadConfigurationFile($input, $templateConfigurationFile)) { |
170
|
|
|
$configurationFileList[] = $newConfigurationFile; |
171
|
|
|
} |
172
|
|
|
$this->updateConfigurationFile->run( |
173
|
|
|
new UpdateConfigurationFileListRequest( |
174
|
|
|
$configurationFileList, |
175
|
|
|
$input->getArgument(self::ARGUMENT_CONFIGURATION_DEST_FOLDER) |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param InputInterface $input |
182
|
|
|
* @param ConfigurationFile|null $templateConfigurationFile |
183
|
|
|
* |
184
|
|
|
* @return null|ConfigurationFile |
185
|
|
|
*/ |
186
|
|
|
protected function loadConfigurationFile(InputInterface $input, ConfigurationFile $templateConfigurationFile = null) |
187
|
|
|
{ |
188
|
|
|
$packageName = $input->getArgument(InputTransformer::KEY_PACKAGE_NAME); |
189
|
|
|
if (null === $packageName) { |
190
|
|
|
if (null === $templateConfigurationFile |
191
|
|
|
|| '' === trim($templateConfigurationFile->getConfiguration()->getPackageName()) |
192
|
|
|
) { |
193
|
|
|
throw new InvalidArgumentException( |
194
|
|
|
sprintf( |
195
|
|
|
'A package name must be given if no template containing package name is given !', |
196
|
|
|
gettype($packageName) |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return null; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->inputTransformer->fromCommandLine( |
205
|
|
|
[ |
206
|
|
|
InputTransformer::KEY_PACKAGE_NAME => $packageName |
207
|
|
|
] + $input->getOptions() |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|