Skip to content

Commit

Permalink
a lot of new features and bug fixes!
Browse files Browse the repository at this point in the history
  • Loading branch information
jerfowler committed Mar 1, 2012
1 parent b57decc commit 696d129
Show file tree
Hide file tree
Showing 29 changed files with 747 additions and 375 deletions.
6 changes: 5 additions & 1 deletion README.md
@@ -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
51 changes: 9 additions & 42 deletions classes/controller/rest.php
@@ -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 {}
59 changes: 59 additions & 0 deletions classes/controller/template/rest.php
@@ -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);
}

}
108 changes: 75 additions & 33 deletions classes/model/rest/test.php
@@ -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);
}
}
9 changes: 9 additions & 0 deletions classes/rest.php
@@ -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 { }
6 changes: 3 additions & 3 deletions classes/rest/content/atom.php
@@ -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();
}
13 changes: 13 additions & 0 deletions classes/rest/content/csv.php
@@ -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();
}
6 changes: 3 additions & 3 deletions classes/rest/content/html.php
@@ -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();
}
6 changes: 3 additions & 3 deletions classes/rest/content/json.php
@@ -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();
}
6 changes: 3 additions & 3 deletions classes/rest/content/rdf.php
@@ -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();
}

0 comments on commit 696d129

Please sign in to comment.