]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/History.php
Add history entries for API endpoint
[github/shaarli/Shaarli.git] / application / History.php
index c06067df80ca454bf0b8d3647e2f2d2b2cc3b35f..116b9264019c2a667e19f2d1e47aee0d5ab0b362 100644 (file)
@@ -70,6 +70,15 @@ class History
         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();
     }
@@ -120,9 +129,13 @@ class History
      */
     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);
@@ -164,7 +177,7 @@ class 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]);
             }
         }
@@ -178,6 +191,10 @@ class History
      */
     public function getHistory()
     {
+        if ($this->history === null) {
+            $this->initialize();
+        }
+
         return $this->history;
     }
 }