History: lazy loading for the history file
Only read it when it's necessary
This commit is contained in:
parent
4306b184c4
commit
d16ca2e22f
2 changed files with 32 additions and 3 deletions
|
@ -70,6 +70,15 @@ public function __construct($historyFilePath, $retentionTime = null)
|
||||||
if ($retentionTime !== null) {
|
if ($retentionTime !== null) {
|
||||||
$this->retentionTime = $retentionTime;
|
$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->check();
|
||||||
$this->read();
|
$this->read();
|
||||||
}
|
}
|
||||||
|
@ -120,6 +129,10 @@ public function updateSettings()
|
||||||
*/
|
*/
|
||||||
protected function addEvent($status, $id = null)
|
protected function addEvent($status, $id = null)
|
||||||
{
|
{
|
||||||
|
if ($this->history === null) {
|
||||||
|
$this->initialize();
|
||||||
|
}
|
||||||
|
|
||||||
$item = [
|
$item = [
|
||||||
'event' => $status,
|
'event' => $status,
|
||||||
'datetime' => (new DateTime())->format(DateTime::ATOM),
|
'datetime' => (new DateTime())->format(DateTime::ATOM),
|
||||||
|
@ -178,6 +191,10 @@ protected function write()
|
||||||
*/
|
*/
|
||||||
public function getHistory()
|
public function getHistory()
|
||||||
{
|
{
|
||||||
|
if ($this->history === null) {
|
||||||
|
$this->initialize();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->history;
|
return $this->history;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,19 @@ public function tearDown()
|
||||||
/**
|
/**
|
||||||
* Test that the history file is created if it doesn't exist.
|
* Test that the history file is created if it doesn't exist.
|
||||||
*/
|
*/
|
||||||
public function testConstructFileCreated()
|
public function testConstructLazyLoading()
|
||||||
{
|
{
|
||||||
new History(self::$historyFilePath);
|
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);
|
$this->assertFileExists(self::$historyFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +47,8 @@ public function testConstructNotWritable()
|
||||||
{
|
{
|
||||||
touch(self::$historyFilePath);
|
touch(self::$historyFilePath);
|
||||||
chmod(self::$historyFilePath, 0440);
|
chmod(self::$historyFilePath, 0440);
|
||||||
new History(self::$historyFilePath);
|
$history = new History(self::$historyFilePath);
|
||||||
|
$history->updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,8 +60,9 @@ public function testConstructNotWritable()
|
||||||
public function testConstructNotParsable()
|
public function testConstructNotParsable()
|
||||||
{
|
{
|
||||||
file_put_contents(self::$historyFilePath, 'not parsable');
|
file_put_contents(self::$historyFilePath, 'not parsable');
|
||||||
|
$history = new History(self::$historyFilePath);
|
||||||
// gzinflate generates a warning
|
// gzinflate generates a warning
|
||||||
@new History(self::$historyFilePath);
|
@$history->updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue