1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Console\Command; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Core\Converter\JsonConverterInterface; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
abstract class AbstractJsonObjectOutputCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var JsonConverterInterface |
27
|
|
|
*/ |
28
|
|
|
protected $jsonConverter; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AbstractGeneratorCommand constructor. |
32
|
|
|
* |
33
|
|
|
* @param JsonConverterInterface $jsonConverter |
34
|
|
|
* @param string|null $name |
35
|
|
|
*/ |
36
|
|
|
public function __construct(JsonConverterInterface $jsonConverter, string $name = null) |
37
|
|
|
{ |
38
|
|
|
$this->jsonConverter = $jsonConverter; |
39
|
|
|
parent::__construct($name); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Configures the current command. |
44
|
|
|
*/ |
45
|
|
|
protected function configure() |
46
|
|
|
{ |
47
|
|
|
$this |
48
|
|
|
->setDefinition( |
49
|
|
|
new InputDefinition([ |
50
|
|
|
new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'), |
51
|
|
|
]) |
52
|
|
|
) |
53
|
|
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param InputInterface $input |
58
|
|
|
* @param OutputInterface $output |
59
|
|
|
* @param \JsonSerializable $json |
60
|
|
|
*/ |
61
|
|
|
protected function prepareOutput(InputInterface $input, OutputInterface $output, \JsonSerializable $json) |
62
|
|
|
{ |
63
|
|
|
$json = $this->jsonConverter->encode($json); |
64
|
|
|
$file = $input->getOption('out'); |
65
|
|
|
if (null !== $file) { |
66
|
|
|
file_put_contents($file, $json, LOCK_EX); |
67
|
|
|
} else { |
68
|
|
|
$output->write($json); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|