|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rs\XmlFilter\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Rs\XmlFilter\Loader\JsonLoader; |
|
8
|
|
|
use Rs\XmlFilter\Loader\Loader; |
|
9
|
|
|
use Rs\XmlFilter\Loader\YamlLoader; |
|
10
|
|
|
use Rs\XmlFilter\XmlFilter; |
|
11
|
|
|
use Symfony\Component\Console\Command\Command; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
17
|
|
|
|
|
18
|
|
|
class FilterCommand extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var XmlFilter |
|
22
|
|
|
*/ |
|
23
|
|
|
private $filter; |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(XmlFilter $filter) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->filter = $filter; |
|
28
|
|
|
|
|
29
|
1 |
|
parent::__construct(); |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function configure() |
|
33
|
|
|
{ |
|
34
|
|
|
$this |
|
35
|
1 |
|
->setName('filter') |
|
36
|
1 |
|
->setDescription('filters an xml file|string|stdin with a given config and returns json or dumps the result') |
|
37
|
1 |
|
->setDefinition([ |
|
38
|
1 |
|
new InputOption('input', null, InputOption::VALUE_REQUIRED, 'which input to use [file|string|stdin]', 'stdin'), |
|
39
|
1 |
|
new InputOption('output', null, InputOption::VALUE_REQUIRED, 'which output format to use [json|dump]', 'dump'), |
|
40
|
1 |
|
new InputArgument('config', InputArgument::REQUIRED, 'the config file'), |
|
41
|
|
|
]) |
|
42
|
|
|
; |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$content = $input->getOption('input'); |
|
48
|
|
|
|
|
49
|
|
|
if ('stdin' === $input->getOption('input')) { |
|
50
|
|
|
$content = stream_get_contents(STDIN); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$document = $this->filter->load($content); |
|
54
|
|
|
$loader = $this->createLoader($input->getArgument('config')); |
|
55
|
|
|
|
|
56
|
|
|
$result = $this->filter->filter($document, $loader); |
|
57
|
|
|
|
|
58
|
|
|
if ('json' === $input->getOption('output')) { |
|
59
|
|
|
return $output->writeln(json_encode($result)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return dump($result); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function createLoader($configFile) : Loader |
|
66
|
|
|
{ |
|
67
|
|
|
json_decode(file_get_contents($configFile)); |
|
68
|
|
|
|
|
69
|
|
|
if (!json_last_error()) { |
|
70
|
|
|
return new JsonLoader($configFile); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
Yaml::parse(file_get_contents($configFile)); |
|
74
|
|
|
|
|
75
|
|
|
return new YamlLoader($configFile); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|