|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: jaredchu |
|
5
|
|
|
* Date: 11/29/16 |
|
6
|
|
|
* Time: 3:47 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace JCFirebase; |
|
10
|
|
|
|
|
11
|
|
|
use Requests; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class JCFirebase |
|
15
|
|
|
* @package JCFirebase |
|
16
|
|
|
* reference https://www.firebase.com/docs/rest/api/ |
|
17
|
|
|
*/ |
|
18
|
|
|
class JCFirebase |
|
19
|
|
|
{ |
|
20
|
|
|
public $firebaseURI; |
|
21
|
|
|
|
|
22
|
|
|
public $rootPath; |
|
23
|
|
|
|
|
24
|
|
|
public $requestHeader = array( |
|
25
|
|
|
'accept' => 'application/json', |
|
26
|
|
|
'contentType' => 'application/json; charset=utf-8', |
|
27
|
|
|
'dataType' => 'json' |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
public $requestOptions = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var OAuth |
|
34
|
|
|
*/ |
|
35
|
|
|
public $auth; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* JCFirebase constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param $firebaseURI |
|
42
|
|
|
* @param array $firebaseAuth |
|
|
|
|
|
|
43
|
|
|
* @param string $rootPath |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($firebaseURI, OAuth $auth, $rootPath = '/') |
|
46
|
|
|
{ |
|
47
|
|
|
$this->firebaseURI = $firebaseURI; |
|
48
|
|
|
$this->firebaseDefaultPath = $rootPath; |
|
|
|
|
|
|
49
|
|
|
$this->auth = $auth; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $firebaseURI |
|
55
|
|
|
* @param $jsonString |
|
56
|
|
|
* @param string $rootPath |
|
57
|
|
|
* @return JCFirebase |
|
58
|
|
|
* @throws \Exception |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function fromJson($firebaseURI, $jsonString, $rootPath = '/') |
|
61
|
|
|
{ |
|
62
|
|
|
if ($jsonString) { |
|
63
|
|
|
$serviceAccount = $jsonString->client_email; |
|
64
|
|
|
$privateKey = $jsonString->private_key; |
|
65
|
|
|
|
|
66
|
|
|
return new self($firebaseURI, new OAuth($privateKey, $serviceAccount), $rootPath); |
|
67
|
|
|
} else { |
|
68
|
|
|
throw new \Exception("can't get data from key file"); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $firebaseURI |
|
74
|
|
|
* @param $keyFile |
|
75
|
|
|
* @param string $rootPath |
|
76
|
|
|
* |
|
77
|
|
|
* @return JCFirebase |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function fromKeyFile($firebaseURI, $keyFile, $rootPath = '/') |
|
81
|
|
|
{ |
|
82
|
|
|
$jsonString = null; |
|
83
|
|
|
try { |
|
84
|
|
|
$jsonString = json_decode(file_get_contents($keyFile)); |
|
85
|
|
|
} catch (\Exception $exception) { |
|
86
|
|
|
$jsonString = json_decode(Requests::get($keyFile)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return self::fromJson($firebaseURI, $jsonString, $rootPath); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getPathURI($path = '', $print = '') |
|
93
|
|
|
{ |
|
94
|
|
|
//remove last slash from firebaseURI |
|
95
|
|
|
$template = '/'; |
|
96
|
|
|
$this->firebaseURI = rtrim($this->firebaseURI, $template); |
|
97
|
|
|
$path = rtrim($path, $template); |
|
98
|
|
|
$path = ltrim($path, $template); |
|
99
|
|
|
|
|
100
|
|
|
//check https |
|
101
|
|
|
if (strpos($this->firebaseURI, 'http://') !== false) { |
|
102
|
|
|
throw new \Exception("https is required."); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
//check firebaseURI |
|
106
|
|
|
if (empty($this->firebaseURI)) { |
|
107
|
|
|
throw new \Exception("firebase URI is required"); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (strpos($this->firebaseDefaultPath, "/") !== 0) { |
|
111
|
|
|
throw new \Exception("firebase default path must contain /"); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
115
|
|
|
|
|
116
|
|
|
//set query data |
|
117
|
|
|
$queryData = array(); |
|
118
|
|
|
if (!empty($print)) { |
|
119
|
|
|
$queryData[Option::OPTION_PRINT] = $print; |
|
120
|
|
|
} |
|
121
|
|
|
if (!empty($queryData)) { |
|
122
|
|
|
$pathURI = $pathURI . '?' . http_build_query($queryData); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->refreshToken(); |
|
126
|
|
|
|
|
127
|
|
|
return $pathURI; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getShallow($path = '', $options = array()) |
|
131
|
|
|
{ |
|
132
|
|
|
return Requests::get( |
|
133
|
|
|
$this->getPathURI($path) . '?' . http_build_query(array( |
|
134
|
|
|
Option::OPTION_SHALLOW => Option::SHALLOW_TRUE |
|
135
|
|
|
)), |
|
136
|
|
|
$this->requestHeader, |
|
137
|
|
|
$this->addDataToRequest($options) |
|
|
|
|
|
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $path |
|
143
|
|
|
* @param array $options |
|
144
|
|
|
* |
|
145
|
|
|
* @return \Requests_Response |
|
146
|
|
|
*/ |
|
147
|
|
|
public function get($path = '', $options = array()) |
|
148
|
|
|
{ |
|
149
|
|
|
return Requests::get( |
|
150
|
|
|
$this->addDataToPathURI($path, $options), $this->requestHeader, |
|
151
|
|
|
$this->addDataToRequest($options) |
|
|
|
|
|
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $path |
|
157
|
|
|
* @param array $options |
|
158
|
|
|
* |
|
159
|
|
|
* @return \Requests_Response |
|
160
|
|
|
*/ |
|
161
|
|
|
public function put($path = '', $options = array()) |
|
162
|
|
|
{ |
|
163
|
|
|
return Requests::put($this->getPathURI($path), $this->requestHeader, |
|
164
|
|
|
$this->addDataToRequest($options, true)); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $path |
|
169
|
|
|
* @param array $options |
|
170
|
|
|
* |
|
171
|
|
|
* @return \Requests_Response |
|
172
|
|
|
*/ |
|
173
|
|
|
public function post($path = '', $options = array()) |
|
174
|
|
|
{ |
|
175
|
|
|
return Requests::post($this->getPathURI($path), $this->requestHeader, |
|
176
|
|
|
$this->addDataToRequest($options, true)); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $path |
|
181
|
|
|
* @param array $options |
|
182
|
|
|
* |
|
183
|
|
|
* @return \Requests_Response |
|
184
|
|
|
*/ |
|
185
|
|
|
public function patch($path = '', $options = array()) |
|
186
|
|
|
{ |
|
187
|
|
|
return Requests::patch($this->getPathURI($path), $this->requestHeader, |
|
188
|
|
|
$this->addDataToRequest($options, true)); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $path |
|
193
|
|
|
* @param array $options |
|
194
|
|
|
* |
|
195
|
|
|
* @return \Requests_Response |
|
196
|
|
|
*/ |
|
197
|
|
|
public function delete($path = '', $options = array()) |
|
198
|
|
|
{ |
|
199
|
|
|
return Requests::delete($this->getPathURI($path), $this->requestHeader, |
|
200
|
|
|
$this->addDataToRequest($options)); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Function that check firebase authencation |
|
205
|
|
|
* and configuration valid or not |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool |
|
208
|
|
|
*/ |
|
209
|
|
|
public function isValid() |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->get(null, array( |
|
212
|
|
|
Option::OPTION_PRINT => Option::PRINT_SILENT |
|
213
|
|
|
))->status_code == 204; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
protected function refreshToken() |
|
217
|
|
|
{ |
|
218
|
|
|
$this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
protected function addDataToPathURI($path = '', $options = array(), $reqType = Option::REQ_TYPE_GET) |
|
222
|
|
|
{ |
|
223
|
|
|
$print = ''; |
|
224
|
|
|
if (isset($options['print'])) { |
|
225
|
|
|
if (Option::isAllowPrint($reqType, $options['print'])) { |
|
226
|
|
|
$print = $options['print']; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $this->getPathURI($path, $print); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function addDataToRequest($options = array(), $jsonEncode = false) |
|
234
|
|
|
{ |
|
235
|
|
|
$requestOptions = array(); |
|
236
|
|
|
|
|
237
|
|
|
if (isset($options['data'])) { |
|
238
|
|
|
$requestOptions = array_merge($options['data'], $requestOptions); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
if ($jsonEncode) { |
|
242
|
|
|
$requestOptions = json_encode($requestOptions); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $requestOptions; |
|
246
|
|
|
} |
|
247
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.