File tree 3 files changed +162
-0
lines changed
3 files changed +162
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * PHP Mutex implementation.
5
+ *
6
+ * @author Maksim Masiukevich <[email protected] >
7
+ * @license MIT
8
+ * @license https://opensource.org/licenses/MIT
9
+ */
10
+
11
+ declare (strict_types = 1 );
12
+
13
+ namespace ServiceBus \Mutex ;
14
+
15
+ use Amp \Promise ;
16
+ use Amp \Success ;
17
+ use ServiceBus \Mutex \Storage \InMemoryCollectionStorage ;
18
+
19
+ /**
20
+ *
21
+ */
22
+ final class InMemoryLockCollection implements LockCollection
23
+ {
24
+ public function __destruct ()
25
+ {
26
+ InMemoryCollectionStorage::instance ()->reset ();
27
+ }
28
+
29
+ /**
30
+ * @inheritDoc
31
+ */
32
+ public function has (string $ key ): Promise
33
+ {
34
+ return new Success (InMemoryCollectionStorage::instance ()->has ($ key ));
35
+ }
36
+
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ public function place (string $ key , Lock $ lock ): Promise
41
+ {
42
+ InMemoryCollectionStorage::instance ()->add ($ key , $ lock );
43
+
44
+ return new Success ();
45
+ }
46
+
47
+ /**
48
+ * @inheritDoc
49
+ */
50
+ public function remove (string $ key ): Promise
51
+ {
52
+ InMemoryCollectionStorage::instance ()->remove ($ key );
53
+
54
+ return new Success ();
55
+ }
56
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * PHP Mutex implementation.
5
+ *
6
+ * @author Maksim Masiukevich <[email protected] >
7
+ * @license MIT
8
+ * @license https://opensource.org/licenses/MIT
9
+ */
10
+
11
+ declare (strict_types = 1 );
12
+
13
+ namespace ServiceBus \Mutex ;
14
+
15
+ use Amp \Promise ;
16
+
17
+ /**
18
+ *
19
+ */
20
+ interface LockCollection
21
+ {
22
+ public function has (string $ key ): Promise ;
23
+
24
+ public function place (string $ key , Lock $ lock ): Promise ;
25
+
26
+ public function remove (string $ key ): Promise ;
27
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * PHP Mutex implementation.
5
+ *
6
+ * @author Maksim Masiukevich <[email protected] >
7
+ * @license MIT
8
+ * @license https://opensource.org/licenses/MIT
9
+ */
10
+
11
+ declare (strict_types = 1 );
12
+
13
+ namespace ServiceBus \Mutex \Storage ;
14
+
15
+ use ServiceBus \Mutex \Lock ;
16
+
17
+ /**
18
+ * @internal
19
+ */
20
+ final class InMemoryCollectionStorage
21
+ {
22
+ /**
23
+ * @psalm-var array<string, \ServiceBus\Mutex\Lock>
24
+ */
25
+ private $ localStorage = [];
26
+
27
+ /** @var self|null */
28
+ private static $ instance = null ;
29
+
30
+ /**
31
+ * @return self
32
+ */
33
+ public static function instance (): self
34
+ {
35
+ if (self ::$ instance === null )
36
+ {
37
+ self ::$ instance = new self ();
38
+ }
39
+
40
+ return self ::$ instance ;
41
+ }
42
+
43
+ public function has (string $ key ): bool
44
+ {
45
+ return isset ($ this ->localStorage [$ key ]);
46
+ }
47
+
48
+ public function remove (string $ key ): void
49
+ {
50
+ unset($ this ->localStorage [$ key ]);
51
+ }
52
+
53
+ public function add (string $ key , Lock $ lock ): void
54
+ {
55
+ $ this ->localStorage [$ key ] = $ lock ;
56
+ }
57
+
58
+ /**
59
+ * Reset instance.
60
+ */
61
+ public function reset (): void
62
+ {
63
+ self ::$ instance = null ;
64
+ }
65
+
66
+ /**
67
+ * @codeCoverageIgnore
68
+ */
69
+ private function __clone ()
70
+ {
71
+ }
72
+
73
+ /**
74
+ * @codeCoverageIgnore
75
+ */
76
+ private function __construct ()
77
+ {
78
+ }
79
+ }
You can’t perform that action at this time.
0 commit comments