1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonDiff\JsonValueReplace; |
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
7
|
|
|
use Yaoi\Command; |
8
|
|
|
|
9
|
|
|
class Replace extends Command |
10
|
|
|
{ |
11
|
|
|
public $path; |
12
|
|
|
public $output; |
13
|
|
|
|
14
|
|
|
public $search; |
15
|
|
|
public $replace; |
16
|
|
|
public $pathFilter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Command\Definition $definition |
20
|
|
|
* @param \stdClass|static $options |
21
|
|
|
*/ |
22
|
1 |
|
static function setUpDefinition(Command\Definition $definition, $options) |
23
|
|
|
{ |
24
|
1 |
|
$options->path = Command\Option::create()->setIsUnnamed()->setIsRequired() |
25
|
1 |
|
->setDescription('Path to JSON/YAML file'); |
26
|
1 |
|
$options->search = Command\Option::create()->setIsUnnamed()->setIsRequired() |
27
|
1 |
|
->setDescription('Search JSON value'); |
28
|
1 |
|
$options->replace = Command\Option::create()->setIsUnnamed()->setIsRequired() |
29
|
1 |
|
->setDescription('Replace JSON value'); |
30
|
1 |
|
$options->pathFilter = Command\Option::create()->setType() |
31
|
1 |
|
->setDescription('JSON path filter regex, example "/definitions/.*/properties/deletedAt"'); |
32
|
1 |
|
$options->output = Command\Option::create()->setType() |
33
|
1 |
|
->setDescription('Path to output, default STDOUT'); |
34
|
1 |
|
$definition->description = 'Minify JSON document'; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
public function performAction() |
38
|
|
|
{ |
39
|
|
|
if (!file_exists($this->path)) { |
40
|
|
|
$this->response->error('File not found: ' . $this->path); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
$fileData = file_get_contents($this->path); |
44
|
|
|
if (!$fileData) { |
45
|
|
|
$this->response->error('Unable to read ' . $this->path); |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
if (substr($this->path, -5) === '.yaml' || substr($this->path, -4) === '.yml') { |
49
|
|
|
$jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT); |
50
|
|
|
} else { |
51
|
|
|
$jsonData = json_decode($fileData); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$search = json_decode($this->search); |
55
|
|
|
if (json_last_error()) { |
56
|
|
|
$this->response->error('Invalid JSON: ' . $this->search); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
$replace = json_decode($this->replace); |
60
|
|
|
if (json_last_error()) { |
61
|
|
|
$this->response->error('Invalid JSON: ' . $this->replace); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$pathFilter = null; |
66
|
|
|
if ($this->pathFilter) { |
67
|
|
|
$pathFilter = '|' . $this->pathFilter . '|'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$replacer = new JsonValueReplace($search, $replace, $pathFilter); |
71
|
|
|
$jsonData = $replacer->process($jsonData); |
72
|
|
|
|
73
|
|
|
$result = json_encode($jsonData, JSON_UNESCAPED_SLASHES); |
74
|
|
|
|
75
|
|
|
if ($this->output) { |
76
|
|
|
file_put_contents($this->output, $result); |
77
|
|
|
} else { |
78
|
|
|
$this->response->addContent($result); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |