1 | <?php |
||
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) |
|
60 | |||
61 | 56 | public function getDefaultLockImplementorName() |
|
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) |
|
94 | } |
||
95 |