Permalink
Browse files

Version 0.7

  • Loading branch information...
1 parent c89f2dc commit 005238bef155e8ef253c3c43b9826cfc9102c689 Mashape committed Apr 11, 2011
View
@@ -1,4 +1,4 @@
-Mashape PHP library v0.6
+Mashape PHP library v0.7
Copyright (C) 2011 Mashape, Inc.
The Mashape PHP Library is:
View
@@ -2,7 +2,7 @@
<api>
<method http="GET" name="sayHello" >
- <result type="simple" name="helloWorldMessage" />
+ <result type="simple" name="helloWorldMessage"/>
</method>
</api>
@@ -75,16 +75,17 @@ function loadMethodsFromXML($xmlParser) {
}
// Get the result
- $resultsNode = $xmlMethod->result;
+ $resultsNode = XmlParserUtils::getChildren($xmlMethod, "result"); //$xmlMethod->result;
$resultNode = null;
if (count($resultsNode) > 1) {
throw new MashapeException(sprintf(EXCEPTION_RESULT_MULTIPLE, $name), EXCEPTION_XML_CODE);
} elseif (count($resultsNode)==1) {
$resultNode = $resultsNode[0];
- } else {
- throw new MashapeException(sprintf(EXCEPTION_RESULT_MISSING, $name), EXCEPTION_XML_CODE);
}
+// else {
+// throw new MashapeException(sprintf(EXCEPTION_RESULT_MISSING, $name), EXCEPTION_XML_CODE);
+// }
$object = null;
$array = null;
@@ -65,15 +65,15 @@
define("EXCEPTION_NOTSUPPORTED_OPERATION_CODE", 1004);
define("EXCEPTION_NOTSUPPORTED_OPERATION", "Operation not supported");
-define("EXCEPTION_AUTH_INVALID_SERVERKEY_CODE", 1005);
-define("EXCEPTION_AUTH_INVALID_SERVERKEY", "The request can't be authenticated because the server key sent for the request, and the one set in your implementation, don't match");
-
define("EXCEPTION_METHOD_NOTFOUND_CODE", 1006);
define("EXCEPTION_METHOD_NOTFOUND", "The method requested was not found: \"%s\"");
define("EXCEPTION_AUTH_INVALID_CODE", 1007);
define("EXCEPTION_AUTH_INVALID", "The request has not been authorized");
+define("EXCEPTION_AUTH_INVALID_SERVERKEY_CODE", 1005);
+define("EXCEPTION_AUTH_INVALID_SERVERKEY", "The request can't be authenticated because the server key sent for the request, and the one set in your implementation, don't match");
+
define("EXCEPTION_REQUIRED_PARAMETERS_CODE", 1008);
define("EXCEPTION_REQUIRED_PARAMETERS", "Some parameters required by the method are missing");
@@ -31,4 +31,4 @@
require_once(dirname(__FILE__) . "/../exceptions/mashapeException.php");
define("LIBRARY_LANGUAGE", "PHP");
-define("LIBRARY_VERSION", "V06");
+define("LIBRARY_VERSION", "V07");
@@ -27,7 +27,7 @@
class JsonUtils {
public static function serializeError($message, $code) {
- return '{"errors":[{"message":' . self::encodeToJson($message) . ',"code":' . self::encodeToJson($code) . '}], "result":null}';
+ return '[{"message":' . self::encodeToJson($message) . ',"code":' . self::encodeToJson($code) . '}]';
}
public static function encodeToJson($text) {
@@ -30,34 +30,31 @@
function doCall($method, $parameters, $instance, $serverKey) {
$callParameters = validateCallParameters($method, $parameters, $instance);
-
+
$reflectedClass = new ReflectionClass(get_class($instance));
$reflectedMethod = $reflectedClass->getMethod($method->getName());
$result;
-
+
$result = $reflectedMethod->invokeArgs($instance, $callParameters);
- $resultJson = '{';
+ $resultJson = "";
//Print custom errors
$reflectedErrorMethod = $reflectedClass->getMethod("getErrors");
$reflectedErrors = $reflectedErrorMethod->invoke($instance);
- $resultJson .= '"errors":[';
-
if (!empty($reflectedErrors)) {
+ $resultJson .= "[";
foreach ($reflectedErrors as $reflectedError) {
$reflectedErrorClass = new ReflectionClass(get_class($reflectedError));
$code = $reflectedErrorClass->getMethod("getCode")->invoke($reflectedError);
$message = $reflectedErrorClass->getMethod("getMessage")->invoke($reflectedError);
$resultJson .= '{"code":' . JsonUtils::encodeToJson($code) . ',"message":' . JsonUtils::encodeToJson($message) . '},';
}
$resultJson = JsonUtils::removeLastChar($reflectedErrors, $resultJson);
+ $resultJson .= "]";
+ } else {
+ $resultJson .= serializeMethodResult($method, $result, $instance, $serverKey);
}
-
- $resultJson .= ']';
- $resultJson .= ',"result":';
- $resultJson .= serializeMethodResult($method, $result, $instance, $serverKey);
- $resultJson .= '}';
return $resultJson;
}
@@ -34,13 +34,17 @@
function serializeMethodResult($method, $result, $instance, $serverKey) {
$json = "";
+ if (isNoResult($method)) {
+ return "{}";
+ }
+
$isSimpleResult = isSimpleResult($method);
if ($result === null) {
if($isSimpleResult) {
$json .= '{"' . $method->getResult() . '":null}';
} else {
- $json .= "null";
+ $json .= "{}";
}
} else {
if ($isSimpleResult) {
@@ -68,6 +72,14 @@ function serializeMethodResult($method, $result, $instance, $serverKey) {
return $json;
}
+function isNoResult($method) {
+ $resultName = $method->getResult();
+ $objectName = $method->getObject();
+ if (empty($resultName) && empty($objectName)) {
+ return true;
+ }
+ return false;
+}
function isSimpleResult($method) {
$resultName = $method->getResult();
$objectName = $method->getObject();
@@ -44,38 +44,57 @@ public function handle($instance, $serverKey, $parameters, $httpRequestMethod) {
if (strtolower($httpRequestMethod) != "get") {
throw new MashapeException(EXCEPTION_INVALID_HTTPMETHOD, EXCEPTION_INVALID_HTTPMETHOD_CODE);
}
+
// Validate request
if ($this->validateRequest($serverKey) == false) {
throw new MashapeException(EXCEPTION_AUTH_INVALID_SERVERKEY, EXCEPTION_AUTH_INVALID_SERVERKEY_CODE);
}
- $resultJson = "{";
+ $resultXml = "<?xml version=\"1.0\" ?>\n";
+
+ $fileParts = Explode('/', $_SERVER["PHP_SELF"]);
+ $scriptName = $fileParts[count($fileParts) - 1];
+
+ $baseUrl = Explode("/" . $scriptName, $this->curPageURL());
+ $resultXml .= "<api baseUrl=\"" . $baseUrl[0] . "\" " . $this->getSimpleInfo() . ">\n";
$mode = (isset($parameters[MODE])) ? $parameters[MODE] : null;
-
+
$configuration = RESTConfigurationLoader::reloadConfiguration($serverKey);
if ($mode == null || $mode != SIMPLE_MODE) {
$objectsFound = array();
- $methods = discoverMethods($instance, $configuration, $objectsFound);
+ $objectsToCreate = array();
+
+ $methods = discoverMethods($instance, $configuration, $objectsFound, $objectsToCreate, $scriptName);
$objects = discoverObjects($configuration, $objectsFound);
- $resultJson .= $methods . "," . $objects . "," . $this->getSimpleInfo();
-
+ $resultXml .= $methods . $objects . generateObjects($objectsToCreate);
+
// Update the .htaccess file with the new route settings
updateHtaccess($instance);
-
- } else {
- $resultJson .= $this->getSimpleInfo();
+
}
- $resultJson .= "}";
+ $resultXml .= "</api>";
- return $resultJson;
+ return $resultXml;
}
-
+
private function getSimpleInfo() {
- $libraryVersion = '"version":"' . LIBRARY_VERSION . '"';
- $libraryLanguage = '"language":"' . LIBRARY_LANGUAGE . '"';
-
- return $libraryLanguage . "," . $libraryVersion;
+ $libraryLanguage = "language=\"" . LIBRARY_LANGUAGE . "\"";
+ $libraryVersion = " version=\"" . LIBRARY_VERSION . "\"";
+
+ return $libraryLanguage . $libraryVersion;
+ }
+
+ private function curPageURL() {
+ $pageURL = 'http';
+ if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
+ $pageURL .= "://";
+ if (isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] != "80") {
+ $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
+ } else {
+ $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
+ }
+ return $pageURL;
}
private function validateRequest($serverKey) {
@@ -27,58 +27,93 @@
require_once(dirname(__FILE__) . "/../../../json/jsonUtils.php");
require_once(dirname(__FILE__) . "/../../../utils/routeUtils.php");
-function discoverMethods($instance, $configuration, &$objectsFound) {
+function discoverMethods($instance, $configuration, &$objectsFound, &$objectsToCreate, $scriptName) {
// Serialize methods
- $result = '"methods":[';
+ $result = "";
$methods = $configuration->getMethods();
foreach($methods as $method) {
- $result .= "{";
+ $result .= "\t<method ";
$name = $method->getName();
- $result .= '"name":"' . $name . '",';
- $object = $method->getObject();
+ $result .= 'name="' . $name . '"';
+ $http = $method->getHttp();
+ $result .= " http=\"" . strtoupper($http) . "\">\n";
$route = $method->getRoute();
- if (empty($object)) {
- $result .= '"object":null,';
- } else {
- $result .= '"object":"' . $object . '",';
- array_push($objectsFound, $object);
- }
+
+ $result .= "\t\t<url><![CDATA[";
if (empty($route)) {
- $result .= '"route":null,';
+ $result .= "/" . $scriptName . "?_method=" . $name . serializeParametersQueryString($method, $instance);
} else {
- $result .= '"route":"' . $route . '",';
+ $result .= $route;
}
+ $result .= "]]></url>\n";
+ $result .= serializeParameters($method, $instance);
+
+ $object = $method->getObject();
$resultName = $method->getResult();
- if (empty($resultName)) {
- $result .= '"result":null,';
- } else {
- $result .= '"result":"' . $resultName . '",';
+
+ if (!empty($object)) {
+ $result .= "\t\t<result object=\"" . $object . "\"";
+ array_push($objectsFound, $object);
+ }
+
+ if (!empty($resultName)) {
+ $uniqueName = findUniqueObjectName($objectsToCreate, $resultName, 0);
+ $result .= "\t\t<result object=\"" . $uniqueName . "\"";
+ $objectsToCreate[$uniqueName] = $resultName;
}
+
$array = $method->isArray();
- $result .= '"array":' . ($array ? "true" : "false") . ',';
- $http = $method->getHttp();
- $result .= '"http":"' . $http . '",';
- $result .= serializeParameters($method, $instance);
+ $result .= " array=\"" . ($array ? "true" : "false") . "\" />\n";
- $result .= "},";
+ $result .= "\t\t<error object=\"StandardMashapeError\" array=\"true\" />\n";
+
+ $result .= "\t</method>\n";
}
- // Remove the last comma
- $result = JsonUtils::removeLastChar($methods, $result);
- $result .= "]";
return $result;
}
-function serializeParameters($method, $instance) {
+function findUniqueObjectName($objects, $name, $index) {
+ $numeratedName = $name;
+ if ($index > 0) {
+ $numeratedName .= $index;
+ }
+ $keys = array_keys($objects);
+ foreach ($keys as $key) {
+ if ($key == $numeratedName) {
+ return findUniqueObjectName($objects, $name, $index + 1);
+ }
+ }
+ return $numeratedName;
+}
+
+function serializeParametersQueryString($method, $instance) {
$reflectedClass = new ReflectionClass(get_class($instance));
$reflectedMethod = $reflectedClass->getMethod($method->getName());
$reflectedParameters = $reflectedMethod->getParameters();
- $result = '"parameters":[';
+ $result = "";
for ($i=0;$i<count($reflectedParameters);$i++) {
$param = $reflectedParameters[$i];
- $result .= '{"name":"' . $param->name . '", "optional":' . ($param->isDefaultValueAvailable() ? "true" : "false") . ',"index":' . $i . '},';
+ if ($i == 0) {
+ $result .= "&";
+ }
+ $result .= $param->name . "={" . $param->name . "}&";
}
- // Remove the last comma
+
$result = JsonUtils::removeLastChar($reflectedParameters, $result);
+ return $result;
+}
+
+function serializeParameters($method, $instance) {
+ $reflectedClass = new ReflectionClass(get_class($instance));
+ $reflectedMethod = $reflectedClass->getMethod($method->getName());
+ $reflectedParameters = $reflectedMethod->getParameters();
+ $result = "\t\t<parameters>\n";
+ for ($i=0;$i<count($reflectedParameters);$i++) {
+ $param = $reflectedParameters[$i];
+ $result .= "\t\t\t<parameter optional=\"" . ($param->isDefaultValueAvailable() ? "true" : "false") . "\">" . $param->name . "</parameter>\n";
+ }
+
+ $result .= "\t\t</parameters>\n";
// Check route parameters
$route = $method->getRoute();
@@ -105,6 +140,5 @@ function serializeParameters($method, $instance) {
}
}
- $result .= ']';
return $result;
}
Oops, something went wrong.

0 comments on commit 005238b

Please sign in to comment.