Skip to content

Commit f283dff

Browse files
committed
Merge pull request #8 from mathroc/refactoring
refactoring
2 parents c44fe5a + 60410e2 commit f283dff

File tree

6 files changed

+50
-43
lines changed

6 files changed

+50
-43
lines changed

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ $lock->release();
4444

4545
use TH\Lock\FileLock;
4646

47-
$lock = new FileLock('/path/to/file');
47+
$lock = new FileLock('/path/to/file', FileLock::SHARED);
4848

49-
$lock->acquire(FileLock::SHARED);
49+
$lock->acquire();
5050

5151
// other processes that try to acquire an exclusive lock on the file will fail,
5252
// processes that try to acquire an shared lock on the file will succeed
@@ -58,10 +58,6 @@ $lock->acquire();
5858
// other processes can now acquire an exclusive lock on the file if no other shared lock remains.
5959
```
6060

61-
### Upgrading or downgrading a lock
62-
63-
`$lock->acquire()` can be called multiple times to either upgrade a shared lock to an exclusive lock or downgrade an exclusive lock to a shared one. You can use it when you're done editing the file.
64-
6561
### Auto release
6662

6763
`$lock->release()` is called automatically when the lock is destroyed so you don't need to manually release it at the end of a script or if it got out of scope.
@@ -121,13 +117,13 @@ $lock = $factory->create('resource identifier');
121117

122118
There are two methods you can use on a `FileLock`:
123119

124-
* `\TH\Lock\FileLock::acquire($exclusive = FileLock::EXCLUSIVE, $blocking = FileLock::NON_BLOCKING)` used to acquire a lock on the file
120+
* `\TH\Lock\FileLock::acquire()` used to acquire a lock on the file
125121
* `\TH\Lock\FileLock::release()` used to release a lock on the file
126122

127-
And one on a `FileFactory':
123+
And one on a `FileFactory`:
128124

129-
* `\TH\Lock\FileFactory::create($resource, $owner = null)` used to create a `FileLock` for $resource
125+
* `\TH\Lock\FileFactory::create($resource, $owner = null, $exclusive = FileLock::EXCLUSIVE, $blocking = FileLock::NON_BLOCKING)` used to create a `FileLock` for `$resource`
130126

131127
## Notes
132128

133-
Only lock file are currently supported. It means distributed processes can't be lock this way. It should be easy to implements the `TH\Lock\Lock` and `TH\Lock\LockFactory` interface to use a distributed lock mechanism (maybe with [Redis](http://redis.io/topics/distlock)).
129+
Only lock file are currently supported. It means distributed processes can't be lock this way. It should be easy to implements the `TH\Lock\Lock` interface to use a distributed lock mechanism (maybe with [Redis](http://redis.io/topics/distlock)).

spec/TH/Lock/FileFactorySpec.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function let()
2222
public function it_is_initializable()
2323
{
2424
$this->shouldHaveType('TH\Lock\FileFactory');
25-
$this->shouldImplement('TH\Lock\LockFactory');
2625
}
2726

2827
public function it_should_create_a_file_lock()

spec/TH/Lock/FileLockSpec.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ public function it_should_acquire_an_exclusive_lock()
4242
}
4343
}
4444

45-
public function it_should_acquire_an_shared_lock()
45+
public function it_should_acquire_a_shared_lock()
4646
{
47-
$this->acquire(FileLock::SHARED);
47+
$this->beConstructedWith($this->lock_file, FileLock::SHARED);
48+
49+
$this->acquire();
4850

4951
if (!file_exists($this->lock_file)) {
5052
throw new Exception('Lock file was not created');
@@ -61,7 +63,7 @@ public function it_should_acquire_an_shared_lock()
6163

6264
public function it_should_release_a_lock()
6365
{
64-
$this->acquire(FileLock::SHARED);
66+
$this->acquire();
6567
$this->release();
6668

6769
if (!flock(fopen($this->lock_file, 'r'), LOCK_EX|LOCK_NB)) {
@@ -79,7 +81,7 @@ public function it_should_throw_if_it_cannot_lock()
7981

8082
public function it_remove_its_lock_file_if_not_locked()
8183
{
82-
$this->beConstructedWith($this->lock_file, null, null, true);
84+
$this->beConstructedWith($this->lock_file, FileLock::EXCLUSIVE, FileLock::NON_BLOCKING, null, null, true);
8385

8486
$this->acquire();
8587
$this->release();
@@ -91,12 +93,12 @@ public function it_remove_its_lock_file_if_not_locked()
9193

9294
public function it_does_not_remove_its_lock_file_if_still_locked()
9395
{
94-
$this->beConstructedWith($this->lock_file, null, null, true);
96+
$this->beConstructedWith($this->lock_file, FileLock::SHARED, FileLock::NON_BLOCKING, null, null, true);
9597

9698
touch($this->lock_file);
9799
flock(fopen($this->lock_file, 'r'), LOCK_SH|LOCK_NB);
98100

99-
$this->acquire(FileLock::SHARED);
101+
$this->acquire();
100102
$this->release();
101103

102104
if (!file_exists($this->lock_file)) {
@@ -106,7 +108,7 @@ public function it_does_not_remove_its_lock_file_if_still_locked()
106108

107109
public function it_can_acquire_then_release_and_acquire_again()
108110
{
109-
$this->beConstructedWith($this->lock_file, null, null, true);
111+
$this->beConstructedWith($this->lock_file, FileLock::EXCLUSIVE, FileLock::NON_BLOCKING, null, null, true);
110112

111113
$this->acquire();
112114
$this->release();

src/FileFactory.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
use Psr\Log\LoggerInterface;
66
use Psr\Log\NullLogger;
77

8-
class FileFactory implements LockFactory
8+
class FileFactory
99
{
1010
private $lock_dir;
1111
private $hash_algo;
1212

1313
private $logger;
1414

15+
/**
16+
* Create a new FileFactory
17+
* @param string $lock_dir directory where lock files will be created
18+
* @param string $hash_algo hashing algotithms used to generate lock file name from identifier
19+
* see http://php.net/manual/en/function.hash-algos.php
20+
* @param LoggerInterface|null $logger
21+
*/
1522
public function __construct($lock_dir, $hash_algo = 'sha256', LoggerInterface $logger = null)
1623
{
1724
$this->lock_dir = $lock_dir;
@@ -20,15 +27,27 @@ public function __construct($lock_dir, $hash_algo = 'sha256', LoggerInterface $l
2027
$this->logger = $logger ?: new NullLogger;
2128
}
2229

23-
public function create($resource, $owner = null)
24-
{
30+
/**
31+
* Create a FileLock for $resource
32+
* @param string $resource resource identifier
33+
* @param string|null $owner owner name (for logging)
34+
* @param boolean $exclusive true for an exclusive lock, false for shared one
35+
* @param boolean $blocking true to wait for lock to be available, false to throw exception instead of waiting
36+
* @return FileLock
37+
*/
38+
public function create(
39+
$resource,
40+
$owner = null,
41+
$exclusive = FileLock::EXCLUSIVE,
42+
$blocking = FileLock::NON_BLOCKING
43+
) {
2544
if (!is_dir($this->lock_dir)) {
2645
mkdir($this->lock_dir, 0777, true);
2746
}
2847

2948
$path = $this->lock_dir.'/'.hash($this->hash_algo, serialize($resource)).'.lock';
3049

31-
$lock = new FileLock($path, $resource, $owner, true, $this->logger);
50+
$lock = new FileLock($path, $exclusive, $blocking, $resource, $owner, true, $this->logger);
3251

3352
return $lock;
3453
}

src/FileLock.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class FileLock implements Lock
1515
const NON_BLOCKING = false;
1616

1717
private $lock_file;
18+
private $exclusive;
19+
private $blocking;
1820
private $identifier;
1921
private $owner;
2022
private $fh;
@@ -24,19 +26,26 @@ class FileLock implements Lock
2426

2527
/**
2628
* @param string $lock_file path to file
29+
* @param boolean $exclusive true for an exclusive lock, false for shared one
30+
* @param boolean $blocking true to wait for lock to be available,
31+
* false to throw exception instead of waiting
2732
* @param string|null $identifier resource identifier (default to $lock_file) for logging
2833
* @param string|null $owner owner name for logging
2934
* @param boolean $remove_on_release remove file on release if no other lock remains
3035
* @param LoggerInterface $logger
3136
*/
3237
public function __construct(
3338
$lock_file,
39+
$exclusive = FileLock::EXCLUSIVE,
40+
$blocking = FileLock::NON_BLOCKING,
3441
$identifier = null,
3542
$owner = null,
3643
$remove_on_release = false,
3744
LoggerInterface $logger = null
3845
) {
3946
$this->lock_file = $lock_file;
47+
$this->exclusive = $exclusive;
48+
$this->blocking = $blocking;
4049
$this->identifier = $identifier?:$lock_file;
4150
$this->owner = $owner === null ? '' : $owner.': ';
4251
$this->remove_on_release = $remove_on_release;
@@ -45,21 +54,19 @@ public function __construct(
4554
}
4655

4756
/**
48-
* @param boolean $exclusive true for an exclusive lock, false for shared one
49-
* @param boolean $blocking true to wait for lock to be available, false to throw exception instead of waiting
5057
* @inherit
5158
*/
52-
public function acquire($exclusive = FileLock::EXCLUSIVE, $blocking = FileLock::NON_BLOCKING)
59+
public function acquire()
5360
{
54-
if ($exclusive === FileLock::EXCLUSIVE) {
61+
if ($this->exclusive === FileLock::EXCLUSIVE) {
5562
$lock_type = 'exclusive';
5663
$operation = LOCK_EX;
5764
} else {
5865
$lock_type = 'shared';
5966
$operation = LOCK_SH;
6067
}
6168

62-
if ($blocking === FileLock::NON_BLOCKING) {
69+
if ($this->blocking === FileLock::NON_BLOCKING) {
6370
$operation |= LOCK_NB;
6471
}
6572

src/LockFactory.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)