Skip to content

Commit b654f07

Browse files
committed
Detector: Add isDebugModeByCookie()
1 parent 96064bc commit b654f07

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/Detector.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
class Detector
1212
{
1313
private const DEBUG_ENV_NAME = 'APP_DEBUG';
14+
private const DEBUG_COOKIE_NAME = 'app-debug-mode';
15+
1416

1517
/** @var Enabler */
1618
private $enabler;
@@ -28,6 +30,7 @@ public function getEnabler(): Enabler
2830
public function isDebugMode(?bool $default = false): ?bool
2931
{
3032
return $this->isDebugModeByEnabler()
33+
?? $this->isDebugModeByCookie()
3134
?? $this->isDebugModeByEnv()
3235
?? $this->isDebugModeByIp()
3336
?? $default;
@@ -36,16 +39,39 @@ public function isDebugMode(?bool $default = false): ?bool
3639
/**
3740
* Detect debug state by DobugModeEnabler helper
3841
* Returned value:
39-
* - false (force to turn-off debug mode)
40-
* - true (force to turn-on debug mode)
41-
* - null (enabler is not activated)
42+
* - `false` (force to turn-off debug mode)
43+
* - `true` (force to turn-on debug mode)
44+
* - `null` (enabler is not activated)
45+
*
4246
* @return bool|null
4347
*/
4448
public function isDebugModeByEnabler(): ?bool
4549
{
4650
return $this->enabler->isDebug();
4751
}
4852

53+
/**
54+
* Detect disabling debug mode by Cookie: `app-debug-mode: 0`
55+
*
56+
* ENV value vs. returned value:
57+
* - `0`: `false` (force to turn-off debug mode)
58+
* - `undefined` or any other value (includes `1`): `null`
59+
*
60+
* Note: This cookie allows only turn-off Debug mode.
61+
* Using cookie to turn-on debug mode is unsecure!
62+
*
63+
* @return bool|null
64+
*/
65+
public function isDebugModeByCookie(): ?bool
66+
{
67+
$cookieValue = $_COOKIE[self::DEBUG_COOKIE_NAME] ?? null;
68+
if (is_numeric($cookieValue) && (int)$cookieValue === 0) {
69+
return false;
70+
}
71+
72+
return null;
73+
}
74+
4975
/**
5076
* Detect debug state by ENV parameter
5177
* ENV value vs. returned value:

0 commit comments

Comments
 (0)