Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request cookie parser #50

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/psr7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -18,6 +19,7 @@
use Symfony\Component\HttpFoundation\Request;

$symfonyRequest = Request::createFromGlobals();
$symfonyRequest->cookies->add(["user", "John Doe"]);
$psr7Factory = new DiactorosFactory();
$request = $psr7Factory->createRequest($symfonyRequest);

Expand All @@ -37,6 +39,13 @@ public function hello()
return "Hello $name";
}

public function helloFromCookie()
{
$fullName = $this->cookieParameter('fullName')->string()->required();

return "Hello $fullName";
}

public function helloWithDefault()
{
$name = $this->queryParameter('name')->string()->defaultsTo('unknown');
Expand Down
9 changes: 9 additions & 0 deletions examples/symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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&timestamps=2016-01-01%2000:00:00,2016-12-31%2023:59:59
Expand All @@ -26,6 +27,7 @@ class MyController

public function __construct(\Symfony\Component\HttpFoundation\Request $request)
{
$request->cookies->add(["user", "John Doe"]);
$this->initRequestParser($request);
}

Expand All @@ -36,6 +38,13 @@ public function hello()
return "Hello $name";
}

public function helloFromCookie()
{
$fullName = $this->cookieParameter('fullName')->string()->required();

return "Hello $fullName";
}

public function helloWithDefault()
{
$name = $this->queryParameter('name')->string()->defaultsTo('unknown');
Expand Down
19 changes: 19 additions & 0 deletions src/BaseControllerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -47,4 +53,17 @@ protected function bodyParameter($name)
{
return $this->bodyParser->get($name);
}

/**
* Use this method to access the request cookie parameters.
*
* $userId = $this->cookieParameter('userId')->int()->defaultsTo(null)
*
* @param $name
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a string typehint here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* @return TypeParser
*/
protected function cookieParameter($name)
{
return $this->cookieParser->get($name);
}
}
14 changes: 14 additions & 0 deletions src/Psr7/Psr7RequestParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/RequestParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public function createQueryParser();
* @return RequestParser
*/
public function createBodyParser();

/**
* @return RequestParser
*/
public function createCookieParser();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might break existing classes implementing the interface. What do you think about creating a second interface CookieParserFactory with just the createCookieParser? Then the specific *RequestParserFactory implementations can just implements RequestParserFactory, CookieParserFactory

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, true 👍 will change that

}
10 changes: 10 additions & 0 deletions src/Symfony/SymfonyRequestParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions tests/Integration/Psr7IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public function controllersProvider()

$getRequest = $request
->withMethod('GET')
->withCookieParams($parameters)
->withQueryParams($parameters);

$postRequest = $request
->withMethod('POST')
->withCookieParams($parameters)
->withParsedBody($parameters);

return [
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/SymfonyIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
28 changes: 20 additions & 8 deletions tests/Integration/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,23 +48,27 @@ public function __construct($request)
$this->request = $request;
}

protected function parameter($name)
protected function parameter($name, $cookie = false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split this into two functions (parameter and cookieParameter)? Otherwise the function is basically doing two things. Here's some interesting discussion on that topic in general: https://softwareengineering.stackexchange.com/questions/147977/is-it-wrong-to-use-a-boolean-parameter-to-determine-behavior

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually wondering about it, good point, this discussion solves my conflict :-) good point

{
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()
public function testString($fromCookie = false)
{
return $this->parameter('string')->string()->required();
return $this->parameter('string', $fromCookie)->string()->required();
}

public function testInt()
public function testInt($fromCookie = false)
{
return $this->parameter('int')->int()->required();
return $this->parameter('int', $fromCookie)->int()->required();
}

public function testNotFound()
Expand Down