Skip to content

Commit 24751f6

Browse files
committed
refacto debug flag
1 parent c87ad5d commit 24751f6

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

examples/auto-prepend/scripts/bounce-via-auto-prepend.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
$bounce = new StandAloneBounce();
99
$bounce->init($crowdSecStandaloneBouncerConfig);
10+
$bounce->setDebug($crowdSecStandaloneBouncerConfig['debug_mode']);
1011
$bounce->safelyBounce();

src/AbstractBounce.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ abstract class AbstractBounce
2626
/** @var array */
2727
protected $settings = [];
2828

29+
/** @var bool */
30+
protected $debug = false;
31+
2932
/** @var LoggerInterface */
3033
protected $logger;
3134

@@ -47,7 +50,12 @@ public function run(
4750
}
4851
}
4952

50-
protected function initLoggerHelper($logDirectoryPath, $loggerName, $debugMode): void
53+
public function setDebug(bool $debug)
54+
{
55+
$this->debug = $debug;
56+
}
57+
58+
protected function initLoggerHelper($logDirectoryPath, $loggerName): void
5159
{
5260
// Singleton for this function
5361
if ($this->logger) {
@@ -60,8 +68,8 @@ protected function initLoggerHelper($logDirectoryPath, $loggerName, $debugMode):
6068
$fileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%context%\n"));
6169
$this->logger->pushHandler($fileHandler);
6270

63-
// Set custom readable logger when debug_mode=true
64-
if ($debugMode) {
71+
// Set custom readable logger when debug=true
72+
if ($this->debug) {
6573
$debugLogPath = $logDirectoryPath.'/debug.log';
6674
$debugFileHandler = new RotatingFileHandler($debugLogPath, 0, Logger::DEBUG);
6775
if (class_exists('\Bramus\Monolog\Formatter\ColoredLineFormatter')) {
@@ -104,7 +112,7 @@ protected function bounceCurrentIp()
104112
'file' => $e->getFile(),
105113
'line' => $e->getLine(),
106114
]);
107-
if ($this->getSettings('debug_mode')) {
115+
if ($this->debug) {
108116
throw $e;
109117
}
110118
}

src/StandAloneBounce.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function getBouncerInstance(): Bouncer
149149

150150
public function initLogger(): void
151151
{
152-
$this->initLoggerHelper($this->getSettings('log_directory_path'), 'php_standalone_bouncer', $this->getSettings('debug_mode'));
152+
$this->initLoggerHelper($this->getSettings('log_directory_path'), 'php_standalone_bouncer');
153153
}
154154

155155
/**
@@ -371,7 +371,7 @@ public function safelyBounce(): void
371371
'file' => $e->getFile(),
372372
'line' => $e->getLine(),
373373
]);
374-
if ($this->getSettings('debug_mode')) {
374+
if ($this->debug) {
375375
throw $e;
376376
}
377377
}

0 commit comments

Comments
 (0)