2021-10-18 18:24:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
// simple write all into an array that we can poll in the return group
|
2021-10-20 09:36:18 +09:00
|
|
|
// to activate AmazonDebug::setDebug(true) must be called once
|
2021-10-18 18:24:38 +09:00
|
|
|
|
2021-10-21 09:55:32 +09:00
|
|
|
namespace gullevek\AmazonIncentives\Debug;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
|
|
|
|
class AmazonDebug
|
|
|
|
|
{
|
|
|
|
|
private static $log = [];
|
|
|
|
|
private static $debug = false;
|
|
|
|
|
private static $id = null;
|
|
|
|
|
|
2021-10-20 09:36:18 +09:00
|
|
|
/**
|
|
|
|
|
* set the ID for current run
|
|
|
|
|
* if debug is off, nothing will be set and id is null
|
|
|
|
|
* This is run on setFlag, if debug is true
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id If not set, will default to uniqid() call
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function setId(?string $id = null): void
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
if (self::$debug === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($id === null) {
|
|
|
|
|
$id = uniqid();
|
|
|
|
|
}
|
|
|
|
|
self::$id = $id;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 09:36:18 +09:00
|
|
|
/**
|
|
|
|
|
* set the debug flag.
|
2021-10-21 09:55:32 +09:00
|
|
|
* This is automatically run in gullevek\AmazonIncentives\AmazonIncentives::__construct
|
2021-10-20 09:36:18 +09:00
|
|
|
* No need to run manuall
|
|
|
|
|
*
|
|
|
|
|
* @param boolean $debug Can only be True or False
|
|
|
|
|
* @param string|null $id If not set, will default to uniqid() call
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function setDebug(bool $debug, ?string $id = null): void
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
2021-10-20 09:36:18 +09:00
|
|
|
self::$debug = $debug;
|
|
|
|
|
if (self::$debug === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self::setId($id);
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 09:36:18 +09:00
|
|
|
/**
|
|
|
|
|
* returns current debug flag status
|
|
|
|
|
*
|
|
|
|
|
* @return boolean True if debug is on, False if debug is off
|
|
|
|
|
*/
|
|
|
|
|
public static function getDebug(): bool
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
2021-10-20 09:36:18 +09:00
|
|
|
return self::$debug;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get the current set ID, can return null if debug is off
|
|
|
|
|
*
|
|
|
|
|
* @return string|null Current set ID for this log run
|
|
|
|
|
*/
|
|
|
|
|
public static function getId(): ?string
|
|
|
|
|
{
|
|
|
|
|
return self::$id;
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 09:36:18 +09:00
|
|
|
/**
|
|
|
|
|
* write a log entry
|
|
|
|
|
* Data is as array key -> value
|
|
|
|
|
* Will be pushed as new array entry int log
|
|
|
|
|
* Main key is the set Id for this run
|
|
|
|
|
*
|
|
|
|
|
* @param array $data Any array data to store in the log
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-10-18 18:24:38 +09:00
|
|
|
public static function writeLog(array $data): void
|
|
|
|
|
{
|
|
|
|
|
if (self::$debug === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-20 09:36:18 +09:00
|
|
|
self::$log[self::getId()][] = $data;
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 09:36:18 +09:00
|
|
|
/**
|
|
|
|
|
* get all logs written since first class run
|
|
|
|
|
* or get all log entries for given ID
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id If set returns only this id logs
|
|
|
|
|
* or empty array if not found
|
|
|
|
|
* @return array Always array, empty if not data or not found
|
|
|
|
|
*/
|
2021-10-18 18:24:38 +09:00
|
|
|
public static function getLog(?string $id = null): array
|
|
|
|
|
{
|
|
|
|
|
if ($id === null) {
|
|
|
|
|
return self::$log;
|
|
|
|
|
} else {
|
|
|
|
|
return self::$log[$id] ?? [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// __END__
|