| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Trait for VikBooking classes that may hold logs. |
| 16 |
* |
| 17 |
* @since 1.5.10 |
| 18 |
*/ |
| 19 |
trait VBOLoggerAware |
| 20 |
{ |
| 21 |
/** |
| 22 |
* A string used to hold the cron registered logs. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $logs = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* Sets the internal logs with the given value. |
| 30 |
* In case of an object or an array, the system will encode the |
| 31 |
* resulting value in JSON format. |
| 32 |
* |
| 33 |
* @param mixed $log |
| 34 |
* |
| 35 |
* @return self |
| 36 |
*/ |
| 37 |
final protected function setLog($log) |
| 38 |
{ |
| 39 |
$this->logs = trim($this->format($log)); |
| 40 |
|
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Appends the specified value to the existing logs. |
| 46 |
* |
| 47 |
* @param mixed $log The value to append. |
| 48 |
* @param string $separator The separator to use (new line by default). |
| 49 |
* |
| 50 |
* @return self |
| 51 |
*/ |
| 52 |
final protected function appendLog($log, $separator = "\n") |
| 53 |
{ |
| 54 |
$prev = $this->getLog(); |
| 55 |
|
| 56 |
$this->setLog($log); |
| 57 |
|
| 58 |
if ($prev) |
| 59 |
{ |
| 60 |
$this->setLog($prev . $separator . $this->getLog()); |
| 61 |
} |
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Prepends the specified value to the existing logs. |
| 68 |
* |
| 69 |
* @param mixed $log The value to prepend. |
| 70 |
* @param string $separator The separator to use (new line by default). |
| 71 |
* |
| 72 |
* @return self |
| 73 |
*/ |
| 74 |
final protected function prependLog($log, $separator = "\n") |
| 75 |
{ |
| 76 |
$prev = $this->getLog(); |
| 77 |
|
| 78 |
$this->setLog($log); |
| 79 |
|
| 80 |
if ($prev) |
| 81 |
{ |
| 82 |
$this->setLog($this->getLog() . $separator . $prev); |
| 83 |
} |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the internal logs. |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
final public function getLog() |
| 94 |
{ |
| 95 |
return $this->logs; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Helper method used to stringify non-scalar values. |
| 100 |
* |
| 101 |
* @param mixed $data The value to stringify. |
| 102 |
* |
| 103 |
* @return string The value as a string. |
| 104 |
*/ |
| 105 |
protected function format($data) |
| 106 |
{ |
| 107 |
if (is_array($data) || is_object($data)) |
| 108 |
{ |
| 109 |
// encode given object by using JSON pretty print mask |
| 110 |
$data = json_encode($data, defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); |
| 111 |
} |
| 112 |
|
| 113 |
return $data; |
| 114 |
} |
| 115 |
} |
| 116 |
|