From f626fad4ad40d89ddc5ea3263f2324ebb668cd58 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 14 Jan 2015 02:40:38 +0900 Subject: [PATCH] add $_SERVER for transfer() as second param --- default-app/www/index.php | 4 ++-- docs/demo/MyVendor/HelloWorld/www/index.php | 4 ++-- src/Extension/Router/RouterInterface.php | 5 +++-- src/Provide/Router/WebRouter.php | 6 ++--- src/Provide/Transfer/HttpResponder.php | 2 +- src/Provide/Transfer/JsonResponder.php | 2 +- tests/Provide/Router/WebRouterTest.php | 23 ++++++++++---------- tests/Provide/Transfer/FakeHttpResponder.php | 5 +++-- tests/Provide/Transfer/FakeJsonResponder.php | 4 ++-- tests/Provide/Transfer/HttpResponderTest.php | 2 +- tests/Provide/Transfer/JsonResponderTest.php | 4 ++-- 11 files changed, 31 insertions(+), 30 deletions(-) diff --git a/default-app/www/index.php b/default-app/www/index.php index 9ff1d127..f95c2c05 100644 --- a/default-app/www/index.php +++ b/default-app/www/index.php @@ -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 @@ -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); diff --git a/docs/demo/MyVendor/HelloWorld/www/index.php b/docs/demo/MyVendor/HelloWorld/www/index.php index ca2018b6..52f1d2c1 100644 --- a/docs/demo/MyVendor/HelloWorld/www/index.php +++ b/docs/demo/MyVendor/HelloWorld/www/index.php @@ -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 @@ -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; diff --git a/src/Extension/Router/RouterInterface.php b/src/Extension/Router/RouterInterface.php index 43ea16ab..cc389ba0 100644 --- a/src/Extension/Router/RouterInterface.php +++ b/src/Extension/Router/RouterInterface.php @@ -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); } diff --git a/src/Provide/Router/WebRouter.php b/src/Provide/Router/WebRouter.php index a2a97a99..57f01f70 100644 --- a/src/Provide/Router/WebRouter.php +++ b/src/Provide/Router/WebRouter.php @@ -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'] ]; diff --git a/src/Provide/Transfer/HttpResponder.php b/src/Provide/Transfer/HttpResponder.php index efd9853a..0090e121 100644 --- a/src/Provide/Transfer/HttpResponder.php +++ b/src/Provide/Transfer/HttpResponder.php @@ -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); diff --git a/src/Provide/Transfer/JsonResponder.php b/src/Provide/Transfer/JsonResponder.php index da7f074f..7637d78b 100644 --- a/src/Provide/Transfer/JsonResponder.php +++ b/src/Provide/Transfer/JsonResponder.php @@ -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); diff --git a/tests/Provide/Router/WebRouterTest.php b/tests/Provide/Router/WebRouterTest.php index b6b7c9bc..f7dbd39b 100644 --- a/tests/Provide/Router/WebRouterTest.php +++ b/tests/Provide/Router/WebRouterTest.php @@ -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); @@ -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); diff --git a/tests/Provide/Transfer/FakeHttpResponder.php b/tests/Provide/Transfer/FakeHttpResponder.php index 9ca462d1..9036846b 100644 --- a/tests/Provide/Transfer/FakeHttpResponder.php +++ b/tests/Provide/Transfer/FakeHttpResponder.php @@ -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; } diff --git a/tests/Provide/Transfer/FakeJsonResponder.php b/tests/Provide/Transfer/FakeJsonResponder.php index 96740f19..59544437 100644 --- a/tests/Provide/Transfer/FakeJsonResponder.php +++ b/tests/Provide/Transfer/FakeJsonResponder.php @@ -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; } diff --git a/tests/Provide/Transfer/HttpResponderTest.php b/tests/Provide/Transfer/HttpResponderTest.php index 1c81cccc..f7caa60e 100644 --- a/tests/Provide/Transfer/HttpResponderTest.php +++ b/tests/Provide/Transfer/HttpResponderTest.php @@ -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"}'; diff --git a/tests/Provide/Transfer/JsonResponderTest.php b/tests/Provide/Transfer/JsonResponderTest.php index 55beadfe..76f6290f 100644 --- a/tests/Provide/Transfer/JsonResponderTest.php +++ b/tests/Provide/Transfer/JsonResponderTest.php @@ -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); @@ -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]);