| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engine; |
| 4 |
|
| 5 |
class Logger |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Flag to determine whether a debug log should be generated by the calculation engine |
| 9 |
* If true, then a debug log will be generated |
| 10 |
* If false, then a debug log will not be generated. |
| 11 |
*/ |
| 12 |
private bool $writeDebugLog = false; |
| 13 |
|
| 14 |
/** |
| 15 |
* Flag to determine whether a debug log should be echoed by the calculation engine |
| 16 |
* If true, then a debug log will be echoed |
| 17 |
* If false, then a debug log will not be echoed |
| 18 |
* A debug log can only be echoed if it is generated. |
| 19 |
*/ |
| 20 |
private bool $echoDebugLog = false; |
| 21 |
|
| 22 |
/** |
| 23 |
* The debug log generated by the calculation engine. |
| 24 |
* |
| 25 |
* @var string[] |
| 26 |
*/ |
| 27 |
private array $debugLog = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* The calculation engine cell reference stack. |
| 31 |
*/ |
| 32 |
private CyclicReferenceStack $cellStack; |
| 33 |
|
| 34 |
/** |
| 35 |
* Instantiate a Calculation engine logger. |
| 36 |
*/ |
| 37 |
public function __construct(CyclicReferenceStack $stack) |
| 38 |
{ |
| 39 |
$this->cellStack = $stack; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enable/Disable Calculation engine logging. |
| 44 |
*/ |
| 45 |
public function setWriteDebugLog(bool $writeDebugLog): void |
| 46 |
{ |
| 47 |
$this->writeDebugLog = $writeDebugLog; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Return whether calculation engine logging is enabled or disabled. |
| 52 |
*/ |
| 53 |
public function getWriteDebugLog(): bool |
| 54 |
{ |
| 55 |
return $this->writeDebugLog; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Enable/Disable echoing of debug log information. |
| 60 |
*/ |
| 61 |
public function setEchoDebugLog(bool $echoDebugLog): void |
| 62 |
{ |
| 63 |
$this->echoDebugLog = $echoDebugLog; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Return whether echoing of debug log information is enabled or disabled. |
| 68 |
*/ |
| 69 |
public function getEchoDebugLog(): bool |
| 70 |
{ |
| 71 |
return $this->echoDebugLog; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Write an entry to the calculation engine debug log. |
| 76 |
* @param mixed ...$args |
| 77 |
*/ |
| 78 |
public function writeDebugLog(string $message, ...$args): void |
| 79 |
{ |
| 80 |
// Only write the debug log if logging is enabled |
| 81 |
if ($this->writeDebugLog) { |
| 82 |
$message = sprintf($message, ...$args); //* @phpstan-ignore argument.type (...$args could contain items that sprintf can't handle) |
| 83 |
$cellReference = implode(' -> ', $this->cellStack->showStack()); //* @phpstan-ignore argument.type (showStack can return array<mixed> not array<string>) |
| 84 |
if ($this->echoDebugLog) { |
| 85 |
echo $cellReference, |
| 86 |
($this->cellStack->count() > 0 ? ' => ' : ''), |
| 87 |
$message, |
| 88 |
PHP_EOL; |
| 89 |
} |
| 90 |
$this->debugLog[] = $cellReference |
| 91 |
. ($this->cellStack->count() > 0 ? ' => ' : '') |
| 92 |
. $message; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Write a series of entries to the calculation engine debug log. |
| 98 |
* |
| 99 |
* @param string[] $args |
| 100 |
*/ |
| 101 |
public function mergeDebugLog(array $args): void |
| 102 |
{ |
| 103 |
if ($this->writeDebugLog) { |
| 104 |
foreach ($args as $entry) { |
| 105 |
$this->writeDebugLog($entry); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Clear the calculation engine debug log. |
| 112 |
*/ |
| 113 |
public function clearLog(): void |
| 114 |
{ |
| 115 |
$this->debugLog = []; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return the calculation engine debug log. |
| 120 |
* |
| 121 |
* @return string[] |
| 122 |
*/ |
| 123 |
public function getLog(): array |
| 124 |
{ |
| 125 |
return $this->debugLog; |
| 126 |
} |
| 127 |
} |
| 128 |
|