|
@@ -0,0 +1,186 @@
|
|
1
|
+<?php
|
|
2
|
+$apiUrl = 'http://localhost:8001/blog.php';
|
|
3
|
+$debug = true;
|
|
4
|
+
|
|
5
|
+function apiCall($method, $url, $data = false) {
|
|
6
|
+ $ch = curl_init();
|
|
7
|
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
8
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
9
|
+ if ($data) {
|
|
10
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
11
|
+ $headers = array();
|
|
12
|
+ $headers[] = 'Content-Type: application/json';
|
|
13
|
+ $headers[] = 'Content-Length: ' . strlen($data);
|
|
14
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
15
|
+ }
|
|
16
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
17
|
+ $response = curl_exec($ch);
|
|
18
|
+ curl_close($ch);
|
|
19
|
+ return json_decode($response,true);
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+function menu($tags) {
|
|
23
|
+ $html = '<ul>';
|
|
24
|
+ foreach ($tags as $tag) {
|
|
25
|
+ $html.= '<li><a href="?action=list&subject='.$tag['name'].'">'.$tag['name'].'</a></li>';
|
|
26
|
+ }
|
|
27
|
+ $html.= '</ul></div>';
|
|
28
|
+ return $html;
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+function home($definition) {
|
|
32
|
+ $html = 'Nothing';
|
|
33
|
+ return $html;
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+function head() {
|
|
37
|
+ $html = '<html><head></head><body>';
|
|
38
|
+ return $html;
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+function foot($debug) {
|
|
42
|
+ if ($debug) $html = '<hr/><pre>'.$debug.'</pre></body></html>';
|
|
43
|
+ else $html = '';
|
|
44
|
+ return $html;
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+function listRecords($apiUrl,$subject,$field,$id,$references,$related) {
|
|
48
|
+ $filter = '';
|
|
49
|
+ $html = '';
|
|
50
|
+ if ($field) {
|
|
51
|
+ $filter = '?filter[]='.$field.',eq,'.$id;
|
|
52
|
+ $html .= '<p>filtered where "'.$field.'" = "'.$id.'".';
|
|
53
|
+ $href = '?action=list&subject='.$subject;
|
|
54
|
+ $html .= ' <a href="'.$href.'">remove</a></p>';
|
|
55
|
+ }
|
|
56
|
+ $data = apiCall('GET',$apiUrl.'/'.$subject.$filter );
|
|
57
|
+ $html.= '<table>';
|
|
58
|
+ $html.= '<tr>';
|
|
59
|
+ foreach ($data[$subject]['columns'] as $column) {
|
|
60
|
+ $html.= '<th>'.$column.'</th>';
|
|
61
|
+ }
|
|
62
|
+ $html.= '<th>related</th>';
|
|
63
|
+ $html.= '</tr>';
|
|
64
|
+ foreach ($data[$subject]['records'] as $record) {
|
|
65
|
+ $html.= '<tr>';
|
|
66
|
+ foreach ($record as $i=>$field) {
|
|
67
|
+ if ($references[$i]) {
|
|
68
|
+ $href = '?action=view&subject='.$references[$i].'&id='.$field;
|
|
69
|
+ $html.= '<td><a href="'.$href.'">'.$field.'</a></td>';
|
|
70
|
+ } else {
|
|
71
|
+ $html.= '<td>'.$field.'</td>';
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+ $html.= '<td>';
|
|
75
|
+ foreach ($related as $i=>$relations) {
|
|
76
|
+ $id = $record[$i];
|
|
77
|
+ foreach ($relations as $j=>$relation) {
|
|
78
|
+ if ($j) $html.= ' ';
|
|
79
|
+ $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
|
|
80
|
+ $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ $html.= '</td>';
|
|
84
|
+ $html.= '</tr>';
|
|
85
|
+ }
|
|
86
|
+ $html.= '</table>';
|
|
87
|
+ return $html;
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+function viewRecord($apiUrl,$subject,$id,$references,$related) {
|
|
91
|
+ $data = apiCall('GET',$apiUrl.'/'.$subject.'/'.$id);
|
|
92
|
+ $html = '<table>';
|
|
93
|
+ $i=0;
|
|
94
|
+ foreach ($data as $column=>$field) {
|
|
95
|
+ $html.= '<tr><th>'.$column.'</th>';
|
|
96
|
+ if ($references[$i]) {
|
|
97
|
+ $href = '?action=view&subject='.$references[$i].'&id='.$field;
|
|
98
|
+ $html.= '<td><a href="'.$href.'">'.$field.'</a></td>';
|
|
99
|
+ } else {
|
|
100
|
+ $html.= '<td>'.$field.'</td>';
|
|
101
|
+ }
|
|
102
|
+ $html.= '</tr>';
|
|
103
|
+ $i++;
|
|
104
|
+ }
|
|
105
|
+ $html.= '<tr><th>related</th><td>';
|
|
106
|
+ foreach ($related as $i=>$relations) {
|
|
107
|
+ $keys = array_keys($data);
|
|
108
|
+ $id = $data[$keys[$i]];
|
|
109
|
+ foreach ($relations as $j=>$relation) {
|
|
110
|
+ if ($j) $html.= ' ';
|
|
111
|
+ $href = '?action=list&subject='.$relation[0].'&field='.$relation[1].'&id='.$id;
|
|
112
|
+ $html.= '<a href="'.$href.'">'.$relation[0].'</a>';
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+ $html.= '</td></tr>';
|
|
116
|
+ $html.= '</table>';
|
|
117
|
+ return $html;
|
|
118
|
+}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+function references($subject,$action,$definition) {
|
|
122
|
+ if (!$subject || !$definition) return false;
|
|
123
|
+ if ($action=='view') {
|
|
124
|
+ $path = '/'.$subject.'/{id}';
|
|
125
|
+ if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
|
|
126
|
+ return false;
|
|
127
|
+ }
|
|
128
|
+ $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
|
|
129
|
+ } else {
|
|
130
|
+ $path = '/'.$subject;
|
|
131
|
+ if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
|
|
132
|
+ return false;
|
|
133
|
+ }
|
|
134
|
+ $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
|
|
135
|
+ }
|
|
136
|
+ $references = array();
|
|
137
|
+ foreach ($properties as $field=>$property) {
|
|
138
|
+ $references[] = isset($property['x-references'])?$property['x-references']:false;
|
|
139
|
+ }
|
|
140
|
+ return $references;
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+function related($subject,$action,$definition) {
|
|
144
|
+ if (!$subject || !$definition) return false;
|
|
145
|
+ if ($action=='view') {
|
|
146
|
+ $path = '/'.$subject.'/{id}';
|
|
147
|
+ if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['properties'])) {
|
|
148
|
+ return false;
|
|
149
|
+ }
|
|
150
|
+ $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['properties'];
|
|
151
|
+ } else {
|
|
152
|
+ $path = '/'.$subject;
|
|
153
|
+ if (!isset($definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'])) {
|
|
154
|
+ return false;
|
|
155
|
+ }
|
|
156
|
+ $properties = $definition['paths'][$path]['get']['responses']['200']['schema']['items']['properties'];
|
|
157
|
+ }
|
|
158
|
+ $related = array();
|
|
159
|
+ foreach ($properties as $field=>$property) {
|
|
160
|
+ $related[] = isset($property['x-related'])?$property['x-related']:false;
|
|
161
|
+ }
|
|
162
|
+ return $related;
|
|
163
|
+}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+$action = isset($_GET['action'])?$_GET['action']:'';
|
|
167
|
+$subject = isset($_GET['subject'])?$_GET['subject']:'';
|
|
168
|
+$field = isset($_GET['field'])?$_GET['field']:'';
|
|
169
|
+$id = isset($_GET['id'])?$_GET['id']:'';
|
|
170
|
+$definition = apiCall('GET',$apiUrl);
|
|
171
|
+$references = references($subject,$action,$definition);
|
|
172
|
+$related = related($subject,$action,$definition);
|
|
173
|
+$debug = $debug?json_encode($definition,JSON_PRETTY_PRINT):false;
|
|
174
|
+
|
|
175
|
+echo head();
|
|
176
|
+echo '<div class="menu">';
|
|
177
|
+echo menu($definition['tags']);
|
|
178
|
+echo '</div>';
|
|
179
|
+echo '<div class="content">';
|
|
180
|
+switch ($action){
|
|
181
|
+ case '': echo home(); break;
|
|
182
|
+ case 'list': echo listRecords($apiUrl,$subject,$field,$id,$references,$related); break;
|
|
183
|
+ case 'view': echo viewRecord($apiUrl,$subject,$id,$references,$related); break;
|
|
184
|
+}
|
|
185
|
+echo '</div>';
|
|
186
|
+echo foot($debug);
|