|
| 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