if ($retentionTime !== null) {
$this->retentionTime = $retentionTime;
}
+ }
+
+ /**
+ * Initialize: read history file.
+ *
+ * Allow lazy loading (don't read the file if it isn't necessary).
+ */
+ protected function initialize()
+ {
$this->check();
$this->read();
}
*/
protected function addEvent($status, $id = null)
{
+ if ($this->history === null) {
+ $this->initialize();
+ }
+
$item = [
'event' => $status,
- 'datetime' => (new DateTime())->format(DateTime::ATOM),
+ 'datetime' => new DateTime(),
'id' => $id !== null ? $id : '',
];
$this->history = array_merge([$item], $this->history);
{
$comparaison = new DateTime('-'. $this->retentionTime . ' seconds');
foreach ($this->history as $key => $value) {
- if (DateTime::createFromFormat(DateTime::ATOM, $value['datetime']) < $comparaison) {
+ if ($value['datetime'] < $comparaison) {
unset($this->history[$key]);
}
}
*/
public function getHistory()
{
+ if ($this->history === null) {
+ $this->initialize();
+ }
+
return $this->history;
}
}