|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* import json data into graviton |
|
4
|
|
|
* |
|
5
|
|
|
* Supports importing json data from either a single file or a complete folder of files. |
|
6
|
|
|
* |
|
7
|
|
|
* The data needs to contain frontmatter to hint where the bits and pieces should go. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Graviton\ImportExport\Command; |
|
11
|
|
|
|
|
12
|
|
|
use Graviton\ImportExport\Exception\MissingTargetException; |
|
13
|
|
|
use Graviton\ImportExport\Exception\JsonParseException; |
|
14
|
|
|
use Graviton\ImportExport\Exception\UnknownFileTypeException; |
|
15
|
|
|
use Graviton\ImportExport\Service\HttpClient; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Symfony\Component\Finder\Finder; |
|
21
|
|
|
use Symfony\Component\Yaml\Parser; |
|
22
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
|
23
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper as Dumper; |
|
24
|
|
|
use GuzzleHttp\Promise; |
|
25
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
26
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
27
|
|
|
use Webuni\FrontMatter\FrontMatter; |
|
28
|
|
|
use Webuni\FrontMatter\Document; |
|
29
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
|
33
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
34
|
|
|
* @link http://swisscom.ch |
|
35
|
|
|
*/ |
|
36
|
|
|
class ImportCommand extends ImportCommandAbstract |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var HttpClient |
|
40
|
|
|
*/ |
|
41
|
|
|
private $client; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var FrontMatter |
|
45
|
|
|
*/ |
|
46
|
|
|
private $frontMatter; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Parser |
|
50
|
|
|
*/ |
|
51
|
|
|
private $parser; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var VarCloner |
|
55
|
|
|
*/ |
|
56
|
|
|
private $cloner; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Dumper |
|
60
|
|
|
*/ |
|
61
|
|
|
private $dumper; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param HttpClient $client Grv HttpClient guzzle http client |
|
65
|
|
|
* @param Finder $finder symfony/finder instance |
|
66
|
|
|
* @param FrontMatter $frontMatter frontmatter parser |
|
67
|
|
|
* @param Parser $parser yaml/json parser |
|
68
|
|
|
* @param VarCloner $cloner var cloner for dumping reponses |
|
69
|
|
|
* @param Dumper $dumper dumper for outputing responses |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function __construct( |
|
72
|
|
|
HttpClient $client, |
|
73
|
|
|
Finder $finder, |
|
74
|
|
|
FrontMatter $frontMatter, |
|
75
|
|
|
Parser $parser, |
|
76
|
|
|
VarCloner $cloner, |
|
77
|
|
|
Dumper $dumper |
|
78
|
|
|
) { |
|
79
|
4 |
|
parent::__construct( |
|
80
|
|
|
$finder |
|
81
|
4 |
|
); |
|
82
|
4 |
|
$this->client = $client; |
|
83
|
4 |
|
$this->frontMatter = $frontMatter; |
|
84
|
4 |
|
$this->parser = $parser; |
|
85
|
4 |
|
$this->cloner = $cloner; |
|
86
|
4 |
|
$this->dumper = $dumper; |
|
87
|
4 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Configures the current command. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
4 |
|
protected function configure() |
|
95
|
|
|
{ |
|
96
|
4 |
|
$this |
|
97
|
4 |
|
->setName('graviton:import') |
|
98
|
4 |
|
->setDescription('Import files from a folder or file.') |
|
99
|
4 |
|
->addOption( |
|
100
|
4 |
|
'rewrite-host', |
|
101
|
4 |
|
'r', |
|
102
|
4 |
|
InputOption::VALUE_OPTIONAL, |
|
103
|
4 |
|
'Replace the value of this option with the <host> value before importing.', |
|
104
|
|
|
'http://localhost' |
|
105
|
4 |
|
) |
|
106
|
4 |
|
->addOption( |
|
107
|
4 |
|
'rewrite-to', |
|
108
|
4 |
|
't', |
|
109
|
4 |
|
InputOption::VALUE_OPTIONAL, |
|
110
|
4 |
|
'String to use as the replacement value for the [REWRITE-HOST] string.', |
|
111
|
|
|
'<host>' |
|
112
|
4 |
|
) |
|
113
|
4 |
|
->addOption( |
|
114
|
4 |
|
'sync-requests', |
|
115
|
4 |
|
's', |
|
116
|
4 |
|
InputOption::VALUE_NONE, |
|
117
|
|
|
'Send requests synchronously' |
|
118
|
4 |
|
) |
|
119
|
4 |
|
->addArgument( |
|
120
|
4 |
|
'host', |
|
121
|
4 |
|
InputArgument::REQUIRED, |
|
122
|
|
|
'Protocol and host to load data into (ie. https://graviton.nova.scapp.io)' |
|
123
|
4 |
|
) |
|
124
|
4 |
|
->addArgument( |
|
125
|
4 |
|
'file', |
|
126
|
4 |
|
InputArgument::REQUIRED + InputArgument::IS_ARRAY, |
|
127
|
|
|
'Directories or files to load' |
|
128
|
4 |
|
); |
|
129
|
4 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Executes the current command. |
|
133
|
|
|
* |
|
134
|
|
|
* @param Finder $finder Finder |
|
135
|
|
|
* @param InputInterface $input User input on console |
|
136
|
|
|
* @param OutputInterface $output Output of the command |
|
137
|
|
|
* |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
4 |
|
protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output) |
|
141
|
|
|
{ |
|
142
|
4 |
|
$host = $input->getArgument('host'); |
|
143
|
4 |
|
$rewriteHost = $input->getOption('rewrite-host'); |
|
144
|
4 |
|
$rewriteTo = $input->getOption('rewrite-to'); |
|
145
|
4 |
|
if ($rewriteTo === $this->getDefinition()->getOption('rewrite-to')->getDefault()) { |
|
146
|
4 |
|
$rewriteTo = $host; |
|
147
|
4 |
|
} |
|
148
|
4 |
|
$sync = $input->getOption('sync-requests'); |
|
149
|
|
|
|
|
150
|
4 |
|
$this->importPaths($finder, $output, $host, $rewriteHost, $rewriteTo, $sync); |
|
151
|
2 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param Finder $finder finder primmed with files to import |
|
155
|
|
|
* @param OutputInterface $output output interfac |
|
156
|
|
|
* @param string $host host to import into |
|
157
|
|
|
* @param string $rewriteHost string to replace with value from $rewriteTo during loading |
|
158
|
|
|
* @param string $rewriteTo string to replace value from $rewriteHost with during loading |
|
159
|
|
|
* @param boolean $sync send requests syncronously |
|
160
|
|
|
* |
|
161
|
|
|
* @return void |
|
162
|
|
|
* |
|
163
|
|
|
* @throws MissingTargetException |
|
164
|
|
|
*/ |
|
165
|
4 |
|
protected function importPaths( |
|
166
|
|
|
Finder $finder, |
|
167
|
|
|
OutputInterface $output, |
|
168
|
|
|
$host, |
|
169
|
|
|
$rewriteHost, |
|
170
|
|
|
$rewriteTo, |
|
171
|
|
|
$sync = false |
|
172
|
|
|
) { |
|
173
|
4 |
|
$promises = []; |
|
174
|
4 |
|
foreach ($finder as $file) { |
|
175
|
4 |
|
$doc = $this->frontMatter->parse($file->getContents()); |
|
176
|
|
|
|
|
177
|
4 |
|
$output->writeln("<info>Loading data from ${file}</info>"); |
|
178
|
|
|
|
|
179
|
4 |
|
if (!array_key_exists('target', $doc->getData())) { |
|
180
|
1 |
|
throw new MissingTargetException('Missing target in \'' . $file . '\''); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
3 |
|
$targetUrl = sprintf('%s%s', $host, $doc->getData()['target']); |
|
184
|
|
|
|
|
185
|
3 |
|
$promises[] = $this->importResource( |
|
186
|
3 |
|
$targetUrl, |
|
187
|
3 |
|
(string) $file, |
|
188
|
3 |
|
$output, |
|
189
|
3 |
|
$doc, |
|
190
|
3 |
|
$host, |
|
191
|
3 |
|
$rewriteHost, |
|
192
|
3 |
|
$rewriteTo, |
|
193
|
|
|
$sync |
|
194
|
3 |
|
); |
|
195
|
2 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
try { |
|
198
|
|
|
Promise\unwrap($promises); |
|
199
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
|
200
|
|
|
// silently ignored since we already output an error when the promise fails |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param string $targetUrl target url to import resource into |
|
206
|
|
|
* @param string $file path to file being loaded |
|
207
|
|
|
* @param OutputInterface $output output of the command |
|
208
|
|
|
* @param Document $doc document to load |
|
209
|
|
|
* @param string $host host to import into |
|
210
|
|
|
* @param string $rewriteHost string to replace with value from $host during loading |
|
211
|
|
|
* @param string $rewriteTo string to replace value from $rewriteHost with during loading |
|
212
|
|
|
* @param boolean $sync send requests syncronously |
|
213
|
|
|
* |
|
214
|
|
|
* @return Promise\Promise|null |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function importResource( |
|
217
|
|
|
$targetUrl, |
|
218
|
|
|
$file, |
|
219
|
|
|
OutputInterface $output, |
|
220
|
|
|
Document $doc, |
|
221
|
|
|
$host, |
|
|
|
|
|
|
222
|
|
|
$rewriteHost, |
|
223
|
|
|
$rewriteTo, |
|
224
|
|
|
$sync = false |
|
225
|
|
|
) { |
|
226
|
3 |
|
$content = str_replace($rewriteHost, $rewriteTo, $doc->getContent()); |
|
227
|
3 |
|
$uploadFile = $this->validateUploadFile($doc, $file); |
|
228
|
|
|
|
|
229
|
|
|
$successFunc = function (ResponseInterface $response) use ($output) { |
|
230
|
1 |
|
$output->writeln( |
|
231
|
1 |
|
'<comment>Wrote ' . $response->getHeader('Link')[0] . '</comment>' |
|
232
|
1 |
|
); |
|
233
|
3 |
|
}; |
|
234
|
|
|
|
|
235
|
|
|
$errFunc = function (RequestException $e) use ($output, $file) { |
|
236
|
1 |
|
$output->writeln( |
|
237
|
1 |
|
'<error>' . str_pad( |
|
238
|
1 |
|
sprintf( |
|
239
|
1 |
|
'Failed to write <%s> from \'%s\' with message \'%s\'', |
|
240
|
1 |
|
$e->getRequest()->getUri(), |
|
241
|
1 |
|
$file, |
|
242
|
1 |
|
$e->getMessage() |
|
243
|
1 |
|
), |
|
244
|
1 |
|
140, |
|
245
|
|
|
' ' |
|
246
|
1 |
|
) . '</error>' |
|
247
|
1 |
|
); |
|
248
|
1 |
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
|
249
|
1 |
|
$this->dumper->dump( |
|
250
|
1 |
|
$this->cloner->cloneVar( |
|
251
|
1 |
|
$this->parser->parse($e->getResponse()->getBody(), false, false, true) |
|
252
|
1 |
|
), |
|
253
|
|
|
function ($line, $depth) use ($output) { |
|
254
|
1 |
|
if ($depth > 0) { |
|
255
|
1 |
|
$output->writeln( |
|
256
|
1 |
|
'<error>' . str_pad(str_repeat(' ', $depth) . $line, 140, ' ') . '</error>' |
|
257
|
1 |
|
); |
|
258
|
1 |
|
} |
|
259
|
1 |
|
} |
|
260
|
1 |
|
); |
|
261
|
1 |
|
} |
|
262
|
3 |
|
}; |
|
263
|
|
|
|
|
264
|
3 |
|
if ($sync === false) { |
|
265
|
3 |
|
$promise = $this->client->requestAsync( |
|
266
|
3 |
|
'PUT', |
|
267
|
3 |
|
$targetUrl, |
|
268
|
|
|
[ |
|
269
|
3 |
|
'json' => $this->parseContent($content, $file), |
|
270
|
|
|
'upload' => $uploadFile |
|
271
|
3 |
|
] |
|
272
|
3 |
|
); |
|
273
|
2 |
|
$promise->then($successFunc, $errFunc); |
|
274
|
2 |
|
} else { |
|
275
|
|
|
$promise = new Promise\Promise; |
|
276
|
|
|
try { |
|
277
|
|
|
$promise->resolve( |
|
278
|
|
|
$successFunc( |
|
279
|
|
|
$this->client->request( |
|
280
|
|
|
'PUT', |
|
281
|
|
|
$targetUrl, |
|
282
|
|
|
[ |
|
283
|
|
|
'json' => $this->parseContent($content, $file), |
|
284
|
|
|
'upload' => $uploadFile |
|
285
|
|
|
] |
|
286
|
|
|
) |
|
287
|
|
|
) |
|
288
|
|
|
); |
|
289
|
|
|
} catch (BadResponseException $e) { |
|
290
|
|
|
$promise->resolve( |
|
291
|
|
|
$errFunc($e) |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
2 |
|
return $promise; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* parse contents of a file depending on type |
|
300
|
|
|
* |
|
301
|
|
|
* @param string $content contents part of file |
|
302
|
|
|
* @param string $file full path to file |
|
303
|
|
|
* |
|
304
|
|
|
* @return mixed |
|
305
|
|
|
*/ |
|
306
|
|
|
protected function parseContent($content, $file) |
|
307
|
|
|
{ |
|
308
|
3 |
|
if (substr($file, -5) == '.json') { |
|
309
|
3 |
|
$data = json_decode($content); |
|
310
|
3 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
311
|
|
|
throw new JsonParseException( |
|
312
|
|
|
sprintf( |
|
313
|
|
|
'%s in %s', |
|
314
|
|
|
json_last_error_msg(), |
|
315
|
|
|
$file |
|
316
|
|
|
) |
|
317
|
|
|
); |
|
318
|
|
|
} |
|
319
|
3 |
|
} elseif (substr($file, -4) == '.yml') { |
|
320
|
|
|
$data = $this->parser->parse($content); |
|
321
|
|
|
} else { |
|
322
|
|
|
throw new UnknownFileTypeException($file); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
3 |
|
return $data; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Checks if file exists and return qualified fileName location |
|
330
|
|
|
* |
|
331
|
|
|
* @param Document $doc Data source for import data |
|
332
|
|
|
* @param string $originFile Original full filename used toimport |
|
333
|
|
|
* @return bool|mixed |
|
334
|
|
|
*/ |
|
335
|
|
|
private function validateUploadFile(Document $doc, $originFile) |
|
336
|
|
|
{ |
|
337
|
3 |
|
$documentData = $doc->getData(); |
|
338
|
|
|
|
|
339
|
3 |
|
if (!array_key_exists('file', $documentData)) { |
|
340
|
3 |
|
return false; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
// Find file |
|
344
|
|
|
$fileName = preg_replace('/([^\/]+$)/', $documentData['file'], $originFile); |
|
345
|
|
|
$fileName = str_replace('//', '/', $fileName); |
|
346
|
|
|
if (!file_exists($fileName)) { |
|
347
|
|
|
return false; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
return $fileName; |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.