Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a lot of new features and bug fixes!
- Loading branch information
Showing
29 changed files
with
747 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
#Kohana RESTful Module | ||
#Kohana RESTful Web Service Library. | ||
|
||
Author: Jeremy Fowler | ||
Copyright: (c) 2012 Jeremy Fowler | ||
License: http://www.opensource.org/licenses/BSD-3-Clause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,11 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Controller_REST extends Controller | ||
implements REST_Content_JSON, REST_Content_XML, REST_Content_HTML { | ||
|
||
/** | ||
* Rest object | ||
* @var Rest | ||
*/ | ||
protected $_rest; | ||
|
||
public function before() | ||
{ | ||
parent::before(); | ||
|
||
$this->_rest = REST::instance($this) | ||
->method_override(TRUE) | ||
->content_override(TRUE) | ||
->execute(); | ||
} | ||
|
||
public function action_json() | ||
{ | ||
$model = $this->_rest->model(); | ||
$this->response->body(json_encode($model->values())); | ||
} | ||
|
||
public function action_xml() | ||
{ | ||
$model = $this->_rest->model(); | ||
$name = Rest::common_name('model', $model); | ||
$xml = new SimpleXMLElement('<'.$name.'/>'); | ||
$values = $model->values(); | ||
array_walk_recursive($values, array ($xml, 'addChild')); | ||
$this->response->body($xml->asXML()); | ||
} | ||
|
||
public function action_html() | ||
{ | ||
$model = $this->_rest->model(); | ||
$this->response->body('<pre>'.print_r($model->values(), TRUE).'</pre>'); | ||
} | ||
|
||
} // End REST | ||
/** | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
class Controller_REST extends Controller_Template_REST {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
class Controller_Template_REST extends Controller | ||
implements REST_Content_HTML, | ||
REST_Content_JSON, | ||
REST_Content_XML, | ||
REST_Content_CSV { | ||
|
||
/** | ||
* Rest object | ||
* @var Rest | ||
*/ | ||
protected $_rest; | ||
|
||
public function before() | ||
{ | ||
parent::before(); | ||
|
||
$this->_rest = REST::instance($this) | ||
->method_override(TRUE) | ||
->content_override(TRUE) | ||
->execute(); | ||
} | ||
|
||
public function action_html() | ||
{ | ||
$values = $this->_rest->result(); | ||
$view = View::factory('rest/html', array('values' => $values)); | ||
$this->response->body($view); | ||
} | ||
|
||
public function action_json() | ||
{ | ||
$json = $this->_rest->etag()->result_json(); | ||
$this->response->body($json); | ||
} | ||
|
||
public function action_xml() | ||
{ | ||
$xml = $this->_rest->etag()->result_xml(); | ||
$this->response->body($xml->asXML()); | ||
} | ||
|
||
public function action_csv() | ||
{ | ||
$csv = $this->_rest->result_csv(); | ||
$this->response->body($csv); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,124 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Model_REST_Test implements REST_Method_Basic { | ||
class Model_REST_Test implements REST_CORS, REST_Method_Basic { | ||
|
||
protected static $origin = array( | ||
'http://example.com:3001', | ||
'http://www.example.com', | ||
'http://test.example.com' | ||
); | ||
|
||
/** | ||
* Sample data | ||
* | ||
* @var mixed | ||
*/ | ||
protected $_data = array( | ||
1 => array('one', 'two', 'three'), | ||
2 => array('four', 'five', 'six'), | ||
3 => array('seven', 'eight', 'nine') | ||
); | ||
|
||
protected $_values; | ||
|
||
/** | ||
* Cross-Origin Resource Sharing | ||
* | ||
* @param Rest $rest | ||
*/ | ||
public function rest_cors(Rest $rest) | ||
{ | ||
$origin = $rest->request()->headers('Origin'); | ||
if (in_array($origin, self::$origin)) | ||
{ | ||
$rest->cors(array('origin' => $origin, 'creds' => 'true')); | ||
} | ||
} | ||
|
||
/** | ||
* Cross-Origin Resource Sharing | ||
* | ||
* @param Rest $rest | ||
*/ | ||
public function rest_options(Rest $rest) | ||
{ | ||
$rest->send_code(200); | ||
} | ||
|
||
/** | ||
* Returns test data | ||
* | ||
* @param Rest $rest | ||
* @param Rest $rest | ||
*/ | ||
public function rest_get(Rest $rest) | ||
public function rest_get(Rest $rest) | ||
{ | ||
$data = Session::instance()->get('rest_test_data', $this->_data); | ||
$id = $rest->param('id'); | ||
if ( ! empty($id)) | ||
{ | ||
if ( ! isset($this->_data[$id])) | ||
if ( ! isset($data[$id])) | ||
{ | ||
throw new Http_Exception_404('Resource not found, ID: :id', array(':id' => $id)); | ||
} | ||
$this->_values = $this->_data[$id]; | ||
return $data[$id]; | ||
|
||
} | ||
else | ||
{ | ||
$this->_values = $this->_data; | ||
return $data; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param Rest $rest | ||
*/ | ||
public function rest_put(Rest $rest) | ||
{ | ||
// TODO | ||
$rest->send_code(403); //Forbidden | ||
} | ||
|
||
/** | ||
* | ||
* @param Rest $rest | ||
* @param Rest $rest | ||
*/ | ||
public function rest_post(Rest $rest) | ||
public function rest_put(Rest $rest) | ||
{ | ||
$id = $rest->param('id'); | ||
if ( ! empty($id)) | ||
{ | ||
// TODO | ||
$rest->send_code(403); //Forbidden | ||
$data = Session::instance()->get('rest_test_data', $this->_data); | ||
$data[$id] = $rest->body('json', TRUE); | ||
Session::instance()->set('rest_test_data', $data); | ||
return $data[$id]; | ||
} | ||
else | ||
{ | ||
$this->_data[] = $rest->post(); | ||
$rest->send_created(count($this->_data)+1); | ||
// TODO | ||
$rest->send_code(403); //Forbidden | ||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param Rest $rest | ||
* @param Rest $rest | ||
*/ | ||
public function rest_delete(Rest $rest) | ||
public function rest_post(Rest $rest) | ||
{ | ||
$id = $rest->param('id'); | ||
if (isset($this->_data[$id])) | ||
$data = Session::instance()->get('rest_test_data', $this->_data); | ||
$post = $rest->post(); | ||
if (empty($post)) | ||
{ | ||
unset($this->_data[$id]); | ||
$post = $rest->body('json', TRUE); | ||
} | ||
$rest->send_code(204); | ||
$data[] = $post; | ||
Session::instance()->set('rest_test_data', $data); | ||
$rest->send_created(count($data)+1); | ||
} | ||
|
||
public function values() | ||
/** | ||
* | ||
* @param Rest $rest | ||
*/ | ||
public function rest_delete(Rest $rest) | ||
{ | ||
return $this->_values; | ||
$id = $rest->param('id'); | ||
$data = Session::instance()->get('rest_test_data', $this->_data); | ||
if (isset($data[$id])) | ||
{ | ||
unset($data[$id]); | ||
Session::instance()->set('rest_test_data', $data); | ||
} | ||
$rest->send_code(204); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
<?php defined('SYSPATH') or die('No direct access allowed.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
class Rest extends Rest_Core { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php defined('SYSPATH') or die('No direct access allowed.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2011 Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
interface REST_Content_ATOM extends REST_Controller { | ||
public function action_atom(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php defined('SYSPATH') or die('No direct access allowed.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
interface REST_Content_CSV extends REST_Controller { | ||
public function action_csv(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php defined('SYSPATH') or die('No direct access allowed.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2011 Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
interface REST_Content_HTML extends REST_Controller { | ||
public function action_html(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php defined('SYSPATH') or die('No direct access allowed.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2011 Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
interface REST_Content_JSON extends REST_Controller { | ||
public function action_json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php defined('SYSPATH') or die('No direct access allowed.'); | ||
|
||
/** | ||
* RESTful web service library. | ||
* RESTful web service library. | ||
* | ||
* @package jeremyf76/REST | ||
* @author Jeremy Fowler | ||
* @copyright (c) 2011 Jeremy Fowler | ||
* @copyright (c) 2012 Jeremy Fowler | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
interface REST_Content_RDF extends REST_Controller { | ||
public function action_rdf(); | ||
} |
Oops, something went wrong.