Skip to content

Commit 5b72720

Browse files
committed
Add Plugin (WIP)
1 parent 1cda8e8 commit 5b72720

File tree

7 files changed

+485
-14
lines changed

7 files changed

+485
-14
lines changed

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
"php": ">=7.4",
1919
"nette/utils": "^3.0"
2020
},
21+
"require-dev": {
22+
"firebase/php-jwt": "^5.0",
23+
"nette/tester": "^2.4",
24+
"phpstan/phpstan": "^0.12.98"
25+
},
26+
"suggest": {
27+
"firebase/php-jwt": "Optional, required for SignedUrl plugin"
28+
},
2129
"autoload": {
2230
"psr-4": {
2331
"Redbitcz\\DebugMode\\": "src/"
@@ -36,10 +44,6 @@
3644
"dev-master": "2.0-dev"
3745
}
3846
},
39-
"require-dev": {
40-
"phpstan/phpstan": "^0.12.98",
41-
"nette/tester": "^2.4"
42-
},
4347
"scripts": {
4448
"phpstan": "phpstan analyze src -c phpstan.neon --level 8",
4549
"tester": "tester tests"

src/Detector.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Redbitcz\DebugMode;
1010

11+
use Redbitcz\DebugMode\Plugin\Plugin;
12+
1113
class Detector
1214
{
1315
/** Name of Environment variable used to detect Debug mode */
@@ -36,10 +38,12 @@ class Detector
3638

3739

3840
private ?Enabler $enabler;
39-
private int $mode;
4041
/** @var string[] */
4142
private array $ips = ['::1', '127.0.0.1'];
4243

44+
/** @var array<int, callable> */
45+
private array $detections;
46+
4347
/**
4448
* @param int $mode Enables methods which is used to detect Debug mode
4549
* @param Enabler|null $enabler Enabler instance. Optional, but required when Enabler mode is enabled
@@ -52,8 +56,21 @@ public function __construct(int $mode = self::MODE_SIMPLE, ?Enabler $enabler = n
5256
);
5357
}
5458

59+
$this->detections = array_filter(
60+
[
61+
$mode & self::MODE_ENABLER ? [$this, 'isDebugModeByEnabler'] : null,
62+
$mode & self::MODE_COOKIE ? [$this, 'isDebugModeByCookie'] : null,
63+
$mode & self::MODE_ENV ? [$this, 'isDebugModeByEnv'] : null,
64+
$mode & self::MODE_IP ? [$this, 'isDebugModeByIp'] : null,
65+
]
66+
);
67+
5568
$this->enabler = $enabler;
56-
$this->mode = $mode;
69+
}
70+
71+
public function hasEnabler(): bool
72+
{
73+
return $this->enabler !== null;
5774
}
5875

5976
public function getEnabler(): Enabler
@@ -74,11 +91,12 @@ public function getEnabler(): Enabler
7491
*/
7592
public function isDebugMode(?bool $default = false): ?bool
7693
{
77-
return ($this->mode & self::MODE_ENABLER ? $this->isDebugModeByEnabler() : null)
78-
?? ($this->mode & self::MODE_COOKIE ? $this->isDebugModeByCookie() : null)
79-
?? ($this->mode & self::MODE_ENV ? $this->isDebugModeByEnv() : null)
80-
?? ($this->mode & self::MODE_IP ? $this->isDebugModeByIp() : null)
81-
?? $default;
94+
foreach ($this->detections as $detection) {
95+
if (($result = $detection()) !== null) {
96+
return $result;
97+
}
98+
}
99+
return $default;
82100
}
83101

84102
/**
@@ -152,6 +170,7 @@ public function isDebugModeByIp(): ?bool
152170

153171
/**
154172
* Set client IP address with allowed Debug mode
173+
* @return static
155174
*/
156175
public function setAllowedIp(string ...$ips): self
157176
{
@@ -161,13 +180,28 @@ public function setAllowedIp(string ...$ips): self
161180

162181
/**
163182
* Add client IP address with allowed Debug mode
183+
* @return static
164184
*/
165185
public function addAllowedIp(string ...$ips): self
166186
{
167187
$this->ips = array_merge($this->ips, $ips);
168188
return $this;
169189
}
170190

191+
/** @return static */
192+
public function prependPlugin(Plugin $plugin): self
193+
{
194+
array_unshift($this->detections, $plugin);
195+
return $this;
196+
}
197+
198+
/** @return static */
199+
public function appendPlugin(Plugin $plugin): self
200+
{
201+
$this->detections[] = $plugin;
202+
return $this;
203+
}
204+
171205
/**
172206
* Shortcut to simple detect Debug mode by all method enabled by Detector mode (argument `$mode`)
173207
*

src/Enabler.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Redbitcz\DebugMode;
1010

11+
use DateTimeInterface;
1112
use Nette\IOException;
1213
use Nette\Utils\DateTime as NetteDateTime;
1314
use Nette\Utils\FileSystem;
@@ -68,14 +69,17 @@ public function setCookieOptions(
6869
return $this;
6970
}
7071

71-
public function activate(bool $isDebug, ?string $time = self::DEFAULT_TTL): void
72+
/**
73+
* @param string|int|DateTimeInterface|null $expires
74+
*/
75+
public function activate(bool $isDebug, $expires = null): void
7276
{
7377
if ($tokenName = $this->getTokenName()) {
7478
$this->destroyToken($tokenName);
7579
}
7680

77-
$tokenExpires = (int)NetteDateTime::from($time ?? self::DEFAULT_TTL)->format('U');
78-
$cookieExpires = $time === null ? 0 : $tokenExpires;
81+
$tokenExpires = (int)NetteDateTime::from($expires ?? self::DEFAULT_TTL)->format('U');
82+
$cookieExpires = $expires === null ? 0 : $tokenExpires;
7983
$tokenName = $this->createToken($isDebug, $tokenExpires);
8084
setcookie(self::DEBUG_COOKIE_NAME, $tokenName, ['expires' => $cookieExpires] + $this->cookieOptions);
8185

src/Plugin/Plugin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Redbitcz\DebugMode\Plugin;
6+
7+
use Redbitcz\DebugMode\Detector;
8+
9+
interface Plugin
10+
{
11+
/**
12+
* Method invoked when Debug mode detection is called
13+
* Returned value:
14+
* - `true` (force to turn-on debug mode)
15+
* - `false` (force to turn-off debug mode)
16+
* - `null` (unknown debug mode state, next detection method will be called)
17+
*/
18+
public function __invoke(Detector $detector): ?bool;
19+
}

0 commit comments

Comments
 (0)