1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/composer-yaml-plugin package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webuni\ComposerYamlPlugin; |
14
|
|
|
|
15
|
|
|
use Composer\Command\BaseCommand; |
16
|
|
|
use Composer\Json\JsonFile; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Yaml\Yaml; |
21
|
|
|
|
22
|
|
|
final class YamlConvertCommand extends BaseCommand |
23
|
|
|
{ |
24
|
|
|
private $formats = [ |
25
|
|
|
'yaml' => 'yaml', |
26
|
|
|
'yml' => 'yaml', |
27
|
|
|
'json' => 'json', |
28
|
|
|
]; |
29
|
|
|
|
30
|
7 |
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
7 |
|
->setName('yaml-convert') |
34
|
7 |
|
->setDescription('Converts a composer.yml to json or vice-versa') |
35
|
7 |
|
->addArgument('input', InputArgument::OPTIONAL, 'The input file', 'composer.yml') |
36
|
7 |
|
->addArgument('output', InputArgument::OPTIONAL, 'The output file', 'composer.json') |
37
|
|
|
; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$data = [ |
43
|
6 |
|
'input' => $input->getArgument('input'), |
44
|
6 |
|
'output' => $input->getArgument('output'), |
45
|
|
|
]; |
46
|
|
|
|
47
|
6 |
|
if (!is_file($data['input'])) { |
48
|
3 |
|
throw new \InvalidArgumentException(sprintf('The input file "%s" does not exist.', $data['input'])); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$formats = $this->getFormats($data); |
52
|
|
|
|
53
|
3 |
|
if ($formats['input'] === $formats['output']) { |
54
|
1 |
|
throw new \InvalidArgumentException('Input format is same as output format.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$content = file_get_contents($data['input']); |
58
|
|
|
|
59
|
2 |
|
if ('json' === $formats['input']) { |
60
|
1 |
|
$converted = Yaml::dump(JsonFile::parseJson($content)); |
61
|
|
|
} else { |
62
|
1 |
|
$converted = JsonFile::encode(Yaml::parse($content)); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
file_put_contents($data['output'], $converted); |
66
|
2 |
|
$output->writeln(sprintf('Converted "%s" to "%s"', $data['input'], $data['output'])); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
3 |
|
private function getFormats(array $data) |
70
|
|
|
{ |
71
|
3 |
|
$formats = []; |
72
|
3 |
|
foreach ($data as $type => $file) { |
73
|
3 |
|
$format = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
74
|
3 |
|
if (!isset($this->formats[$format])) { |
75
|
|
|
throw new \InvalidArgumentException(sprintf( |
76
|
|
|
'Invalid %s format "%s", must be one of: %s', |
77
|
|
|
$type, $format, implode(', ', array_keys($this->formats)) |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
$formats[$type] = $this->formats[$format]; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
return $formats; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|