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