Skip to content

Commit

Permalink
Broken development commit. Introducing proper content negotiation fac…
Browse files Browse the repository at this point in the history
…ilities.
  • Loading branch information
KrisJordan committed May 4, 2009
1 parent 7d6bea6 commit b032776
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 6 deletions.
6 changes: 1 addition & 5 deletions recess/recess/framework/DefaultPolicy.class.php
Expand Up @@ -24,11 +24,7 @@ public function preprocess(Request &$request) {
if($request->format != Formats::XHTML) {
$this->reparameterizeForFormat($request);
}

// if($request->method == Methods::OPTIONS) {
// $response = new
// }


return $request;
}

Expand Down
43 changes: 43 additions & 0 deletions recess/recess/http/ContentNegotiation.class.php
@@ -0,0 +1,43 @@
<?php
class ContentNegotiation {

protected $headers;

public function __construct($httpHeaders) {
$this->headers = $httpHeaders;
}

public function nextFormat() {
return 'html';
}

public function resetFormats() {

}

public function nextLanguage() {
return 'en';
}

public function resetLanguages() {

}

public function nextEncoding() {
return 'gzip';
}

public function resetEncodings() {

}

public function nextCharset() {
return 'utf-8';
}

public function resetCharset() {

}

}
?>
4 changes: 3 additions & 1 deletion recess/recess/http/Environment.class.php
Expand Up @@ -20,7 +20,7 @@ public static function getRawRequest() {
$request->method = $_SERVER['REQUEST_METHOD'];

$request->format = Formats::XHTML;

$request->setResource(self::stripQueryString($_SERVER['REQUEST_URI']));

$request->get = $_GET;
Expand All @@ -42,6 +42,8 @@ public static function getRawRequest() {

$request->headers = self::getHttpRequestHeaders();

$request->accepts = new Accepts($request->headers);

$request->username = @$_SERVER['PHP_AUTH_USER'];

$request->password = @$_SERVER['PHP_AUTH_PW'];
Expand Down
35 changes: 35 additions & 0 deletions recess/recess/http/MimeType.class.php
@@ -0,0 +1,35 @@
<?php
abstract class MimeType {

static function init() {
// TODO: Cache the MIME Type Data Structure
MimeType::registerMany(array(
array('all', '*/*'),
array('text', 'text/plain', array(), array('txt')),
array('html', 'text/html', array('application/xhtml+xml'), array('xhtml')),
array('js', 'text/javascript', array('application/javascript', 'application/x-javascript')),
array('css', 'text/css'),
array('ics', 'text/calendar'),
array('csv', 'text/csv'),
array('xml', 'application/xml', array('text/xml', 'application/x-xml')),
array('rss', 'application/rss+xml'),
array('atom', 'application/atom+xml'),
array('yaml', 'application/x-yaml', array('text/yaml')),
array('multipart_form', 'multipart/form-data'),
array('url_encoded_form', 'application/x-www-form-urlencoded'),
array('json', 'application/json', array('text/x-json','application/jsonrequest')),
));
}

static function register($type, $extension, $synonyms = array()) {
self::registerMany(array(array($type,$extension,$synonyms)));
}

static function registerMany($array) {

}

}

MimeType::init();
?>
2 changes: 2 additions & 0 deletions recess/recess/http/Request.class.php
Expand Up @@ -10,6 +10,8 @@
*/
class Request {

public $accepts;

public $format;
public $headers;
public $resource;
Expand Down
22 changes: 22 additions & 0 deletions recess/test/recess/http/ContentNegotiationTest.php
@@ -0,0 +1,22 @@
<?php
Library::import('recess.http.ContentNegotiation');

class ContentNegotiationTest extends PHPUnit_Framework_TestCase {

function testAccepts() {
$negotiator =
new ContentNegotiation(
array( 'ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
'ACCEPT_ENCODING' => 'gzip,deflate',
'ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
);

$this->assertEquals('things', Inflector::toPlural('thing'));
$this->assertEquals('persons', Inflector::toPlural('person'));
$this->assertEquals('oxs', Inflector::toPlural('ox'));
}

}

?>
16 changes: 16 additions & 0 deletions recess/test/recess/http/RecessHttpTests.php
@@ -0,0 +1,16 @@
<?php
require_once 'PHPUnit/Framework.php';
require_once 'recess/http/ContentNegotiationTest.php';

class RecessHttpAllTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('recess.http');

$suite->addTestSuite('ContentNegotiationTest');

return $suite;
}
}
?>

0 comments on commit b032776

Please sign in to comment.