From 688414270391e2425b9ab4afc4f99eabd7a048d8 Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Mon, 16 Oct 2017 08:49:41 +0200 Subject: [PATCH 1/5] basic cookie parser --- examples/psr7.php | 16 ++++++++++++++++ examples/symfony.php | 20 ++++++++++++++++++++ src/AbstractRequestParserFactory.php | 21 +++++++++++++++++++++ src/BaseControllerHelperTrait.php | 19 +++++++++++++++++++ src/Psr7/Psr7RequestParserFactory.php | 6 +++--- src/RequestParserFactory.php | 5 +++++ src/Symfony/SymfonyRequestParserFactory.php | 6 +++--- 7 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/AbstractRequestParserFactory.php diff --git a/examples/psr7.php b/examples/psr7.php index bca0e48..cfb7200 100644 --- a/examples/psr7.php +++ b/examples/psr7.php @@ -7,6 +7,7 @@ // - Open in browser: // | http://localhost:8080/psr7.php?action=hello // | http://localhost:8080/psr7.php?action=hello&name=yourname +// | http://localhost:8080/psr7.php?action=helloFromCookie // | http://localhost:8080/psr7.php?action=helloWithDefault // | http://localhost:8080/psr7.php?action=json&payload={%22a%22:1} @@ -37,6 +38,14 @@ public function hello() return "Hello $name"; } + public function helloFromCookie() + { + $this->setCookie(); + $fullName = $this->cookieParameter('fullName')->string()->required(); + + return "Hello $fullName"; + } + public function helloWithDefault() { $name = $this->queryParameter('name')->string()->defaultsTo('unknown'); @@ -50,6 +59,13 @@ public function json() return print_r($payload, true); } + + private function setCookie() + { + $cookie_name = "fullName"; + $cookie_value = "John Doe"; + setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); + } } $controller = new MyController($request); diff --git a/examples/symfony.php b/examples/symfony.php index 03b6cfb..c570a89 100644 --- a/examples/symfony.php +++ b/examples/symfony.php @@ -8,6 +8,7 @@ // | http://localhost:8080/symfony.php?action=hello // | http://localhost:8080/symfony.php?action=hello&name=yourname // | http://localhost:8080/symfony.php?action=helloWithDefault +// | http://localhost:8080/symfony.php?action=helloFromCookie // | http://localhost:8080/symfony.php?action=json&payload={%22a%22:1} // | http://localhost:8080/symfony.php?action=intArray&userIds=21,22,23 // | http://localhost:8080/symfony.php?action=dateTimeArray×tamps=2016-01-01%2000:00:00,2016-12-31%2023:59:59 @@ -26,7 +27,11 @@ class MyController public function __construct(\Symfony\Component\HttpFoundation\Request $request) { + $cookie_name = "user"; + $cookie_value = "John Doe"; + setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day $this->initRequestParser($request); + $a = $this->cookieParser; } public function hello() @@ -36,6 +41,14 @@ public function hello() return "Hello $name"; } + public function helloFromCookie() + { + $this->setCookie(); + $fullName = $this->cookieParameter('fullName')->string()->required(); + + return "Hello $fullName"; + } + public function helloWithDefault() { $name = $this->queryParameter('name')->string()->defaultsTo('unknown'); @@ -73,6 +86,13 @@ public function jsonArray() $userIds = $this->queryParameter('events')->commaSeparated()->json()->required(); return print_r($userIds, true); } + + private function setCookie() + { + $cookie_name = "fullName"; + $cookie_value = "John Doe"; + setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); + } } $controller = new MyController($request); diff --git a/src/AbstractRequestParserFactory.php b/src/AbstractRequestParserFactory.php new file mode 100644 index 0000000..fce08eb --- /dev/null +++ b/src/AbstractRequestParserFactory.php @@ -0,0 +1,21 @@ +config = $config; + } + + public function createCookieParser() + { + $readParameter = function ($name) { + return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null; + }; + return new RequestParser($readParameter, $this->config); + } +} diff --git a/src/BaseControllerHelperTrait.php b/src/BaseControllerHelperTrait.php index fe1dc3e..908dacd 100644 --- a/src/BaseControllerHelperTrait.php +++ b/src/BaseControllerHelperTrait.php @@ -14,12 +14,18 @@ trait BaseControllerHelperTrait */ private $bodyParser; + /** + * @var RequestParser + */ + private $cookieParser; + protected final function initRequestParser($request, $config = null) { /** @var $requestParserFactory RequestParserFactory */ $requestParserFactory = $this->createRequestParserFactory($request, $config); $this->queryParser = $requestParserFactory->createQueryParser(); $this->bodyParser = $requestParserFactory->createBodyParser(); + $this->cookieParser = $requestParserFactory->createCookieParser(); } /** @@ -47,4 +53,17 @@ protected function bodyParameter($name) { return $this->bodyParser->get($name); } + + /** + * Use this method to access the cookie parameters (e.g. $_COOKIE). + * + * $userId = $this->cookieParameter('userId')->int()->defaultsTo(null) + * + * @param $name + * @return TypeParser + */ + protected function cookieParameter($name) + { + return $this->cookieParser->get($name); + } } diff --git a/src/Psr7/Psr7RequestParserFactory.php b/src/Psr7/Psr7RequestParserFactory.php index 9ef3724..91b84b5 100644 --- a/src/Psr7/Psr7RequestParserFactory.php +++ b/src/Psr7/Psr7RequestParserFactory.php @@ -2,19 +2,19 @@ namespace MPScholten\RequestParser\Psr7; +use MPScholten\RequestParser\AbstractRequestParserFactory; use MPScholten\RequestParser\RequestParser; use MPScholten\RequestParser\RequestParserFactory; use Psr\Http\Message\ServerRequestInterface; -class Psr7RequestParserFactory implements RequestParserFactory +class Psr7RequestParserFactory extends AbstractRequestParserFactory implements RequestParserFactory { private $request; - private $config; public function __construct(ServerRequestInterface $request, $config = null) { + parent::__construct($config); $this->request = $request; - $this->config = $config; } public function createQueryParser() diff --git a/src/RequestParserFactory.php b/src/RequestParserFactory.php index 17eeb78..291d6ff 100644 --- a/src/RequestParserFactory.php +++ b/src/RequestParserFactory.php @@ -13,4 +13,9 @@ public function createQueryParser(); * @return RequestParser */ public function createBodyParser(); + + /** + * @return RequestParser + */ + public function createCookieParser(); } diff --git a/src/Symfony/SymfonyRequestParserFactory.php b/src/Symfony/SymfonyRequestParserFactory.php index 3ad35a4..c5ca478 100644 --- a/src/Symfony/SymfonyRequestParserFactory.php +++ b/src/Symfony/SymfonyRequestParserFactory.php @@ -2,19 +2,19 @@ namespace MPScholten\RequestParser\Symfony; +use MPScholten\RequestParser\AbstractRequestParserFactory; use MPScholten\RequestParser\RequestParser; use MPScholten\RequestParser\RequestParserFactory; use Symfony\Component\HttpFoundation\Request; -class SymfonyRequestParserFactory implements RequestParserFactory +class SymfonyRequestParserFactory extends AbstractRequestParserFactory implements RequestParserFactory { private $request; - private $config; public function __construct(Request $request, $config = null) { + parent::__construct($config); $this->request = $request; - $this->config = $config; } public function createQueryParser() From f01f7f4640ce46a9ca911c9738fe3177b244698c Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Mon, 16 Oct 2017 09:59:42 +0200 Subject: [PATCH 2/5] test cookies in integration tests --- examples/psr7.php | 6 +++--- examples/symfony.php | 6 +++--- tests/Integration/setup.php | 35 +++++++++++++++++++++++++++-------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/examples/psr7.php b/examples/psr7.php index cfb7200..83b0007 100644 --- a/examples/psr7.php +++ b/examples/psr7.php @@ -62,9 +62,9 @@ public function json() private function setCookie() { - $cookie_name = "fullName"; - $cookie_value = "John Doe"; - setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); + $cookieName = "fullName"; + $cookieValue = "John Doe"; + setcookie($cookieName, $cookieValue, time() + (86400 * 30), "/"); } } diff --git a/examples/symfony.php b/examples/symfony.php index c570a89..39732d5 100644 --- a/examples/symfony.php +++ b/examples/symfony.php @@ -89,9 +89,9 @@ public function jsonArray() private function setCookie() { - $cookie_name = "fullName"; - $cookie_value = "John Doe"; - setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); + $cookieName = "fullName"; + $cookieValue = "John Doe"; + setcookie($cookieName, $cookieValue, time() + (86400 * 30), "/"); } } diff --git a/tests/Integration/setup.php b/tests/Integration/setup.php index b57bd52..66ddf63 100644 --- a/tests/Integration/setup.php +++ b/tests/Integration/setup.php @@ -5,22 +5,30 @@ abstract class AbstractIntegrationTest extends \PHPUnit_Framework_TestCase abstract public function controllersProvider(); /** + * @param AbstractIntegrationTestController $controller + * * @dataProvider controllersProvider */ public function testStringAction(AbstractIntegrationTestController $controller) { $this->assertEquals('Test', $controller->testString()); + $this->assertEquals('Test', $controller->testString(true)); } /** + * @param AbstractIntegrationTestController $controller + * * @dataProvider controllersProvider */ public function testIntAction(AbstractIntegrationTestController $controller) { $this->assertEquals(1, $controller->testInt()); + $this->assertEquals(1, $controller->testInt(true)); } /** + * @param AbstractIntegrationTestController $controller + * * @dataProvider controllersProvider */ public function testNotFoundActionThrowsException(AbstractIntegrationTestController $controller) @@ -36,27 +44,38 @@ abstract class AbstractIntegrationTestController public function __construct($request) { + $this->setCookies(); $this->initRequestParser($request); $this->request = $request; } - protected function parameter($name) + protected function parameter($name, $cookie = false) { - if ($this->request->getMethod() === 'GET') { - return $this->queryParameter($name); + if (!$cookie) { + if ($this->request->getMethod() === 'GET') { + return $this->queryParameter($name); + } else { + return $this->bodyParameter($name); + } } else { - return $this->bodyParameter($name); + return $this->cookieParameter($name); } } - public function testString() + private function setCookies() { - return $this->parameter('string')->string()->required(); + $_COOKIE['string'] = 'Test'; + $_COOKIE['int'] = 1; } - public function testInt() + public function testString($fromCookie = false) { - return $this->parameter('int')->int()->required(); + return $this->parameter('string', $fromCookie)->string()->required(); + } + + public function testInt($fromCookie = false) + { + return $this->parameter('int', $fromCookie)->int()->required(); } public function testNotFound() From c8f91c1a2e1e1fa3bd71820fc0626d1c658efe6d Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Mon, 16 Oct 2017 10:12:24 +0200 Subject: [PATCH 3/5] get cookies from actual request --- src/AbstractRequestParserFactory.php | 21 -------------------- src/Psr7/Psr7RequestParserFactory.php | 20 ++++++++++++++++--- src/Symfony/SymfonyRequestParserFactory.php | 16 ++++++++++++--- tests/Integration/Psr7IntegrationTest.php | 2 ++ tests/Integration/SymfonyIntegrationTest.php | 4 ++-- 5 files changed, 34 insertions(+), 29 deletions(-) delete mode 100644 src/AbstractRequestParserFactory.php diff --git a/src/AbstractRequestParserFactory.php b/src/AbstractRequestParserFactory.php deleted file mode 100644 index fce08eb..0000000 --- a/src/AbstractRequestParserFactory.php +++ /dev/null @@ -1,21 +0,0 @@ -config = $config; - } - - public function createCookieParser() - { - $readParameter = function ($name) { - return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null; - }; - return new RequestParser($readParameter, $this->config); - } -} diff --git a/src/Psr7/Psr7RequestParserFactory.php b/src/Psr7/Psr7RequestParserFactory.php index 91b84b5..9021ff9 100644 --- a/src/Psr7/Psr7RequestParserFactory.php +++ b/src/Psr7/Psr7RequestParserFactory.php @@ -2,19 +2,19 @@ namespace MPScholten\RequestParser\Psr7; -use MPScholten\RequestParser\AbstractRequestParserFactory; use MPScholten\RequestParser\RequestParser; use MPScholten\RequestParser\RequestParserFactory; use Psr\Http\Message\ServerRequestInterface; -class Psr7RequestParserFactory extends AbstractRequestParserFactory implements RequestParserFactory +class Psr7RequestParserFactory implements RequestParserFactory { private $request; + private $config; public function __construct(ServerRequestInterface $request, $config = null) { - parent::__construct($config); $this->request = $request; + $this->config = $config; } public function createQueryParser() @@ -46,4 +46,18 @@ public function createBodyParser() return new RequestParser($readParameter, $this->config); } + + public function createCookieParser() + { + $cookies = $this->request->getCookieParams(); + $readParameter = function ($name) use ($cookies) { + if (!isset($cookies[$name])) { + return null; + } + + return $cookies[$name]; + }; + + return new RequestParser($readParameter, $this->config); + } } diff --git a/src/Symfony/SymfonyRequestParserFactory.php b/src/Symfony/SymfonyRequestParserFactory.php index c5ca478..78e437d 100644 --- a/src/Symfony/SymfonyRequestParserFactory.php +++ b/src/Symfony/SymfonyRequestParserFactory.php @@ -2,19 +2,19 @@ namespace MPScholten\RequestParser\Symfony; -use MPScholten\RequestParser\AbstractRequestParserFactory; use MPScholten\RequestParser\RequestParser; use MPScholten\RequestParser\RequestParserFactory; use Symfony\Component\HttpFoundation\Request; -class SymfonyRequestParserFactory extends AbstractRequestParserFactory implements RequestParserFactory +class SymfonyRequestParserFactory implements RequestParserFactory { private $request; + private $config; public function __construct(Request $request, $config = null) { - parent::__construct($config); $this->request = $request; + $this->config = $config; } public function createQueryParser() @@ -34,4 +34,14 @@ public function createBodyParser() return new RequestParser($readParameter, $this->config); } + + public function createCookieParser() + { + $readParameter = function ($name) { + $cookies = $this->request->cookies; + return $cookies->has($name) ? $cookies->get($name) : null; + }; + + return new RequestParser($readParameter, $this->config); + } } diff --git a/tests/Integration/Psr7IntegrationTest.php b/tests/Integration/Psr7IntegrationTest.php index 8395d1a..677f1fb 100644 --- a/tests/Integration/Psr7IntegrationTest.php +++ b/tests/Integration/Psr7IntegrationTest.php @@ -15,10 +15,12 @@ public function controllersProvider() $getRequest = $request ->withMethod('GET') + ->withCookieParams($parameters) ->withQueryParams($parameters); $postRequest = $request ->withMethod('POST') + ->withCookieParams($parameters) ->withParsedBody($parameters); return [ diff --git a/tests/Integration/SymfonyIntegrationTest.php b/tests/Integration/SymfonyIntegrationTest.php index d5b4306..36782e5 100644 --- a/tests/Integration/SymfonyIntegrationTest.php +++ b/tests/Integration/SymfonyIntegrationTest.php @@ -11,10 +11,10 @@ public function controllersProvider() 'int' => 1 ]; - $getRequest = new \Symfony\Component\HttpFoundation\Request($parameters); + $getRequest = new \Symfony\Component\HttpFoundation\Request($parameters, [], [], $parameters); $getRequest->setMethod('GET'); - $postRequest = new \Symfony\Component\HttpFoundation\Request([], $parameters); + $postRequest = new \Symfony\Component\HttpFoundation\Request([], $parameters, [], $parameters); $postRequest->setMethod('POST'); return [ From 074b7aa7d3648bd13d9eb4dd5e16779d9e0312ee Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Mon, 16 Oct 2017 10:17:10 +0200 Subject: [PATCH 4/5] update examples --- examples/psr7.php | 9 +-------- examples/symfony.php | 13 +------------ 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/examples/psr7.php b/examples/psr7.php index 83b0007..80aa493 100644 --- a/examples/psr7.php +++ b/examples/psr7.php @@ -19,6 +19,7 @@ use Symfony\Component\HttpFoundation\Request; $symfonyRequest = Request::createFromGlobals(); +$symfonyRequest->cookies->add(["user", "John Doe"]); $psr7Factory = new DiactorosFactory(); $request = $psr7Factory->createRequest($symfonyRequest); @@ -40,7 +41,6 @@ public function hello() public function helloFromCookie() { - $this->setCookie(); $fullName = $this->cookieParameter('fullName')->string()->required(); return "Hello $fullName"; @@ -59,13 +59,6 @@ public function json() return print_r($payload, true); } - - private function setCookie() - { - $cookieName = "fullName"; - $cookieValue = "John Doe"; - setcookie($cookieName, $cookieValue, time() + (86400 * 30), "/"); - } } $controller = new MyController($request); diff --git a/examples/symfony.php b/examples/symfony.php index 39732d5..0c667d4 100644 --- a/examples/symfony.php +++ b/examples/symfony.php @@ -27,11 +27,8 @@ class MyController public function __construct(\Symfony\Component\HttpFoundation\Request $request) { - $cookie_name = "user"; - $cookie_value = "John Doe"; - setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day + $request->cookies->add(["user", "John Doe"]); $this->initRequestParser($request); - $a = $this->cookieParser; } public function hello() @@ -43,7 +40,6 @@ public function hello() public function helloFromCookie() { - $this->setCookie(); $fullName = $this->cookieParameter('fullName')->string()->required(); return "Hello $fullName"; @@ -86,13 +82,6 @@ public function jsonArray() $userIds = $this->queryParameter('events')->commaSeparated()->json()->required(); return print_r($userIds, true); } - - private function setCookie() - { - $cookieName = "fullName"; - $cookieValue = "John Doe"; - setcookie($cookieName, $cookieValue, time() + (86400 * 30), "/"); - } } $controller = new MyController($request); From 33a1cda26f35d31841b02815ab53971f62ff9b3b Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Mon, 16 Oct 2017 10:21:51 +0200 Subject: [PATCH 5/5] remove deadcode --- src/BaseControllerHelperTrait.php | 2 +- tests/Integration/setup.php | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/BaseControllerHelperTrait.php b/src/BaseControllerHelperTrait.php index 908dacd..03ff3f9 100644 --- a/src/BaseControllerHelperTrait.php +++ b/src/BaseControllerHelperTrait.php @@ -55,7 +55,7 @@ protected function bodyParameter($name) } /** - * Use this method to access the cookie parameters (e.g. $_COOKIE). + * Use this method to access the request cookie parameters. * * $userId = $this->cookieParameter('userId')->int()->defaultsTo(null) * diff --git a/tests/Integration/setup.php b/tests/Integration/setup.php index 66ddf63..78b19c4 100644 --- a/tests/Integration/setup.php +++ b/tests/Integration/setup.php @@ -44,7 +44,6 @@ abstract class AbstractIntegrationTestController public function __construct($request) { - $this->setCookies(); $this->initRequestParser($request); $this->request = $request; } @@ -62,12 +61,6 @@ protected function parameter($name, $cookie = false) } } - private function setCookies() - { - $_COOKIE['string'] = 'Test'; - $_COOKIE['int'] = 1; - } - public function testString($fromCookie = false) { return $this->parameter('string', $fromCookie)->string()->required();