Skip to content

Commit

Permalink
Merge pull request #24 from artursvonda/support-symfony-5.0-cookies
Browse files Browse the repository at this point in the history
Fix legacy cookie defaults deprecations
  • Loading branch information
stof authored Nov 30, 2018
2 parents 87ca639 + 81b8277 commit 7abcb42
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion http-kernel-fixtures/cookie_page1.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
$resp = new Symfony\Component\HttpFoundation\Response();
$cook = new Symfony\Component\HttpFoundation\Cookie('srvr_cookie', 'srv_var_is_set', 0, '/');
if (method_exists('Symfony\Component\HttpFoundation\Cookie', 'create')) {
$cook = Symfony\Component\HttpFoundation\Cookie::create('srvr_cookie', 'srv_var_is_set', 0, '/');
} else {
$cook = new Symfony\Component\HttpFoundation\Cookie('srvr_cookie', 'srv_var_is_set', 0, '/');
}
$resp->headers->setCookie($cook);
?>
<!doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
Expand Down
6 changes: 5 additions & 1 deletion http-kernel-fixtures/cookie_page3.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

$hasCookie = $request->cookies->has('foo');
$resp = new Symfony\Component\HttpFoundation\Response();
$cook = new Symfony\Component\HttpFoundation\Cookie('foo', 'bar');
if (method_exists('Symfony\Component\HttpFoundation\Cookie', 'create')) {
$cook = Symfony\Component\HttpFoundation\Cookie::create('foo', 'bar');
} else {
$cook = new Symfony\Component\HttpFoundation\Cookie('foo', 'bar');
}
$resp->headers->setCookie($cook);

?>
Expand Down
6 changes: 5 additions & 1 deletion http-kernel-fixtures/issue140.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
<?php
if ($request->isMethod('POST')) {
$resp = new Symfony\Component\HttpFoundation\Response();
$cook = new Symfony\Component\HttpFoundation\Cookie('tc', $request->request->get('cookie_value'));
if (method_exists('Symfony\Component\HttpFoundation\Cookie', 'create')) {
$cook = Symfony\Component\HttpFoundation\Cookie::create('tc', $request->request->get('cookie_value'));
} else {
$cook = new Symfony\Component\HttpFoundation\Cookie('tc', $request->request->get('cookie_value'));
}
$resp->headers->setCookie($cook);
} elseif ($request->query->has('show_value')) {
echo html_escape_value($request->cookies->get('tc'));
Expand Down
6 changes: 5 additions & 1 deletion http-kernel-fixtures/sub-folder/cookie_page1.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php
$requestUri = $request->server->get('REQUEST_URI');
$resp = new Symfony\Component\HttpFoundation\Response();
$cook = new Symfony\Component\HttpFoundation\Cookie('srvr_cookie', 'srv_var_is_set_sub_folder', 0, dirname($requestUri));
if (method_exists('Symfony\Component\HttpFoundation\Cookie', 'create')) {
$cook = Symfony\Component\HttpFoundation\Cookie::create('srvr_cookie', 'srv_var_is_set_sub_folder', 0, dirname($requestUri));
} else {
$cook = new Symfony\Component\HttpFoundation\Cookie('srvr_cookie', 'srv_var_is_set_sub_folder', 0, dirname($requestUri));
}
$resp->headers->setCookie($cook);
?>
<!doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
Expand Down
8 changes: 7 additions & 1 deletion src/FixturesKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ private function saveSession(Request $request, Response $response)

$params = session_get_cookie_params();

$response->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
if (method_exists('Symfony\Component\HttpFoundation\Cookie', 'create')) {
$cookie = Cookie::create($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']);
} else {
$cookie = new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}

$response->headers->setCookie($cookie);
}
}
}

0 comments on commit 7abcb42

Please sign in to comment.