Skip to content

Commit

Permalink
Fixes fuel#792: Made it possible to specify xml basenode in rest cont…
Browse files Browse the repository at this point in the history
…rollers.
  • Loading branch information
frankdejonge committed Sep 24, 2012
1 parent 0aabc9e commit 8fdbe96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
22 changes: 20 additions & 2 deletions classes/controller/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ abstract class Controller_Rest extends \Controller
*/
protected $http_status = null;

/**
* @var string xml basenode name
*/
protected $xml_basenode = null;

/**
* @var array List all supported methods
*/
Expand Down Expand Up @@ -165,8 +170,21 @@ protected function response($data = array(), $http_status = null)
// Set the correct format header
$this->response->set_header('Content-Type', $this->_supported_formats[$this->format]);

// Set the formatted response
$this->response->body(\Format::forge($data)->{'to_'.$this->format}());
// Handle XML output
if ($this->format === 'xml')
{
// Detect basenode
$xml_basenode = $this->xml_basenode;
$xml_basenode or $xml_basenode = \Config::get('format.xml.basenode', 'xml');

// Set the XML response
$this->response->body(\Format::forge($data)->{'to_'.$this->format}(null, null, $xml_basenode));
}
else
{
// Set the formatted response
$this->response->body(\Format::forge($data)->{'to_'.$this->format}());
}

// Set the reponse http status
$http_status and $this->response->status = $http_status;
Expand Down
10 changes: 6 additions & 4 deletions classes/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ public function to_json($data = null, $pretty = false)
/**
* To JSONP conversion
*
* @param mixed $data
* @param bool wether to make the json pretty
* @return string
* @param mixed $data
* @param bool $pretty wether to make the json pretty
* @param string $callback JSONP callback
* @return string formatted JSONP
*/
public function to_jsonp($data = null, $pretty = false)
public function to_jsonp($data = null, $pretty = false, $callback = null)
{
$callback = \Input::param('callback');
is_null($callback) and $callback = 'response';
Expand Down Expand Up @@ -433,6 +434,7 @@ private function _from_serialize($string)
protected static function pretty_json($data)
{
$json = json_encode($data);

if ( ! $json)
{
return false;
Expand Down
3 changes: 3 additions & 0 deletions config/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
'regex_newline' => '\n',
'escape' => '\\',
),
'xml' => array(
'basenode' => 'xml',
),
);

0 comments on commit 8fdbe96

Please sign in to comment.