aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-01-16 12:50:36 +0100
committerArthurHoaro <arthur@hoa.ro>2017-03-21 20:29:20 +0100
commitd16ca2e22f3d7325fc9593fccd8523eebe226567 (patch)
treea1e5fb646ec69240fd882269a2c85a452e081833
parent4306b184c4471825f916d895b047ed03fdf58985 (diff)
downloadShaarli-d16ca2e22f3d7325fc9593fccd8523eebe226567.tar.gz
Shaarli-d16ca2e22f3d7325fc9593fccd8523eebe226567.tar.zst
Shaarli-d16ca2e22f3d7325fc9593fccd8523eebe226567.zip
History: lazy loading for the history file
Only read it when it's necessary
-rw-r--r--application/History.php17
-rw-r--r--tests/HistoryTest.php18
2 files changed, 32 insertions, 3 deletions
diff --git a/application/History.php b/application/History.php
index c06067df..f93b0356 100644
--- a/application/History.php
+++ b/application/History.php
@@ -70,6 +70,15 @@ class History
70 if ($retentionTime !== null) { 70 if ($retentionTime !== null) {
71 $this->retentionTime = $retentionTime; 71 $this->retentionTime = $retentionTime;
72 } 72 }
73 }
74
75 /**
76 * Initialize: read history file.
77 *
78 * Allow lazy loading (don't read the file if it isn't necessary).
79 */
80 protected function initialize()
81 {
73 $this->check(); 82 $this->check();
74 $this->read(); 83 $this->read();
75 } 84 }
@@ -120,6 +129,10 @@ class History
120 */ 129 */
121 protected function addEvent($status, $id = null) 130 protected function addEvent($status, $id = null)
122 { 131 {
132 if ($this->history === null) {
133 $this->initialize();
134 }
135
123 $item = [ 136 $item = [
124 'event' => $status, 137 'event' => $status,
125 'datetime' => (new DateTime())->format(DateTime::ATOM), 138 'datetime' => (new DateTime())->format(DateTime::ATOM),
@@ -178,6 +191,10 @@ class History
178 */ 191 */
179 public function getHistory() 192 public function getHistory()
180 { 193 {
194 if ($this->history === null) {
195 $this->initialize();
196 }
197
181 return $this->history; 198 return $this->history;
182 } 199 }
183} 200}
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php
index 79322249..91525845 100644
--- a/tests/HistoryTest.php
+++ b/tests/HistoryTest.php
@@ -21,9 +21,19 @@ class HistoryTest extends PHPUnit_Framework_TestCase
21 /** 21 /**
22 * Test that the history file is created if it doesn't exist. 22 * Test that the history file is created if it doesn't exist.
23 */ 23 */
24 public function testConstructFileCreated() 24 public function testConstructLazyLoading()
25 { 25 {
26 new History(self::$historyFilePath); 26 new History(self::$historyFilePath);
27 $this->assertFileNotExists(self::$historyFilePath);
28 }
29
30 /**
31 * Test that the history file is created if it doesn't exist.
32 */
33 public function testAddEventCreateFile()
34 {
35 $history = new History(self::$historyFilePath);
36 $history->updateSettings();
27 $this->assertFileExists(self::$historyFilePath); 37 $this->assertFileExists(self::$historyFilePath);
28 } 38 }
29 39
@@ -37,7 +47,8 @@ class HistoryTest extends PHPUnit_Framework_TestCase
37 { 47 {
38 touch(self::$historyFilePath); 48 touch(self::$historyFilePath);
39 chmod(self::$historyFilePath, 0440); 49 chmod(self::$historyFilePath, 0440);
40 new History(self::$historyFilePath); 50 $history = new History(self::$historyFilePath);
51 $history->updateSettings();
41 } 52 }
42 53
43 /** 54 /**
@@ -49,8 +60,9 @@ class HistoryTest extends PHPUnit_Framework_TestCase
49 public function testConstructNotParsable() 60 public function testConstructNotParsable()
50 { 61 {
51 file_put_contents(self::$historyFilePath, 'not parsable'); 62 file_put_contents(self::$historyFilePath, 'not parsable');
63 $history = new History(self::$historyFilePath);
52 // gzinflate generates a warning 64 // gzinflate generates a warning
53 @new History(self::$historyFilePath); 65 @$history->updateSettings();
54 } 66 }
55 67
56 /** 68 /**