@@ -16,198 +16,198 @@ |
||
16 | 16 | * reference https://www.firebase.com/docs/rest/api/ |
17 | 17 | */ |
18 | 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 | - |
|
33 | - /** |
|
34 | - * JCFirebase constructor. |
|
35 | - * |
|
36 | - * @param $firebaseURI |
|
37 | - * @param array $firebaseAuth |
|
38 | - * @param string $firebaseDefaultPath |
|
39 | - */ |
|
40 | - public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
41 | - $this->firebaseURI = $firebaseURI; |
|
42 | - $this->firebaseDefaultPath = $firebaseDefaultPath; |
|
43 | - $this->setAuth( $firebaseAuth ); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param $firebaseURI |
|
48 | - * @param $keyFile |
|
49 | - * @param string $firebaseDefaultPath |
|
50 | - * |
|
51 | - * @return JCFirebase |
|
52 | - * @throws \Exception |
|
53 | - */ |
|
54 | - public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
55 | - $keyData = null; |
|
56 | - try { |
|
57 | - $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
58 | - } catch ( \Exception $exception ) { |
|
59 | - $keyData = json_decode( Requests::get( $keyFile ) ); |
|
60 | - } |
|
61 | - |
|
62 | - if ( $keyData ) { |
|
63 | - $serviceAccount = $keyData->client_email; |
|
64 | - $privateKey = $keyData->private_key; |
|
65 | - |
|
66 | - return new self( $firebaseURI, array( |
|
67 | - 'key' => $privateKey, |
|
68 | - 'iss' => $serviceAccount |
|
69 | - ), $firebaseDefaultPath ); |
|
70 | - } else { |
|
71 | - throw new \Exception( "can't get data from key file" ); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - public function setAuth( $firebaseServiceAccount ) { |
|
76 | - if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
77 | - $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - public function getPathURI( $path = '', $print = '' ) { |
|
82 | - //remove last slash from firebaseURI |
|
83 | - $template = '/'; |
|
84 | - $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
85 | - $path = rtrim( $path, $template ); |
|
86 | - $path = ltrim( $path, $template ); |
|
87 | - |
|
88 | - //check https |
|
89 | - if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
90 | - throw new \Exception( "https is required." ); |
|
91 | - } |
|
92 | - |
|
93 | - //check firebaseURI |
|
94 | - if ( empty( $this->firebaseURI ) ) { |
|
95 | - throw new \Exception( "firebase URI is required" ); |
|
96 | - } |
|
97 | - |
|
98 | - if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
99 | - throw new \Exception( "firebase default path must contain /" ); |
|
100 | - } |
|
101 | - |
|
102 | - $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
103 | - |
|
104 | - //set query data |
|
105 | - $queryData = array(); |
|
106 | - if ( ! empty( $print ) ) { |
|
107 | - $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
108 | - } |
|
109 | - if ( ! empty( $queryData ) ) { |
|
110 | - $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
111 | - } |
|
112 | - |
|
113 | - $this->refreshToken(); |
|
114 | - |
|
115 | - return $pathURI; |
|
116 | - } |
|
117 | - |
|
118 | - public function getShallow( $path = '', $options = array() ) { |
|
119 | - return Requests::get( |
|
120 | - $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
121 | - JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
|
122 | - ) ), |
|
123 | - $this->requestHeader, |
|
124 | - $this->addDataToRequest( $options ) |
|
125 | - ); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param string $path |
|
130 | - * @param array $options |
|
131 | - * |
|
132 | - * @return \Requests_Response |
|
133 | - */ |
|
134 | - public function get( $path = '', $options = array() ) { |
|
135 | - return Requests::get( |
|
136 | - $this->addDataToPathURI( $path, $options ), $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 put( $path = '', $options = array() ) { |
|
148 | - return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
149 | - $this->addDataToRequest( $options, true ) ); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param string $path |
|
154 | - * @param array $options |
|
155 | - * |
|
156 | - * @return \Requests_Response |
|
157 | - */ |
|
158 | - public function post( $path = '', $options = array() ) { |
|
159 | - return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
160 | - $this->addDataToRequest( $options, true ) ); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @param string $path |
|
165 | - * @param array $options |
|
166 | - * |
|
167 | - * @return \Requests_Response |
|
168 | - */ |
|
169 | - public function patch( $path = '', $options = array() ) { |
|
170 | - return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
171 | - $this->addDataToRequest( $options, true ) ); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $path |
|
176 | - * @param array $options |
|
177 | - * |
|
178 | - * @return \Requests_Response |
|
179 | - */ |
|
180 | - public function delete( $path = '', $options = array() ) { |
|
181 | - return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
182 | - $this->addDataToRequest( $options ) ); |
|
183 | - } |
|
184 | - |
|
185 | - protected function refreshToken() { |
|
186 | - $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
187 | - } |
|
188 | - |
|
189 | - protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
190 | - $print = ''; |
|
191 | - if ( isset( $options['print'] ) ) { |
|
192 | - if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
193 | - $print = $options['print']; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - return $this->getPathURI( $path, $print ); |
|
198 | - } |
|
199 | - |
|
200 | - protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
201 | - $requestOptions = array(); |
|
202 | - |
|
203 | - if ( isset( $options['data'] ) ) { |
|
204 | - $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
205 | - } |
|
206 | - |
|
207 | - if ( $jsonEncode ) { |
|
208 | - $requestOptions = json_encode( $requestOptions ); |
|
209 | - } |
|
210 | - |
|
211 | - return $requestOptions; |
|
212 | - } |
|
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 | + |
|
33 | + /** |
|
34 | + * JCFirebase constructor. |
|
35 | + * |
|
36 | + * @param $firebaseURI |
|
37 | + * @param array $firebaseAuth |
|
38 | + * @param string $firebaseDefaultPath |
|
39 | + */ |
|
40 | + public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
41 | + $this->firebaseURI = $firebaseURI; |
|
42 | + $this->firebaseDefaultPath = $firebaseDefaultPath; |
|
43 | + $this->setAuth( $firebaseAuth ); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param $firebaseURI |
|
48 | + * @param $keyFile |
|
49 | + * @param string $firebaseDefaultPath |
|
50 | + * |
|
51 | + * @return JCFirebase |
|
52 | + * @throws \Exception |
|
53 | + */ |
|
54 | + public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
55 | + $keyData = null; |
|
56 | + try { |
|
57 | + $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
58 | + } catch ( \Exception $exception ) { |
|
59 | + $keyData = json_decode( Requests::get( $keyFile ) ); |
|
60 | + } |
|
61 | + |
|
62 | + if ( $keyData ) { |
|
63 | + $serviceAccount = $keyData->client_email; |
|
64 | + $privateKey = $keyData->private_key; |
|
65 | + |
|
66 | + return new self( $firebaseURI, array( |
|
67 | + 'key' => $privateKey, |
|
68 | + 'iss' => $serviceAccount |
|
69 | + ), $firebaseDefaultPath ); |
|
70 | + } else { |
|
71 | + throw new \Exception( "can't get data from key file" ); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + public function setAuth( $firebaseServiceAccount ) { |
|
76 | + if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
77 | + $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + public function getPathURI( $path = '', $print = '' ) { |
|
82 | + //remove last slash from firebaseURI |
|
83 | + $template = '/'; |
|
84 | + $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
85 | + $path = rtrim( $path, $template ); |
|
86 | + $path = ltrim( $path, $template ); |
|
87 | + |
|
88 | + //check https |
|
89 | + if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
90 | + throw new \Exception( "https is required." ); |
|
91 | + } |
|
92 | + |
|
93 | + //check firebaseURI |
|
94 | + if ( empty( $this->firebaseURI ) ) { |
|
95 | + throw new \Exception( "firebase URI is required" ); |
|
96 | + } |
|
97 | + |
|
98 | + if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
99 | + throw new \Exception( "firebase default path must contain /" ); |
|
100 | + } |
|
101 | + |
|
102 | + $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
103 | + |
|
104 | + //set query data |
|
105 | + $queryData = array(); |
|
106 | + if ( ! empty( $print ) ) { |
|
107 | + $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
108 | + } |
|
109 | + if ( ! empty( $queryData ) ) { |
|
110 | + $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
111 | + } |
|
112 | + |
|
113 | + $this->refreshToken(); |
|
114 | + |
|
115 | + return $pathURI; |
|
116 | + } |
|
117 | + |
|
118 | + public function getShallow( $path = '', $options = array() ) { |
|
119 | + return Requests::get( |
|
120 | + $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
121 | + JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
|
122 | + ) ), |
|
123 | + $this->requestHeader, |
|
124 | + $this->addDataToRequest( $options ) |
|
125 | + ); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param string $path |
|
130 | + * @param array $options |
|
131 | + * |
|
132 | + * @return \Requests_Response |
|
133 | + */ |
|
134 | + public function get( $path = '', $options = array() ) { |
|
135 | + return Requests::get( |
|
136 | + $this->addDataToPathURI( $path, $options ), $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 put( $path = '', $options = array() ) { |
|
148 | + return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
149 | + $this->addDataToRequest( $options, true ) ); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param string $path |
|
154 | + * @param array $options |
|
155 | + * |
|
156 | + * @return \Requests_Response |
|
157 | + */ |
|
158 | + public function post( $path = '', $options = array() ) { |
|
159 | + return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
160 | + $this->addDataToRequest( $options, true ) ); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @param string $path |
|
165 | + * @param array $options |
|
166 | + * |
|
167 | + * @return \Requests_Response |
|
168 | + */ |
|
169 | + public function patch( $path = '', $options = array() ) { |
|
170 | + return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
171 | + $this->addDataToRequest( $options, true ) ); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $path |
|
176 | + * @param array $options |
|
177 | + * |
|
178 | + * @return \Requests_Response |
|
179 | + */ |
|
180 | + public function delete( $path = '', $options = array() ) { |
|
181 | + return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
182 | + $this->addDataToRequest( $options ) ); |
|
183 | + } |
|
184 | + |
|
185 | + protected function refreshToken() { |
|
186 | + $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
187 | + } |
|
188 | + |
|
189 | + protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
190 | + $print = ''; |
|
191 | + if ( isset( $options['print'] ) ) { |
|
192 | + if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
193 | + $print = $options['print']; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + return $this->getPathURI( $path, $print ); |
|
198 | + } |
|
199 | + |
|
200 | + protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
201 | + $requestOptions = array(); |
|
202 | + |
|
203 | + if ( isset( $options['data'] ) ) { |
|
204 | + $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
205 | + } |
|
206 | + |
|
207 | + if ( $jsonEncode ) { |
|
208 | + $requestOptions = json_encode( $requestOptions ); |
|
209 | + } |
|
210 | + |
|
211 | + return $requestOptions; |
|
212 | + } |
|
213 | 213 | } |
214 | 214 | \ No newline at end of file |