1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\JsonDiff\Exception; |
7
|
|
|
use Swaggest\JsonDiff\JsonPointer; |
8
|
|
|
use Yaoi\Command; |
9
|
|
|
use Yaoi\Command\Definition; |
10
|
|
|
use Yaoi\Io\Content\Rows; |
11
|
|
|
|
12
|
|
|
class ResolvePos extends Command |
13
|
|
|
{ |
14
|
|
|
public $path; |
15
|
|
|
public $pointer; |
16
|
|
|
public $dumpAll; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Definition $definition |
20
|
|
|
* @param \stdClass|static $options |
21
|
|
|
*/ |
22
|
1 |
|
static function setUpDefinition(Definition $definition, $options) |
23
|
|
|
{ |
24
|
1 |
|
$options->path = Command\Option::create()->setType()->setIsUnnamed() |
25
|
1 |
|
->setDescription('Path to JSON file'); |
26
|
1 |
|
$options->pointer = Command\Option::create()->setType()->setIsUnnamed() |
27
|
1 |
|
->setDescription('JSON Pointer, example /key4/1/a'); |
28
|
1 |
|
$options->dumpAll = Command\Option::create()->setDescription('Dump all pointer positions from JSON'); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function performAction() |
32
|
|
|
{ |
33
|
1 |
|
$listener = new FilePosition\PositionResolver(); |
34
|
1 |
|
$stream = fopen($this->path, 'r'); |
35
|
|
|
try { |
36
|
1 |
|
$parser = new \JsonStreamingParser\Parser($stream, $listener); |
|
|
|
|
37
|
1 |
|
$parser->parse(); |
38
|
1 |
|
fclose($stream); |
|
|
|
|
39
|
|
|
} catch (\Exception $e) { |
40
|
|
|
fclose($stream); |
41
|
|
|
$this->response->error($e->getMessage()); |
42
|
|
|
die(1); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if ($this->dumpAll) { |
46
|
|
|
$rows = array(); |
47
|
|
|
foreach ($listener->resolved as $pointer => $pos) { |
48
|
|
|
$rows[] = array( |
49
|
|
|
'Pos' => $pos, |
50
|
|
|
'Ptr' => $pointer, |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
$this->response->addContent(new Rows(new \ArrayIterator($rows))); |
54
|
|
|
} else { |
55
|
|
|
try { |
56
|
|
|
// Convert to non-URI pointer |
57
|
1 |
|
$pointer = JsonPointer::buildPath(JsonPointer::splitPath($this->pointer)); |
58
|
|
|
} catch (Exception $e) { |
59
|
|
|
$this->response->error($e->getMessage()); |
60
|
|
|
die(1); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
if (isset($listener->resolved[$pointer])) { |
64
|
1 |
|
$this->response->addContent($listener->resolved[$pointer]); |
65
|
|
|
} else { |
66
|
|
|
$this->response->error('Pointer not found'); |
67
|
|
|
die(1); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
} |