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
|
|
|
public $firebaseURI; |
20
|
|
|
public $firebaseDefaultPath; |
21
|
|
|
public $requestHeader = array( |
22
|
|
|
'accept' => 'application/json', |
23
|
|
|
'contentType' => 'application/json; charset=utf-8', |
24
|
|
|
'dataType' => 'json' |
25
|
|
|
); |
26
|
|
|
public $requestOptions = array(); |
27
|
|
|
/** |
28
|
|
|
* @var OAuth |
29
|
|
|
*/ |
30
|
|
|
protected $auth; |
31
|
|
|
|
32
|
|
|
public function __construct( $firebaseURI, $firebaseSerivceAccount = '', $firebaseDefaultPath = '/' ) { |
33
|
|
|
$this->firebaseURI = $firebaseURI; |
34
|
|
|
$this->firebaseDefaultPath = $firebaseDefaultPath; |
35
|
|
|
$this->setAuth( $firebaseSerivceAccount ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
39
|
|
|
$keyData = null; |
40
|
|
|
try { |
41
|
|
|
$keyData = json_decode( file_get_contents( $keyFile ) ); |
42
|
|
|
} catch ( \Exception $exception ) { |
43
|
|
|
$keyData = json_decode( Requests::get( $keyFile ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( $keyData ) { |
47
|
|
|
$serviceAccount = $keyData->client_email; |
48
|
|
|
$privateKey = $keyData->private_key; |
49
|
|
|
|
50
|
|
|
return new self( $firebaseURI, array( |
|
|
|
|
51
|
|
|
'key' => $privateKey, |
52
|
|
|
'iss' => $serviceAccount |
53
|
|
|
), $firebaseDefaultPath ); |
54
|
|
|
} else { |
55
|
|
|
throw new \Exception( "can't get data from key file" ); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setAuth( $firebaseServiceAccount ) { |
60
|
|
|
if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
61
|
|
|
$this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getPathURI( $path = '', $print = '' ) { |
66
|
|
|
//remove last slash from firebaseURI |
67
|
|
|
$template = '/'; |
68
|
|
|
$this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
69
|
|
|
$path = rtrim( $path, $template ); |
70
|
|
|
$path = ltrim( $path, $template ); |
71
|
|
|
|
72
|
|
|
//check https |
73
|
|
|
if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
74
|
|
|
throw new \Exception( "https is required." ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//check firebaseURI |
78
|
|
|
if ( empty( $this->firebaseURI ) ) { |
79
|
|
|
throw new \Exception( "firebase URI is required" ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
83
|
|
|
throw new \Exception( "firebase default path must contain /" ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
87
|
|
|
|
88
|
|
|
//set query data |
89
|
|
|
$queryData = array(); |
90
|
|
|
if ( ! empty( $print ) ) { |
91
|
|
|
$queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
92
|
|
|
} |
93
|
|
|
if ( ! empty( $queryData ) ) { |
94
|
|
|
$pathURI = $pathURI . '?' . http_build_query( $queryData ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->refreshToken(); |
98
|
|
|
|
99
|
|
|
return $pathURI; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getShallow( $path = '', $options = array() ) { |
103
|
|
|
return Requests::get( |
104
|
|
|
$this->getPathURI( $path ) . '?' . http_build_query( array( |
105
|
|
|
JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
106
|
|
|
) ), |
107
|
|
|
$this->requestHeader, |
108
|
|
|
$this->addDataToRequest( $options ) |
|
|
|
|
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $path |
114
|
|
|
* @param array $options |
115
|
|
|
* |
116
|
|
|
* @return \Requests_Response |
117
|
|
|
*/ |
118
|
|
|
public function get( $path = '', $options = array() ) { |
119
|
|
|
return Requests::get( |
120
|
|
|
$this->addDataToPathURI( $path, $options ), $this->requestHeader, |
121
|
|
|
$this->addDataToRequest( $options ) |
|
|
|
|
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $path |
127
|
|
|
* @param array $options |
128
|
|
|
* |
129
|
|
|
* @return \Requests_Response |
130
|
|
|
*/ |
131
|
|
|
public function put( $path = '', $options = array() ) { |
132
|
|
|
return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
133
|
|
|
$this->addDataToRequest( $options, true ) ); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $path |
138
|
|
|
* @param array $options |
139
|
|
|
* |
140
|
|
|
* @return \Requests_Response |
141
|
|
|
*/ |
142
|
|
|
public function post( $path = '', $options = array() ) { |
143
|
|
|
return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
144
|
|
|
$this->addDataToRequest( $options, true ) ); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $path |
149
|
|
|
* @param array $options |
150
|
|
|
* |
151
|
|
|
* @return \Requests_Response |
152
|
|
|
*/ |
153
|
|
|
public function patch( $path = '', $options = array() ) { |
154
|
|
|
return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
155
|
|
|
$this->addDataToRequest( $options, true ) ); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $path |
160
|
|
|
* @param array $options |
161
|
|
|
* |
162
|
|
|
* @return \Requests_Response |
163
|
|
|
*/ |
164
|
|
|
public function delete( $path = '', $options = array() ) { |
165
|
|
|
return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
166
|
|
|
$this->addDataToRequest( $options ) ); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function refreshToken() { |
170
|
|
|
$this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
174
|
|
|
$print = ''; |
175
|
|
|
if ( isset( $options['print'] ) ) { |
176
|
|
|
if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
177
|
|
|
$print = $options['print']; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->getPathURI( $path, $print ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
185
|
|
|
$requestOptions = array(); |
186
|
|
|
|
187
|
|
|
if ( isset( $options['data'] ) ) { |
188
|
|
|
$requestOptions = array_merge( $options['data'], $requestOptions ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ( $jsonEncode ) { |
192
|
|
|
$requestOptions = json_encode( $requestOptions ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $requestOptions; |
196
|
|
|
} |
197
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: