]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
History: lazy loading for the history file 764/head
authorArthurHoaro <arthur@hoa.ro>
Mon, 16 Jan 2017 11:50:36 +0000 (12:50 +0100)
committerArthurHoaro <arthur@hoa.ro>
Tue, 21 Mar 2017 19:29:20 +0000 (20:29 +0100)
Only read it when it's necessary

application/History.php
tests/HistoryTest.php

index c06067df80ca454bf0b8d3647e2f2d2b2cc3b35f..f93b03568235c1f71ecc7d9d86b21e0a56fab34b 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,6 +129,10 @@ class History
      */
     protected function addEvent($status, $id = null)
     {
+        if ($this->history === null) {
+            $this->initialize();
+        }
+
         $item = [
             'event' => $status,
             'datetime' => (new DateTime())->format(DateTime::ATOM),
@@ -178,6 +191,10 @@ class History
      */
     public function getHistory()
     {
+        if ($this->history === null) {
+            $this->initialize();
+        }
+
         return $this->history;
     }
 }
index 79322249d0da8ddf3b099dafe9f5c49388e73709..9152584509db8a8f7ac98107e15cdecefc17beb2 100644 (file)
@@ -21,9 +21,19 @@ class HistoryTest extends PHPUnit_Framework_TestCase
     /**
      * Test that the history file is created if it doesn't exist.
      */
-    public function testConstructFileCreated()
+    public function testConstructLazyLoading()
     {
         new History(self::$historyFilePath);
+        $this->assertFileNotExists(self::$historyFilePath);
+    }
+
+    /**
+     * Test that the history file is created if it doesn't exist.
+     */
+    public function testAddEventCreateFile()
+    {
+        $history = new History(self::$historyFilePath);
+        $history->updateSettings();
         $this->assertFileExists(self::$historyFilePath);
     }
 
@@ -37,7 +47,8 @@ class HistoryTest extends PHPUnit_Framework_TestCase
     {
         touch(self::$historyFilePath);
         chmod(self::$historyFilePath, 0440);
-        new History(self::$historyFilePath);
+        $history = new History(self::$historyFilePath);
+        $history->updateSettings();
     }
 
     /**
@@ -49,8 +60,9 @@ class HistoryTest extends PHPUnit_Framework_TestCase
     public function testConstructNotParsable()
     {
         file_put_contents(self::$historyFilePath, 'not parsable');
+        $history = new History(self::$historyFilePath);
         // gzinflate generates a warning
-        @new History(self::$historyFilePath);
+        @$history->updateSettings();
     }
 
     /**