1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonDiff\Cli; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonDiff\JsonDiff; |
6
|
|
|
use Yaoi\Command; |
7
|
|
|
use Yaoi\Command\Definition; |
8
|
|
|
|
9
|
|
|
class Diff extends Command |
10
|
|
|
{ |
11
|
|
|
const ACTION_REARRANGE = 'rearrange'; |
12
|
|
|
const ACTION_CHANGES = 'changes'; |
13
|
|
|
const ACTION_REMOVALS = 'removals'; |
14
|
|
|
const ACTION_ADDITIONS = 'additions'; |
15
|
|
|
const ACTION_MODIFICATIONS = 'modifications'; |
16
|
|
|
|
17
|
|
|
public $action; |
18
|
|
|
public $originalPath; |
19
|
|
|
public $newPath; |
20
|
|
|
public $out; |
21
|
|
|
public $showPaths; |
22
|
|
|
public $showJson; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Definition $definition |
27
|
|
|
* @param \stdClass|static $options |
28
|
|
|
*/ |
29
|
|
|
static function setUpDefinition(Definition $definition, $options) |
30
|
|
|
{ |
31
|
|
|
$definition->name = 'json-diff'; |
32
|
|
|
$definition->version = 'v1.0.0'; |
33
|
|
|
$definition->description = 'JSON diff and rearrange tool for PHP, https://github.com/swaggest/json-diff'; |
34
|
|
|
|
35
|
|
|
$options->action = Command\Option::create()->setIsUnnamed()->setIsRequired() |
36
|
|
|
->setDescription('Action to perform') |
37
|
|
|
->addToEnum(self::ACTION_REARRANGE) |
38
|
|
|
->addToEnum(self::ACTION_CHANGES) |
39
|
|
|
->addToEnum(self::ACTION_REMOVALS) |
40
|
|
|
->addToEnum(self::ACTION_ADDITIONS) |
41
|
|
|
->addToEnum(self::ACTION_MODIFICATIONS); |
42
|
|
|
|
43
|
|
|
$options->originalPath = Command\Option::create()->setIsUnnamed()->setIsRequired() |
44
|
|
|
->setDescription('Path to old (original) json file'); |
45
|
|
|
$options->newPath = Command\Option::create()->setIsUnnamed()->setIsRequired() |
46
|
|
|
->setDescription('Path to new json file'); |
47
|
|
|
$options->out = Command\Option::create()->setType() |
48
|
|
|
->setDescription('Path to output result json file, STDOUT if not specified'); |
49
|
|
|
$options->showPaths = Command\Option::create()->setDescription('Show JSON paths'); |
50
|
|
|
$options->showJson = Command\Option::create()->setDescription('Show JSON result'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function performAction() |
54
|
|
|
{ |
55
|
|
|
$originalJson = file_get_contents($this->originalPath); |
56
|
|
|
if (!$originalJson) { |
57
|
|
|
$this->response->error('Unable to read ' . $this->originalPath); |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$newJson = file_get_contents($this->newPath); |
62
|
|
|
if (!$newJson) { |
63
|
|
|
$this->response->error('Unable to read ' . $this->newPath); |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$p = new JsonDiff(json_decode($originalJson), json_decode($newJson)); |
68
|
|
|
|
69
|
|
|
$out = ''; |
70
|
|
|
$paths = false; |
71
|
|
|
|
72
|
|
|
switch ($this->action) { |
73
|
|
|
case self::ACTION_REARRANGE: |
74
|
|
|
$out = $p->getRearranged(); |
75
|
|
|
break; |
76
|
|
|
case self::ACTION_REMOVALS: |
77
|
|
|
$out = $p->getRemoved(); |
78
|
|
|
$paths = $p->getRemovedPaths(); |
79
|
|
|
break; |
80
|
|
|
case self::ACTION_ADDITIONS: |
81
|
|
|
$out = $p->getAdded(); |
82
|
|
|
$paths = $p->getAddedPaths(); |
83
|
|
|
break; |
84
|
|
|
case self::ACTION_MODIFICATIONS: |
85
|
|
|
$out = array('modifiedOriginal' => $p->getModifiedOriginal(), 'modifiedNew' => $p->getModifiedNew()); |
86
|
|
|
$paths = $p->getModifiedPaths(); |
87
|
|
|
break; |
88
|
|
|
case self::ACTION_CHANGES: |
89
|
|
|
$out = array( |
90
|
|
|
'removals' => $p->getRemoved(), |
91
|
|
|
'additions' => $p->getAdded(), |
92
|
|
|
'modifiedOriginal' => $p->getModifiedOriginal(), |
93
|
|
|
'modifiedNew' => $p->getModifiedNew() |
94
|
|
|
); |
95
|
|
|
$paths = array_merge($p->getRemovedPaths(), $p->getAddedPaths(), $p->getModifiedPaths()); |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($paths && $this->showPaths) { |
100
|
|
|
echo implode("\n", $paths), "\n"; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$outJson = json_encode($out, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES); |
104
|
|
|
if ($this->out) { |
105
|
|
|
file_put_contents($this->out, $outJson); |
106
|
|
|
} else { |
107
|
|
|
if ($this->showJson) { |
108
|
|
|
echo $outJson, "\n"; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |