|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of ninja-mutex. |
|
4
|
|
|
* |
|
5
|
|
|
* (C) Kamil Dziedzic <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace NinjaMutex; |
|
11
|
|
|
|
|
12
|
|
|
use NinjaMutex\Lock\LockInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Mutex fabric |
|
16
|
|
|
* |
|
17
|
|
|
* @author Kamil Dziedzic <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class MutexFabric |
|
20
|
|
|
{ |
|
21
|
|
|
protected $defaultLockImplementorName; |
|
22
|
|
|
protected $implementors = array(); |
|
23
|
|
|
protected $mutexes = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $lockImplementorName |
|
27
|
|
|
* @param $lockImplementor |
|
28
|
|
|
* @throws MutexException |
|
29
|
84 |
|
*/ |
|
30
|
|
|
public function __construct($lockImplementorName, LockInterface $lockImplementor) |
|
31
|
84 |
|
{ |
|
32
|
84 |
|
$this->registerLockImplementor($lockImplementorName, $lockImplementor); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $name |
|
37
|
|
|
* @param LockInterface $implementor |
|
38
|
|
|
* @throws MutexException |
|
39
|
|
|
*/ |
|
40
|
84 |
|
public function registerLockImplementor($name, LockInterface $implementor) |
|
41
|
|
|
{ |
|
42
|
84 |
|
if (isset($this->implementors[$name])) { |
|
43
|
14 |
|
throw new MutexException(sprintf('Name %s is already used', $name)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
84 |
|
if (null === $this->defaultLockImplementorName) { |
|
47
|
84 |
|
$this->defaultLockImplementorName = $name; |
|
48
|
84 |
|
} |
|
49
|
|
|
|
|
50
|
84 |
|
$this->implementors[$name] = $implementor; |
|
51
|
84 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $registeredLockImplementorName |
|
55
|
|
|
*/ |
|
56
|
14 |
|
public function setDefaultLockImplementorName($registeredLockImplementorName) |
|
57
|
|
|
{ |
|
58
|
14 |
|
$this->defaultLockImplementorName = $registeredLockImplementorName; |
|
59
|
14 |
|
} |
|
60
|
|
|
|
|
61
|
56 |
|
public function getDefaultLockImplementorName() |
|
62
|
|
|
{ |
|
63
|
56 |
|
return $this->defaultLockImplementorName; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create and/or get mutex |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @param string $registeredLockImplementorName |
|
71
|
|
|
* @return Mutex |
|
72
|
|
|
*/ |
|
73
|
28 |
|
public function get($name, $registeredLockImplementorName = null) |
|
74
|
|
|
{ |
|
75
|
28 |
|
if (null === $registeredLockImplementorName) { |
|
76
|
14 |
|
$registeredLockImplementorName = $this->getDefaultLockImplementorName(); |
|
77
|
14 |
|
} |
|
78
|
|
|
|
|
79
|
28 |
|
if (!isset($this->mutexes[$registeredLockImplementorName][$name])) { |
|
80
|
28 |
|
$this->createMutex($name, $registeredLockImplementorName); |
|
81
|
28 |
|
} |
|
82
|
|
|
|
|
83
|
28 |
|
return $this->mutexes[$registeredLockImplementorName][$name]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* @param string $registeredLockImplementorName |
|
89
|
|
|
*/ |
|
90
|
28 |
|
protected function createMutex($name, $registeredLockImplementorName) |
|
91
|
|
|
{ |
|
92
|
28 |
|
$this->mutexes[$registeredLockImplementorName][$name] = new Mutex($name, $this->implementors[$registeredLockImplementorName]); |
|
93
|
28 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|