Skip to content

Commit

Permalink
Merge pull request #50 from koriym/feature/transfer-server
Browse files Browse the repository at this point in the history
[BC BREAK] add $_SERVER for transfer()
  • Loading branch information
koriym committed Jan 13, 2015
2 parents f63b64e + f626fad commit 28219f7
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 30 deletions.
4 changes: 2 additions & 2 deletions default-app/www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$app = (new Injector(new SundayModule))->getInstance(AppInterface::class);
/** @var $app \BEAR\Sunday\Extension\Application\AbstractApp */
$request = $app->router->match($GLOBALS);
$request = $app->router->match($GLOBALS, $_SERVER);

try {
// resource request
Expand All @@ -22,7 +22,7 @@
/** @var $page \BEAR\Resource\Request */

// representation transfer
$page()->transfer($app->responder);
$page()->transfer($app->responder, $_SERVER);
exit(0);
} catch (\Exception $e) {
$errorPage = $app->error->handle($e, $request);
Expand Down
4 changes: 2 additions & 2 deletions docs/demo/MyVendor/HelloWorld/www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$app = (new Injector(new AppModule))->getInstance(AppInterface::class);
/** @var $app AbstractApp */

$request = $app->router->match($GLOBALS);
$request = $app->router->match($GLOBALS, $_SERVER);
try {
// resource request
$page = $app->resource
Expand All @@ -22,7 +22,7 @@
/** @var $page Request */

// representation transfer
$page()->transfer($app->responder);
$page()->transfer($app->responder, $_SERVER);

} catch (\Exception $e) {
$code = $e->getCode() ?: 500;
Expand Down
5 changes: 3 additions & 2 deletions src/Extension/Router/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
interface RouterInterface extends ExtensionInterface
{
/**
* @param array $globals
* @param array $globals $GLOBALS
* @param array $server $_SERVER
*
* @return RouterMatch
*/
public function match(array $globals);
public function match(array $globals, array $server);
}
6 changes: 3 additions & 3 deletions src/Provide/Router/WebRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class WebRouter implements RouterInterface
/**
* {@inheritdoc}
*/
public function match(array $globals = [])
public function match(array $globals, array $server)
{
$request = new RouterMatch;
$method = strtolower($globals['_SERVER']['REQUEST_METHOD']);
$method = strtolower($server['REQUEST_METHOD']);
list($request->method, $request->path, $request->query) = [
$method,
'page://self' . parse_url($globals['_SERVER']['REQUEST_URI'], PHP_URL_PATH),
'page://self' . parse_url($server['REQUEST_URI'], PHP_URL_PATH),
($method === 'get') ? $globals['_GET'] : $globals['_POST']
];

Expand Down
2 changes: 1 addition & 1 deletion src/Provide/Transfer/HttpResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HttpResponder implements TransferInterface
/**
* {@inheritdoc}
*/
public function __invoke(ResourceObject $resourceObject)
public function __invoke(ResourceObject $resourceObject, array $server)
{
// code
http_response_code($resourceObject->code);
Expand Down
2 changes: 1 addition & 1 deletion src/Provide/Transfer/JsonResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JsonResponder implements TransferInterface
/**
* {@inheritdoc}
*/
public function __invoke(ResourceObject $resourceObject)
public function __invoke(ResourceObject $resourceObject, array $server)
{
// code
http_response_code($resourceObject->code);
Expand Down
23 changes: 11 additions & 12 deletions tests/Provide/Router/WebRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public function setUp()
public function testMatchRoot()
{
$global = [
'_SERVER' => [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/'

],
'_GET' => []
];
$request = $this->router->match($global);
$server = [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/'

];
$request = $this->router->match($global, $server);
$this->assertSame('get', $request->method);
$this->assertSame('page://self/', $request->path);
$this->assertSame([], $request->query);
Expand All @@ -34,17 +34,16 @@ public function testMatchRoot()
public function testMatchWithQuery()
{
$global = [
'_SERVER' => [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/?id=1'

],
'_GET' => [
'id' => 1

]
];
$request = $this->router->match($global);
$server = [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/?id=1'
];
$request = $this->router->match($global, $server);
$this->assertSame('get', $request->method);
$this->assertSame('page://self/', $request->path);
$this->assertSame(['id' => 1], $request->query);
Expand Down
5 changes: 3 additions & 2 deletions tests/Provide/Transfer/FakeHttpResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public static function reset()
static::$headers = [];
static::$content = null;
}
public function __invoke(ResourceObject $resourceObject)

public function __invoke(ResourceObject $resourceObject, array $server)
{
ob_start();
parent::__invoke($resourceObject);
parent::__invoke($resourceObject, $server);
$body = ob_get_clean();
self::$content = $body;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Provide/Transfer/FakeJsonResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public static function reset()
static::$headers = [];
static::$content = null;
}
public function __invoke(ResourceObject $resourceObject)
public function __invoke(ResourceObject $resourceObject, array $server)
{
ob_start();
parent::__invoke($resourceObject);
parent::__invoke($resourceObject, $server);
$body = ob_get_clean();
self::$content = $body;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Provide/Transfer/HttpResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp()
public function testTransfer()
{
$ro = (new FakeResource)->onGet();
$ro->transfer($this->responder);
$ro->transfer($this->responder, []);
$expectedArgs = [['Cache-Control: max-age=0', false]];
$this->assertEquals($expectedArgs, FakeHttpResponder::$headers);
$expect = '{"greeting":"hello world"}';
Expand Down
4 changes: 2 additions & 2 deletions tests/Provide/Transfer/JsonResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp()
public function testTransfer()
{
$ro = (new FakeResource)->onGet();
$ro->transfer($this->responder);
$ro->transfer($this->responder, []);
$expect = '{"greeting":"hello world"}';
$actual = FakeJsonResponder::$content;
$this->assertSame($expect, $actual);
Expand All @@ -30,7 +30,7 @@ public function testTransfer()
public function testRfc4627()
{
$ro = (new FakeResource)->onGet();
$ro->transfer($this->responder);
$ro->transfer($this->responder, []);
$expectedArgs = 'Content-Type: application/json; charset=utf-8';
$this->assertEquals($expectedArgs, FakeHttpResponder::$headers[1][0]);

Expand Down

0 comments on commit 28219f7

Please sign in to comment.