|
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\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
|
|
22
|
|
|
abstract class AbstractObjectOutputCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var JsonConverterInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $jsonConverter; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* AbstractGeneratorCommand constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param JsonConverterInterface $jsonConverter |
|
33
|
|
|
* @param string|null $name |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(JsonConverterInterface $jsonConverter, string $name = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->jsonConverter = $jsonConverter; |
|
38
|
|
|
parent::__construct($name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Configures the current command. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function configure() |
|
45
|
|
|
{ |
|
46
|
|
|
$this |
|
47
|
|
|
->addOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.') |
|
48
|
|
|
; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param InputInterface $input |
|
53
|
|
|
* @param OutputInterface $output |
|
54
|
|
|
* @param \JsonSerializable $json |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function prepareJsonOutput(InputInterface $input, OutputInterface $output, \JsonSerializable $json) |
|
57
|
|
|
{ |
|
58
|
|
|
$json = $this->jsonConverter->encode($json); |
|
59
|
|
|
$this->prepareOutput($input, $output, $json); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param InputInterface $input |
|
64
|
|
|
* @param OutputInterface $output |
|
65
|
|
|
* @param string $data |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function prepareOutput(InputInterface $input, OutputInterface $output, string $data) |
|
68
|
|
|
{ |
|
69
|
|
|
$file = $input->getOption('out'); |
|
70
|
|
|
if (null !== $file) { |
|
71
|
|
|
file_put_contents($file, $data, LOCK_EX); |
|
72
|
|
|
} else { |
|
73
|
|
|
$output->write($data); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|