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