Skip to content

Commit c4ed8fd

Browse files
authored
Server Info (#361)
* Adds ParseServerInfo and tests * Indicate server version being actively tested against * lint * coverage bump, adjustments * lint * test exception cases * updated README.md with examples * Updated informative comments
1 parent 537ff77 commit c4ed8fd

File tree

5 files changed

+407
-2
lines changed

5 files changed

+407
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,40 @@ if(ParsePush::hasStatus($response)) {
328328
}
329329
```
330330

331+
Server Info:
332+
333+
Get information regarding the configuration of the server you are connecting to.
334+
```php
335+
// get the current version of the server you are connected to (2.6.5, 2.5.4, etc.)
336+
$version = ParseServerInfo::getVersion();
337+
338+
// get various features
339+
$globalConfigFeatures = ParseServerInfo::getGlobalConfigFeatures();
340+
/**
341+
* Returns json of the related features
342+
* {
343+
* "create" : true,
344+
* "read" : true,
345+
* "update" : true,
346+
* "delete" : true
347+
* }
348+
*/
349+
```
350+
351+
You can get details on the following features as well:
352+
353+
```php
354+
ParseServerInfo::getHooksFeatures();
355+
ParseServerInfo::getCloudCodeFeatures();
356+
ParseServerInfo::getLogsFeatures();
357+
ParseServerInfo::getPushFeatures();
358+
ParseServerInfo::getSchemasFeatures();
359+
360+
// additional features can be obtained manually using 'get'
361+
$feature = ParseServerInfo::get('new-feature');
362+
363+
```
364+
331365
Contributing / Testing
332366
----------------------
333367

src/Parse/ParseServerInfo.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Class ParseServerInfo | Parse/ParseServerInfo.php
4+
*/
5+
6+
namespace Parse;
7+
8+
/**
9+
* Class ParseFeatures - Representation of server-side features
10+
*
11+
* @author Ben Friedman <friedman.benjamin@gmail.com>
12+
* @package Parse
13+
*/
14+
class ParseServerInfo
15+
{
16+
/**
17+
* Reported server features and configs
18+
*
19+
* @var array
20+
*/
21+
private static $serverFeatures;
22+
23+
/**
24+
* Reported server version
25+
*
26+
* @var string
27+
*/
28+
private static $serverVersion;
29+
30+
/**
31+
* Requests, sets and returns server features and version
32+
*
33+
* @return array
34+
* @throws ParseException
35+
*/
36+
private static function getServerInfo()
37+
{
38+
if (!isset(self::$serverFeatures) || !isset(self::$serverVersion)) {
39+
$info = ParseClient::_request(
40+
'GET',
41+
'serverInfo/',
42+
null,
43+
null,
44+
true
45+
);
46+
47+
// validate we have features & version
48+
49+
if (!isset($info['features'])) {
50+
throw new ParseException('Missing features in server info.');
51+
}
52+
53+
if (!isset($info['parseServerVersion'])) {
54+
throw new ParseException('Missing version in server info.');
55+
}
56+
57+
self::$serverFeatures = $info['features'];
58+
self::_setServerVersion($info['parseServerVersion']);
59+
}
60+
61+
return [
62+
'features' => self::$serverFeatures,
63+
'version' => self::$serverVersion
64+
];
65+
}
66+
67+
/**
68+
* Sets the current server version.
69+
* Allows setting the server version to avoid making an additional request
70+
* if the version is obtained elsewhere.
71+
*
72+
* @param string $version Version to set
73+
*/
74+
public static function _setServerVersion($version)
75+
{
76+
self::$serverVersion = $version;
77+
}
78+
79+
/**
80+
* Get a specific feature set from the server
81+
*
82+
* @param string $key Feature set to get
83+
* @return mixed
84+
*/
85+
public static function get($key)
86+
{
87+
return self::getServerInfo()['features'][$key];
88+
}
89+
90+
/**
91+
* Gets features for the current server
92+
*
93+
* @return array
94+
*/
95+
public static function getFeatures()
96+
{
97+
return self::getServerInfo()['features'];
98+
}
99+
100+
/**
101+
* Gets the reported version of the current server
102+
*
103+
* @return string
104+
*/
105+
public static function getVersion()
106+
{
107+
if (!isset(self::$serverVersion)) {
108+
return self::getServerInfo()['version'];
109+
} else {
110+
return self::$serverVersion;
111+
}
112+
}
113+
114+
/**
115+
* Gets features available for globalConfig
116+
*
117+
* @return array
118+
*/
119+
public static function getGlobalConfigFeatures()
120+
{
121+
return self::get('globalConfig');
122+
}
123+
124+
/**
125+
* Gets features available for hooks
126+
*
127+
* @return array
128+
*/
129+
public static function getHooksFeatures()
130+
{
131+
return self::get('hooks');
132+
}
133+
134+
/**
135+
* Gets features available for cloudCode
136+
*
137+
* @return array
138+
*/
139+
public static function getCloudCodeFeatures()
140+
{
141+
return self::get('cloudCode');
142+
}
143+
144+
/**
145+
* Gets features available for logs
146+
*
147+
* @return array
148+
*/
149+
public static function getLogsFeatures()
150+
{
151+
return self::get('logs');
152+
}
153+
154+
/**
155+
* Gets features available for push
156+
*
157+
* @return array
158+
*/
159+
public static function getPushFeatures()
160+
{
161+
return self::get('push');
162+
}
163+
164+
/**
165+
* Gets features available for schemas
166+
*
167+
* @return array
168+
*/
169+
public static function getSchemasFeatures()
170+
{
171+
return self::get('schemas');
172+
}
173+
}

0 commit comments

Comments
 (0)