Skip to content

Commit b3c6905

Browse files
authored
Symfony 4.2 and 5.0 support
Creating a `new Cookie()` in SF4.2 triggers a deprecation notice, causing test suites to fail. The deprecation notice relates to the default value of two of the cookie constructor arguments. The notice suggests using the `Cookie::create()` method, but this is not available in Symfony versions older than 4.2. The alternative is to explicitly set values for all constructor arguments. This allows compatibility with older versions of Symfony while also supporting the newer versions in future. ``` The default value of the "$secure" and "$samesite" arguments of "Symfony\Component\HttpFoundation\Cookie::__construct"'s constructor will respectively change from "false" to "null" and from "null" to "lax" in Symfony 5.0, you should define their values explicitly or use "Cookie::create()" instead. Stack trace: #0 vendor/sentry/sentry/lib/Raven/ErrorHandler.php(127): Raven_Breadcrumbs_ErrorHandler->handleError(16384, 'The default val...', '/var/www/vendor...', 91, Array) #1 [internal function]: Raven_ErrorHandler->handleError(16384, 'The default val...', '/var/www/vendor...', 91, Array) #2 vendor/symfony/http-foundation/Cookie.php(91): trigger_error('The default val...', 16384) #3 vendor/suncat/mobile-detect-bundle/SunCat/MobileDetectBundle/Helper/DeviceView.php(476): Symfony\Component\HttpFoundation\Cookie->__construct('device_view', 'full', Object(DateTime), '/', '', false, true) #4 vendor/suncat/mobile-detect-bundle/SunCat/MobileDetectBundle/Helper/DeviceView.php(299): SunCat\MobileDetectBundle\Helper\DeviceView->createCookie('full') #5 vendor/suncat/mobile-detect-bundle/SunCat/MobileDetectBundle/EventListener/RequestResponseListener.php(219): SunCat\MobileDetectBundle\Helper\DeviceView->modifyResponse('full', Object(Symfony\Component\HttpFoundation\Response)) #6 vendor/suncat/mobile-detect-bundle/SunCat/MobileDetectBundle/EventListener/RequestResponseListener.php(177): SunCat\MobileDetectBundle\EventListener\RequestResponseListener->SunCat\MobileDetectBundle\EventListener\{closure}(Object(SunCat\MobileDetectBundle\Helper\DeviceView), Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent)) #7 vendor/symfony/event-dispatcher/EventDispatcher.php(212): SunCat\MobileDetectBundle\EventListener\RequestResponseListener->handleResponse(Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent), 'kernel.response', Object(Symfony\Component\EventDispatcher\EventDispatcher)) #8 vendor/symfony/event-dispatcher/EventDispatcher.php(44): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'kernel.response', Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent)) #9 vendor/symfony/http-kernel/HttpKernel.php(189): Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.response', Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent)) #10 vendor/symfony/http-kernel/HttpKernel.php(171): Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object(Symfony\Component\HttpFoundation\Response), Object(Symfony\Component\HttpFoundation\Request), 1) #11 vendor/symfony/http-kernel/HttpKernel.php(67): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #12 vendor/symfony/http-kernel/Kernel.php(198): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #13 vendor/symfony/http-kernel/Client.php(68): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #14 vendor/symfony/framework-bundle/Client.php(131): Symfony\Component\HttpKernel\Client->doRequest(Object(Symfony\Component\HttpFoundation\Request)) #15 vendor/symfony/browser-kit/Client.php(405): Symfony\Bundle\FrameworkBundle\Client->doRequest(Object(Symfony\Component\HttpFoundation\Request)) #16 tests/functional/Bundle/QuizBundle/Controller/PublicControllerTest.php(13): Symfony\Component\BrowserKit\Client->request('GET', 'http://localhos...') #17 [internal function]: Weirdly\Bundle\QuizBundle\Controller\PublicControllerTest->testPublicQuiz() #18 {main} Exited with code 1 ``` This pull request introduces those arguments and sets them explicitly, maintaining current behaviour with no backwards-breaking changes.
1 parent 06007fe commit b3c6905

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

Helper/DeviceView.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class DeviceView
3333
const COOKIE_DOMAIN_DEFAULT = '';
3434
const COOKIE_SECURE_DEFAULT = false;
3535
const COOKIE_HTTP_ONLY_DEFAULT = true;
36+
const COOKIE_RAW_DEFAULT = false;
37+
const COOKIE_SAMESITE_DEFAULT = null;
3638
const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT = '1 month';
3739
const SWITCH_PARAM_DEFAULT = 'device_view';
3840

@@ -75,6 +77,16 @@ class DeviceView
7577
* @var bool
7678
*/
7779
protected $cookieHttpOnly = self::COOKIE_HTTP_ONLY_DEFAULT;
80+
81+
/**
82+
* @var bool
83+
*/
84+
protected $cookieRaw = self::COOKIE_RAW_DEFAULT;
85+
86+
/**
87+
* @var string|null
88+
*/
89+
protected $cookieSameSite = self::COOKIE_SAMESITE_DEFAULT;
7890

7991
/**
8092
* @var string
@@ -415,6 +427,46 @@ public function setCookieHttpOnly($cookieHttpOnly)
415427
$this->cookieHttpOnly = $cookieHttpOnly;
416428
}
417429

430+
/**
431+
* Is the cookie raw.
432+
*
433+
* @return bool
434+
*/
435+
public function isCookieRaw()
436+
{
437+
return $this->cookieRaw;
438+
}
439+
440+
/**
441+
* Setter of CookieRaw.
442+
*
443+
* @param bool $cookieRaw
444+
*/
445+
public function setCookieRaw($cookieRaw)
446+
{
447+
$this->cookieRaw = $cookieRaw;
448+
}
449+
450+
/**
451+
* Getter of CookieSamesite.
452+
*
453+
* @return string|null
454+
*/
455+
public function getCookieSamesite()
456+
{
457+
return $this->cookieSamesite;
458+
}
459+
460+
/**
461+
* Setter of CookieSamesite.
462+
*
463+
* @param string|null $cookieSamesite
464+
*/
465+
public function setCookieSamesite($cookieSamesite)
466+
{
467+
$this->cookieSamesite = $cookieSamesite;
468+
}
469+
418470
/**
419471
* Setter of SwitchParam.
420472
*
@@ -473,7 +525,9 @@ protected function createCookie($value)
473525
$this->getCookiePath(),
474526
$this->getCookieDomain(),
475527
$this->isCookieSecure(),
476-
$this->isCookieHttpOnly()
528+
$this->isCookieHttpOnly(),
529+
$this->isCookieRaw(),
530+
$this->getCookieSamesite()
477531
);
478532
}
479533

0 commit comments

Comments
 (0)