Skip to content

Commit f9fd37a

Browse files
author
Phil Sturgeon
committed
Fixed up all .foo extensions to work when no get arguments provided, and moved .html to Format library.
1 parent c620ca6 commit f9fd37a

File tree

2 files changed

+56
-70
lines changed

2 files changed

+56
-70
lines changed

application/libraries/Format.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Format {
1818
/**
1919
* Returns an instance of the Format object.
2020
*
21-
* echo Format::factory(array('foo' => 'bar'))->to_xml();
21+
* echo $this->format->factory(array('foo' => 'bar'))->to_xml();
2222
*
2323
* @param mixed general date to be converted
2424
* @param string data format the file was provided in
@@ -140,32 +140,35 @@ public function to_xml($data = null, $structure = NULL, $basenode = 'xml')
140140
}
141141

142142
// Format HTML for output
143-
// private function to_html($data = array())
144-
// {
145-
// // Multi-dimentional array
146-
// if (isset($data[0]))
147-
// {
148-
// $headings = array_keys($data[0]);
149-
// }
150-
//
151-
// // Single array
152-
// else
153-
// {
154-
// $headings = array_keys($data);
155-
// $data = array($data);
156-
// }
157-
//
158-
// $this->load->library('table');
159-
//
160-
// $this->table->set_heading($headings);
161-
//
162-
// foreach ($data as &$row)
163-
// {
164-
// $this->table->add_row($row);
165-
// }
166-
//
167-
// return $this->table->generate();
168-
// }
143+
public function to_html()
144+
{
145+
$data = $this->_data;
146+
147+
// Multi-dimentional array
148+
if (isset($data[0]))
149+
{
150+
$headings = array_keys($data[0]);
151+
}
152+
153+
// Single array
154+
else
155+
{
156+
$headings = array_keys($data);
157+
$data = array($data);
158+
}
159+
160+
$ci = get_instance();
161+
$ci->load->library('table');
162+
163+
$ci->table->set_heading($headings);
164+
165+
foreach ($data as &$row)
166+
{
167+
$ci->table->add_row($row);
168+
}
169+
170+
return $ci->table->generate();
171+
}
169172

170173
// Format HTML for output
171174
public function to_csv()

application/libraries/REST_Controller.php

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function __construct()
3737
// How is this request being made? POST, DELETE, GET, PUT?
3838
$this->request->method = $this->_detect_method();
3939

40+
// Set up our GET variables
41+
$this->_get_args = array_merge($this->_get_args, $this->uri->ruri_to_assoc());
42+
4043
$this->load->library('security');
4144

4245
// This library is bundled with REST_Controller 2.5+, but will eventually be part of CodeIgniter itself
@@ -91,9 +94,6 @@ public function __construct()
9194
$this->request->body = $this->format->factory($this->request->body, $this->request->format)->to_array();
9295
}
9396

94-
// Set up our GET variables
95-
$this->_get_args = array_merge($this->_get_args, $this->uri->ruri_to_assoc());
96-
9797
// Merge both for one mega-args variable
9898
$this->_args = array_merge($this->_get_args, $this->_put_args, $this->_post_args, $this->_delete_args);
9999

@@ -146,6 +146,12 @@ public function __construct()
146146
*/
147147
public function _remap($object_called)
148148
{
149+
$pattern = '/^(.*)\.(' . implode('|', array_keys($this->_supported_formats)) . ')$/';
150+
if (preg_match($pattern, $object_called, $matches))
151+
{
152+
$object_called = $matches[1];
153+
}
154+
149155
$controller_method = $object_called . '_' . $this->request->method;
150156

151157
// Do we want to log this method (if allowed by config)?
@@ -223,20 +229,18 @@ public function response($data = array(), $http_code = null)
223229
if (method_exists($this, '_format_'.$this->response->format))
224230
{
225231
// Set the correct format header
226-
header('Content-type: '.$this->_supported_formats[$this->response->format]);
232+
header('Content-Type: '.$this->_supported_formats[$this->response->format]);
227233

228-
$formatted_data = $this->{'_format_'.$this->response->format}($data);
229-
$output = $formatted_data;
234+
$output = $this->{'_format_'.$this->response->format}($data);
230235
}
231236

232237
// If the format method exists, call and return the output in that format
233238
elseif (method_exists($this->format, 'to_'.$this->response->format))
234239
{
235240
// Set the correct format header
236-
header('Content-type: '.$this->_supported_formats[$this->response->format]);
241+
header('Content-Type: '.$this->_supported_formats[$this->response->format]);
237242

238-
$formatted_data = $this->format->factory($data)->{'to_'.$this->response->format}();
239-
$output = $formatted_data;
243+
$output = $this->format->factory($data)->{'to_'.$this->response->format}();
240244
}
241245

242246
// Format not supported, output directly
@@ -248,6 +252,7 @@ public function response($data = array(), $http_code = null)
248252

249253
header('HTTP/1.1: ' . $http_code);
250254
header('Status: ' . $http_code);
255+
header('Content-Length: ' . strlen($output));
251256

252257
exit($output);
253258
}
@@ -284,7 +289,13 @@ private function _detect_output_format()
284289
$pattern = '/\.(' . implode('|', array_keys($this->_supported_formats)) . ')$/';
285290

286291
// Check if a file extension is used
287-
if (preg_match($pattern, end($this->_get_args), $matches))
292+
if (preg_match($pattern, $this->uri->uri_string(), $matches))
293+
{
294+
return $matches[1];
295+
}
296+
297+
// Check if a file extension is used
298+
elseif ($this->_get_args AND preg_match($pattern, end($this->_get_args), $matches))
288299
{
289300
// The key of the last argument
290301
$last_key = end(array_keys($this->_get_args));
@@ -297,9 +308,9 @@ private function _detect_output_format()
297308
}
298309

299310
// A format has been passed as an argument in the URL and it is supported
300-
if (isset($this->_args['format']) AND isset($this->_supported_formats))
311+
if (isset($this->_get_args['format']) AND array_key_exists($this->_get_args['format'], $this->_supported_formats))
301312
{
302-
return $this->_args['format'];
313+
return $this->_get_args['format'];
303314
}
304315

305316
// Otherwise, check the HTTP_ACCEPT (if it exists and we are allowed)
@@ -336,6 +347,7 @@ private function _detect_output_format()
336347
}
337348
}
338349
} // End HTTP_ACCEPT checking
350+
339351
// Well, none of that has worked! Let's see if the controller has a default
340352
if ( ! empty($this->rest_format))
341353
{
@@ -485,7 +497,7 @@ private function _check_limit($controller_method)
485497
if (!$result OR $result->hour_started < time() - (60 * 60))
486498
{
487499
// Right, set one up from scratch
488-
$this->rest->db->insert('limits', array(
500+
$this->rest->db->insert(config_item('rest_limits_table'), array(
489501
'uri' => $this->uri->uri_string(),
490502
'api_key' => isset($this->rest->key) ? $this->rest->key : '',
491503
'count' => 1,
@@ -506,7 +518,7 @@ private function _check_limit($controller_method)
506518
->where('uri', $this->uri->uri_string())
507519
->where('api_key', $this->rest->key)
508520
->set('count', 'count + 1', FALSE)
509-
->update(config_item('limits'));
521+
->update(config_item('rest_limits_table'));
510522
}
511523

512524
return TRUE;
@@ -557,7 +569,6 @@ private function _auth_override_check()
557569

558570
// Return false when there is an override value set but it doesn't match 'basic', 'digest', or 'none'. (the value was misspelled)
559571
return false;
560-
561572
}
562573

563574

@@ -755,34 +766,6 @@ private function _force_loopable($data)
755766

756767
// Many of these have been moved to the Format class for better separation, but these methods will be checked too
757768

758-
// Format HTML for output
759-
private function _format_html($data = array())
760-
{
761-
// Multi-dimentional array
762-
if (isset($data[0]))
763-
{
764-
$headings = array_keys($data[0]);
765-
}
766-
767-
// Single array
768-
else
769-
{
770-
$headings = array_keys($data);
771-
$data = array($data);
772-
}
773-
774-
$this->load->library('table');
775-
776-
$this->table->set_heading($headings);
777-
778-
foreach ($data as &$row)
779-
{
780-
$this->table->add_row($row);
781-
}
782-
783-
return $this->table->generate();
784-
}
785-
786769
// Encode as JSONP
787770
private function _format_jsonp($data = array())
788771
{

0 commit comments

Comments
 (0)