Skip to content

Server Info #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,40 @@ if(ParsePush::hasStatus($response)) {
}
```

Server Info:

Get information regarding the configuration of the server you are connecting to.
```php
// get the current version of the server you are connected to (2.6.5, 2.5.4, etc.)
$version = ParseServerInfo::getVersion();

// get various features
$globalConfigFeatures = ParseServerInfo::getGlobalConfigFeatures();
/**
* Returns json of the related features
* {
* "create" : true,
* "read" : true,
* "update" : true,
* "delete" : true
* }
*/
```

You can get details on the following features as well:

```php
ParseServerInfo::getHooksFeatures();
ParseServerInfo::getCloudCodeFeatures();
ParseServerInfo::getLogsFeatures();
ParseServerInfo::getPushFeatures();
ParseServerInfo::getSchemasFeatures();

// additional features can be obtained manually using 'get'
$feature = ParseServerInfo::get('new-feature');

```

Contributing / Testing
----------------------

Expand Down
173 changes: 173 additions & 0 deletions src/Parse/ParseServerInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Class ParseServerInfo | Parse/ParseServerInfo.php
*/

namespace Parse;

/**
* Class ParseFeatures - Representation of server-side features
*
* @author Ben Friedman <[email protected]>
* @package Parse
*/
class ParseServerInfo
{
/**
* Reported server features and configs
*
* @var array
*/
private static $serverFeatures;

/**
* Reported server version
*
* @var string
*/
private static $serverVersion;

/**
* Requests, sets and returns server features and version
*
* @return array
* @throws ParseException
*/
private static function getServerInfo()
{
if (!isset(self::$serverFeatures) || !isset(self::$serverVersion)) {
$info = ParseClient::_request(
'GET',
'serverInfo/',
null,
null,
true
);

// validate we have features & version

if (!isset($info['features'])) {
throw new ParseException('Missing features in server info.');
}

if (!isset($info['parseServerVersion'])) {
throw new ParseException('Missing version in server info.');
}

self::$serverFeatures = $info['features'];
self::_setServerVersion($info['parseServerVersion']);
}

return [
'features' => self::$serverFeatures,
'version' => self::$serverVersion
];
}

/**
* Sets the current server version.
* Allows setting the server version to avoid making an additional request
* if the version is obtained elsewhere.
*
* @param string $version Version to set
*/
public static function _setServerVersion($version)
{
self::$serverVersion = $version;
}

/**
* Get a specific feature set from the server
*
* @param string $key Feature set to get
* @return mixed
*/
public static function get($key)
{
return self::getServerInfo()['features'][$key];
}

/**
* Gets features for the current server
*
* @return array
*/
public static function getFeatures()
{
return self::getServerInfo()['features'];
}

/**
* Gets the reported version of the current server
*
* @return string
*/
public static function getVersion()
{
if (!isset(self::$serverVersion)) {
return self::getServerInfo()['version'];
} else {
return self::$serverVersion;
}
}

/**
* Gets features available for globalConfig
*
* @return array
*/
public static function getGlobalConfigFeatures()
{
return self::get('globalConfig');
}

/**
* Gets features available for hooks
*
* @return array
*/
public static function getHooksFeatures()
{
return self::get('hooks');
}

/**
* Gets features available for cloudCode
*
* @return array
*/
public static function getCloudCodeFeatures()
{
return self::get('cloudCode');
}

/**
* Gets features available for logs
*
* @return array
*/
public static function getLogsFeatures()
{
return self::get('logs');
}

/**
* Gets features available for push
*
* @return array
*/
public static function getPushFeatures()
{
return self::get('push');
}

/**
* Gets features available for schemas
*
* @return array
*/
public static function getSchemasFeatures()
{
return self::get('schemas');
}
}
Loading