1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonDiff\Cli; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\JsonDiff\JsonDiff; |
7
|
|
|
use Yaoi\Command; |
8
|
|
|
|
9
|
|
|
abstract class Base extends Command |
10
|
|
|
{ |
11
|
|
|
public $originalPath; |
12
|
|
|
public $newPath; |
13
|
|
|
public $pretty; |
14
|
|
|
public $rearrangeArrays; |
15
|
|
|
|
16
|
|
|
static function setUpDefinition(Command\Definition $definition, $options) |
17
|
|
|
{ |
18
|
|
|
$options->originalPath = Command\Option::create()->setIsUnnamed()->setIsRequired() |
19
|
|
|
->setDescription('Path to old (original) json file'); |
20
|
|
|
$options->newPath = Command\Option::create()->setIsUnnamed()->setIsRequired() |
21
|
|
|
->setDescription('Path to new json file'); |
22
|
|
|
$options->pretty = Command\Option::create() |
23
|
|
|
->setDescription('Pretty-print result JSON'); |
24
|
|
|
$options->rearrangeArrays = Command\Option::create() |
25
|
|
|
->setDescription('Rearrange arrays to match original'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** @var JsonDiff */ |
30
|
|
|
protected $diff; |
31
|
|
|
protected $out; |
32
|
|
|
|
33
|
|
|
protected function prePerform() |
34
|
|
|
{ |
35
|
|
|
$originalJson = file_get_contents($this->originalPath); |
36
|
|
|
if (!$originalJson) { |
37
|
|
|
$this->response->error('Unable to read ' . $this->originalPath); |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$newJson = file_get_contents($this->newPath); |
42
|
|
|
if (!$newJson) { |
43
|
|
|
$this->response->error('Unable to read ' . $this->newPath); |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$options = 0; |
48
|
|
|
if ($this->rearrangeArrays) { |
49
|
|
|
$options += JsonDiff::REARRANGE_ARRAYS; |
50
|
|
|
} |
51
|
|
|
$this->diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), $options); |
52
|
|
|
|
53
|
|
|
$this->out = ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function postPerform() |
57
|
|
|
{ |
58
|
|
|
$options = JSON_UNESCAPED_SLASHES; |
59
|
|
|
if ($this->pretty) { |
60
|
|
|
$options += JSON_PRETTY_PRINT; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$outJson = json_encode($this->out, $options); |
64
|
|
|
$this->response->addContent($outJson); |
65
|
|
|
} |
66
|
|
|
} |