1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Openl10n\Cli\Command; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use Openl10n\Cli\ServiceContainer\Exception\ConfigurationLoadingException; |
7
|
|
|
use Openl10n\Sdk\Model\Project; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
|
13
|
|
|
class InitCommand extends AbstractCommand |
14
|
|
|
{ |
15
|
|
|
protected $configuration = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
protected function configure() |
21
|
|
|
{ |
22
|
|
|
$this |
23
|
|
|
->setName('init') |
24
|
|
|
->setDescription('Create the configuration file and initialize the project') |
25
|
|
|
->setDefinition(array( |
26
|
|
|
new InputArgument('url', InputArgument::OPTIONAL, 'URL of the openl10n instance (eg. http://user:[email protected])'), |
27
|
|
|
new InputArgument('project', InputArgument::OPTIONAL, 'Slug of the project'), |
28
|
|
|
new InputArgument('pattern', InputArgument::OPTIONAL|InputArgument::IS_ARRAY, 'Pattern of the translation files'), |
29
|
|
|
)) |
30
|
|
|
; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
37
|
|
|
{ |
38
|
|
|
// |
39
|
|
|
// First try to read the configuration file if exists |
40
|
|
|
// |
41
|
|
|
$this->getApplication()->ignoreMissingConfiguration(); |
|
|
|
|
42
|
|
|
$configurationLoader = $this->get('configuration.loader'); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
// Init configuration by manually read configuration file (if already exists) |
46
|
|
|
$this->configuration = $configurationLoader->loadConfiguration(); |
47
|
|
|
} catch (ConfigurationLoadingException $e) { |
48
|
|
|
$this->configuration = [ |
49
|
|
|
'server' => [], |
50
|
|
|
'project' => null, |
51
|
|
|
'files' => [], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// |
56
|
|
|
// Then add config from input |
57
|
|
|
// |
58
|
|
|
$server = [ |
59
|
|
|
'scheme' => null, |
60
|
|
|
'user' => null, |
61
|
|
|
'pass' => null, |
62
|
|
|
'port' => null, |
63
|
|
|
'host' => null |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
if (null !== $url = $input->getArgument('url')) { |
67
|
|
|
$urlParts = parse_url($url); |
68
|
|
|
|
69
|
|
|
if (false === $urlParts) { |
70
|
|
|
throw new InvalidArgumentException("$url is not a valid URL"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$server = array_merge($server, $urlParts); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (null !== $hostname = $server['host']) { |
77
|
|
|
$this->configuration['server']['hostname'] = $hostname; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (null !== $scheme = $server['scheme']) { |
81
|
|
|
$this->configuration['server']['use_ssl'] = 'https' === $scheme; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($this->configuration['server']['use_ssl']) && false === $this->configuration['server']['use_ssl']) { |
85
|
|
|
unset($this->configuration['server']['use_ssl']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
if (null !== $port = $server['port']) { |
|
|
|
|
89
|
|
|
$this->configuration['server']['port'] = $port; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (null !== $username = $server['user']) { |
93
|
|
|
$this->configuration['server']['username'] = $username; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
if (null !== $password = $server['pass']) { |
|
|
|
|
97
|
|
|
$this->configuration['server']['password'] = $password; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (null !== $projectSlug = $input->getArgument('project')) { |
101
|
|
|
$this->configuration['project'] = $projectSlug; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (array() !== $patterns = $input->getArgument('pattern')) { |
105
|
|
|
$this->configuration['files'] = $patterns; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// If no file are already set, try to find possible ones |
109
|
|
|
if (empty($this->configuration['files'])) { |
110
|
|
|
$inDir = $this->get('configuration.loader')->getRootDirectory(); |
111
|
|
|
$this->configuration['files'] = $this->get('file.pattern_guess')->suggestPatterns($inDir); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// If no pattern found, add example of file pattern |
115
|
|
|
if (empty($this->configuration['files'])) { |
116
|
|
|
$this->configuration['files'][] = 'path/to/translations.<locale>.yml'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
124
|
|
|
{ |
125
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
126
|
|
|
|
127
|
|
|
// Server |
128
|
|
View Code Duplication |
if (!isset($this->configuration['server']['hostname'])) { |
|
|
|
|
129
|
|
|
$this->configuration['server']['hostname'] = $dialog->ask($output, '<info>Hostname</info> [<comment>openl10n.dev</comment>]: ', 'openl10n.dev'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
if (!isset($this->configuration['server']['use_ssl'])) { |
|
|
|
|
133
|
|
|
$this->configuration['server']['use_ssl'] = $dialog->askConfirmation($output, '<info>Enable ssl</info> [<comment>no</comment>]? ', false); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (false === $this->configuration['server']['use_ssl']) { |
137
|
|
|
unset($this->configuration['server']['use_ssl']); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (!isset($this->configuration['server']['username'])) { |
141
|
|
|
$currentUser = get_current_user(); |
142
|
|
|
$this->configuration['server']['username'] = $dialog->ask($output, "<info>Username</info> [<comment>$currentUser</comment>]: ", $currentUser); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!isset($this->configuration['server']['password'])) { |
146
|
|
|
$currentUser = get_current_user(); |
|
|
|
|
147
|
|
|
$this->configuration['server']['password'] = $dialog->askHiddenResponseAndValidate( |
148
|
|
|
$output, |
149
|
|
|
'<info>Password</info> []: ', |
150
|
|
|
function ($answer) { |
151
|
|
|
if ('' === trim($answer)) { |
152
|
|
|
throw new \RuntimeException('The password can not be empty.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $answer; |
156
|
|
|
}, |
157
|
|
|
false, |
158
|
|
|
false |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Project |
163
|
|
|
if (!isset($this->configuration['project'])) { |
164
|
|
|
$project = strtolower(basename(realpath($this->get('configuration.loader')->getRootDirectory()))); |
165
|
|
|
$this->configuration['project'] = $dialog->ask($output, "<info>Project's slug</info> [<comment>$project</comment>]: ", $project); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
173
|
|
|
{ |
174
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
175
|
|
|
$configurationLoader = $this->get('configuration.loader'); |
176
|
|
|
|
177
|
|
|
// Dump configuration |
178
|
|
|
$content = $this->get('configuration.dumper')->dumpConfiguration($this->configuration); |
179
|
|
|
|
180
|
|
|
if ($input->isInteractive()) { |
181
|
|
|
$output->writeln(['', $content]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if (!$dialog->askConfirmation($output, '<info>Do you confirm generation</info> [<comment>yes</comment>]? ')) { |
185
|
|
|
return 1; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
file_put_contents($configurationLoader->getConfigurationFilepath(), $content); |
189
|
|
|
|
190
|
|
|
// Destroy current container to force recreate it with configured service |
191
|
|
|
$this->getApplication()->destroyContainer(); |
|
|
|
|
192
|
|
|
|
193
|
|
|
$projectApi = $this->get('api')->getEntryPoint('project'); |
194
|
|
|
|
195
|
|
|
try { |
196
|
|
|
$projectSlug = $this->configuration['project']; |
197
|
|
|
$project = $projectApi->get($projectSlug); |
|
|
|
|
198
|
|
|
|
199
|
|
|
return; |
200
|
|
|
} catch (ClientException $e) { |
201
|
|
|
if ('404' !== $e->getResponse()->getStatusCode()) { |
202
|
|
|
throw $e; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$output->writeln(''); |
207
|
|
|
if ($dialog->askConfirmation($output, '<info>Would you like to create the project</info> [<comment>yes</comment>]? ')) { |
208
|
|
|
$project = new Project($projectSlug); |
209
|
|
|
|
210
|
|
|
$defaultName = ucfirst($project->getSlug()); |
211
|
|
|
$name = $dialog->ask($output, "<info>Project's name</info> [<comment>$defaultName</comment>]: ", $defaultName); |
212
|
|
|
$project->setName($name); |
213
|
|
|
|
214
|
|
|
$defaultLocale = $dialog->ask($output, "<info>Default locale</info> [<comment>en</comment>]: ", 'en'); |
215
|
|
|
$project->setDefaultLocale($defaultLocale); |
216
|
|
|
|
217
|
|
|
try { |
218
|
|
|
$projectApi->create($project); |
219
|
|
|
} catch (\Exception $e) { |
220
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
221
|
|
|
|
222
|
|
|
return 1; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: