Skip to content

Commit

Permalink
only write lock when its content has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
stlrnz committed Mar 8, 2019
1 parent 7f04fb5 commit ffe345f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Lock
{
private $json;
private $lock = [];
private $changed = false;

public function __construct($lockFile)
{
Expand All @@ -38,6 +39,7 @@ public function add($name, $data)
{
$current = $this->lock[$name] ?? [];
$this->lock[$name] = array_merge($current, $data);
$this->changed = true;
}

public function get($name)
Expand All @@ -48,15 +50,21 @@ public function get($name)
public function set($name, $data)
{
$this->lock[$name] = $data;
$this->changed = true;
}

public function remove($name)
{
unset($this->lock[$name]);
$this->changed = true;
}

public function write()
{
if (!$this->changed) {
return;
}

if ($this->lock) {
ksort($this->lock);
$this->json->write($this->lock);
Expand Down

0 comments on commit ffe345f

Please sign in to comment.