Skip to content

Commit

Permalink
Add backwards compatibility mode for get()
Browse files Browse the repository at this point in the history
This is somewhat ugly code, but it'll be going away in 4.0.0.
  • Loading branch information
markstory committed Mar 27, 2017
1 parent a9e1d08 commit 9860a55
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
42 changes: 38 additions & 4 deletions src/Http/Cookie/CookieCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function add(CookieInterface $cookie)
/**
* Get the first cookie by name.
*
* If the provided name matches a URL (matches `#^https?://#`) this method
* If the provided name matches a URL (starts with `http:`) this method
* will assume you want a list of cookies that match that URL. This is
* backwards compatible behavior that will be removed in 4.0.0
*
Expand All @@ -92,6 +92,9 @@ public function add(CookieInterface $cookie)
*/
public function get($name)
{
if (substr($name, 0, 4) === 'http') {
return $this->getByUrl($name);
}
$key = mb_strtolower($name);
foreach ($this->cookies as $cookie) {
if (mb_strtolower($cookie->getName()) === $key) {
Expand All @@ -102,6 +105,22 @@ public function get($name)
return null;
}

/**
* Backwards compatibility helper for consumers of Client\CookieCollection
*
* @param string $url The url to get cookies for.
* @return array An array of matching cookies.
* @deprecated 3.5.0 Will be removed in 4.0.0. Use addToRequest() instead.
*/
protected function getByUrl($url)
{
$path = parse_url($url, PHP_URL_PATH) ?: '/';
$host = parse_url($url, PHP_URL_HOST);
$scheme = parse_url($url, PHP_URL_SCHEME);

return $this->findMatchingCookies($scheme, $host, $path);
}

/**
* Check if a cookie with the given name exists
*
Expand Down Expand Up @@ -190,7 +209,22 @@ public function addToRequest(RequestInterface $request)
$path = $uri->getPath();
$host = $uri->getHost();
$scheme = $uri->getScheme();
$cookies = $this->findMatchingCookies($scheme, $host, $path);
$cookies = array_merge($request->getCookieParams(), $cookies);

return $request->withCookieParams($cookies);
}

/**
* Find cookies matching the scheme, host, and path
*
* @param string $scheme The http scheme to match
* @param string $host The host to match.
* @param string $path The path to match
* @return array An array of cookie name/value pairs
*/
protected function findMatchingCookies($scheme, $host, $path)
{
$out = [];
foreach ($this->cookies as $cookie) {
if ($scheme === 'http' && $cookie->isSecure()) {
Expand All @@ -205,7 +239,8 @@ public function addToRequest(RequestInterface $request)
$domain = ltrim($domain, '.');
}

if ($cookie->getExpiry() && time() > $cookie->getExpiry()) {
$expires = $cookie->getExpiry();
if ($expires && time() > $expires) {
continue;
}

Expand All @@ -216,9 +251,8 @@ public function addToRequest(RequestInterface $request)

$out[$cookie->getName()] = $cookie->getValue();
}
$cookies = array_merge($request->getCookieParams(), $out);

return $request->withCookieParams($cookies);
return $out;
}

/**
Expand Down
40 changes: 28 additions & 12 deletions tests/TestCase/Http/Cookie/CookieCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,34 @@ public function testGetByName()
$this->assertSame($cookies[0], $collection->get('remember_me'));
}

/**
* Test that get() provides backwards compat behavior.
*
* When the parameter is a string that looks like a URL
*
* @return void
*/
public function testGetBackwardsCompatibility()
{
$cookies = [
new Cookie('test', 'value', null, '/api', 'example.com', true),
new Cookie('test_two', 'value2', null, '/blog', 'blog.example.com', true),
new Cookie('test3', 'value3', null, '/blog', 'blog.example.com', false),
];
$collection = new CookieCollection($cookies);
$result = $collection->get('http://example.com/api');
$this->assertSame([], $result);

$result = $collection->get('https://example.com/api');
$this->assertSame(['test' => 'value'], $result);

$result = $collection->get('http://foo.blog.example.com/blog/path');
$this->assertSame(['test3' => 'value3'], $result);

$result = $collection->get('https://foo.blog.example.com/blog/path');
$this->assertSame(['test_two' => 'value2', 'test3' => 'value3'], $result);
}

/**
* Test that the constructor takes only an array of objects implementing
* the CookieInterface
Expand Down Expand Up @@ -439,18 +467,6 @@ public function testStoreCompatibility()
$this->assertFalse($collection->has('expired'));
}

/**
* Test that get() provides backwards compat behavior.
*
* When the parameter is a string that looks like a URL
*
* @return void
*/
public function testGetBackwardsCompatibility()
{
$this->markTestIncomplete();
}

/**
* Test that getAll() provides backwards compat behavior.
*
Expand Down

0 comments on commit 9860a55

Please sign in to comment.