mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Added simple JSON processor
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@51914 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
e747282dfa
commit
1f512519e1
@ -84,55 +84,125 @@ class RestfulServer extends Controller {
|
|||||||
return $this->notFound();
|
return $this->notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TO DO - inspect that Accept header as well. $_GET['accept'] can still be checked, as it's handy for debugging
|
||||||
|
$contentType = isset($_GET['accept']) ? $_GET['accept'] : 'text/xml';
|
||||||
|
|
||||||
if($obj->stat('api_access') && $obj->canView()) {
|
if($obj->stat('api_access') && $obj->canView()) {
|
||||||
|
switch($contentType) {
|
||||||
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<$className>\n";
|
case "text/xml":
|
||||||
foreach($obj->db() as $fieldName => $fieldType) {
|
$this->getResponse()->addHeader("Content-type", "text/xml");
|
||||||
$xml .= "<$fieldName>" . Convert::raw2xml($obj->$fieldName) . "</$fieldName>\n";
|
return $this->dataObjectAsXML($obj);
|
||||||
}
|
|
||||||
|
|
||||||
foreach($obj->has_one() as $relName => $relObj) {
|
case "text/json":
|
||||||
$fieldName = $relName . 'ID';
|
$this->getResponse()->addHeader("Content-type", "text/json");
|
||||||
if($obj->$fieldName) {
|
return $this->dataObjectAsJSON($obj);
|
||||||
$href = Director::absoluteURL(self::$api_base . "$relObj/" . $obj->$fieldName);
|
|
||||||
} else {
|
|
||||||
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
|
|
||||||
}
|
|
||||||
$xml .= "<$relName linktype=\"has_one\" href=\"$href\" id=\"{$obj->$fieldName}\" />\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($obj->has_many() as $relName => $relObj) {
|
case "text/html":
|
||||||
$xml .= "<$relName linktype=\"has_many\">\n";
|
case "application/xhtml+xml":
|
||||||
$items = $obj->$relName();
|
$this->getResponse()->addHeader("Content-type", "text/json");
|
||||||
foreach($items as $item) {
|
return $this->dataObjectAsXHTML($obj);
|
||||||
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
||||||
$href = Director::absoluteURL(self::$api_base . "$relObj/$item->ID");
|
|
||||||
$xml .= "<$relObj href=\"$href\" id=\"{$item->ID}\" />\n";
|
|
||||||
}
|
|
||||||
$xml .= "</$relName>\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($obj->many_many() as $relName => $relObj) {
|
|
||||||
$xml .= "<$relName linktype=\"many_many\">\n";
|
|
||||||
$items = $obj->$relName();
|
|
||||||
foreach($items as $item) {
|
|
||||||
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
||||||
$href = Director::absoluteURL(self::$api_base . "$relObj/$item->ID");
|
|
||||||
$xml .= "<$relObj href=\"$href\" id=\"{$item->ID}\" />\n";
|
|
||||||
}
|
|
||||||
$xml .= "</$relName>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
$xml .= "</$className>";
|
|
||||||
|
|
||||||
$this->getResponse()->addHeader("Content-type", "text/xml");
|
|
||||||
|
|
||||||
return $xml;
|
|
||||||
} else {
|
} else {
|
||||||
return $this->permissionFailure();
|
return $this->permissionFailure();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an XML representation of the given DataObject.
|
||||||
|
*/
|
||||||
|
protected function dataObjectAsXML(DataObject $obj) {
|
||||||
|
$className = $obj->class;
|
||||||
|
$id = $obj->ID;
|
||||||
|
|
||||||
|
$json = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<$className>\n";
|
||||||
|
foreach($obj->db() as $fieldName => $fieldType) {
|
||||||
|
$json .= "<$fieldName>" . Convert::raw2xml($obj->$fieldName) . "</$fieldName>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($obj->has_one() as $relName => $relClass) {
|
||||||
|
$fieldName = $relName . 'ID';
|
||||||
|
if($obj->$fieldName) {
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$relClass/" . $obj->$fieldName);
|
||||||
|
} else {
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
|
||||||
|
}
|
||||||
|
$json .= "<$relName linktype=\"has_one\" href=\"$href\" id=\"{$obj->$fieldName}\" />\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($obj->has_many() as $relName => $relClass) {
|
||||||
|
$json .= "<$relName linktype=\"has_many\">\n";
|
||||||
|
$items = $obj->$relName();
|
||||||
|
foreach($items as $item) {
|
||||||
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
||||||
|
$json .= "<$relClass href=\"$href\" id=\"{$item->ID}\" />\n";
|
||||||
|
}
|
||||||
|
$json .= "</$relName>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($obj->many_many() as $relName => $relClass) {
|
||||||
|
$json .= "<$relName linktype=\"many_many\">\n";
|
||||||
|
$items = $obj->$relName();
|
||||||
|
foreach($items as $item) {
|
||||||
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
||||||
|
$json .= "<$relClass href=\"$href\" id=\"{$item->ID}\" />\n";
|
||||||
|
}
|
||||||
|
$json .= "</$relName>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$json .= "</$className>";
|
||||||
|
|
||||||
|
return $json;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an XML representation of the given DataObject.
|
||||||
|
*/
|
||||||
|
protected function dataObjectAsJSON(DataObject $obj) {
|
||||||
|
$className = $obj->class;
|
||||||
|
$id = $obj->ID;
|
||||||
|
|
||||||
|
$json = "{\n className : \"$className\",\n";
|
||||||
|
foreach($obj->db() as $fieldName => $fieldType) {
|
||||||
|
$jsonParts[] = "$fieldName : \"" . Convert::raw2js($obj->$fieldName) . "\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($obj->has_one() as $relName => $relClass) {
|
||||||
|
$fieldName = $relName . 'ID';
|
||||||
|
if($obj->$fieldName) {
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$relClass/" . $obj->$fieldName);
|
||||||
|
} else {
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
|
||||||
|
}
|
||||||
|
$jsonParts[] = "$relName : { className : \"$relClass\", href : \"$href\", id : \"{$obj->$fieldName}\" }";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($obj->has_many() as $relName => $relClass) {
|
||||||
|
$jsonInnerParts = array();
|
||||||
|
$items = $obj->$relName();
|
||||||
|
foreach($items as $item) {
|
||||||
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
||||||
|
$jsonInnerParts[] = "{ className : \"$relClass\", href : \"$href\", id : \"{$obj->$fieldName}\" }";
|
||||||
|
}
|
||||||
|
$jsonParts[] = "$relName : [\n " . implode(",\n ", $jsonInnerParts) . " \n ]";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($obj->many_many() as $relName => $relClass) {
|
||||||
|
$jsonInnerParts = array();
|
||||||
|
$items = $obj->$relName();
|
||||||
|
foreach($items as $item) {
|
||||||
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
||||||
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
||||||
|
$jsonInnerParts[] = " { className : \"$relClass\", href : \"$href\", id : \"{$obj->$fieldName}\" }";
|
||||||
|
}
|
||||||
|
$jsonParts[] = "$relName : [\n " . implode(",\n ", $jsonInnerParts) . "\n ]";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "{\n " . implode(",\n ", $jsonParts) . "\n}";
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Handler for object delete
|
* Handler for object delete
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user