|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\File\Lock; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
use Symfony\Component\Lock\Factory; |
|
17
|
|
|
use Symfony\Component\Lock\Lock; |
|
18
|
|
|
use Symfony\Component\Lock\Store\SemaphoreStore; |
|
19
|
|
|
use Symfony\Component\Lock\StoreInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @internal |
|
23
|
|
|
* |
|
24
|
|
|
* @author Rob Frawley 2nd <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class LockFactory |
|
27
|
|
|
{ |
|
28
|
|
|
private static $logger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var StoreInterface|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $store; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Factory|null |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $factory; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Resets the lock state (including logger, store, and factory) |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function reset(): void |
|
44
|
|
|
{ |
|
45
|
|
|
self::$logger = null; |
|
46
|
|
|
self::$store = null; |
|
47
|
|
|
self::$factory = null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param LoggerInterface|null $logger |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function setLogger(LoggerInterface $logger = null): void |
|
54
|
|
|
{ |
|
55
|
|
|
self::$factory = null; |
|
56
|
|
|
self::$logger = $logger; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return LoggerInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getLogger(): LoggerInterface |
|
63
|
|
|
{ |
|
64
|
|
|
return self::$logger = self::$logger ?: new NullLogger(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param StoreInterface|null $store |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function setStore(StoreInterface $store = null): void |
|
71
|
|
|
{ |
|
72
|
|
|
self::$factory = null; |
|
73
|
|
|
self::$store = $store; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return StoreInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function getStore(): StoreInterface |
|
80
|
|
|
{ |
|
81
|
|
|
return self::$store = self::$store ?: new SemaphoreStore(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return Factory |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function getFactory(): Factory |
|
88
|
|
|
{ |
|
89
|
|
|
if (null === self::$factory) { |
|
90
|
|
|
self::$factory = new Factory(self::getStore()); |
|
91
|
|
|
self::$factory->setLogger(self::getLogger()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return self::$factory; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param mixed $context |
|
99
|
|
|
* |
|
100
|
|
|
* @return null|Lock |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function create($context): ?Lock |
|
103
|
|
|
{ |
|
104
|
|
|
return self::getFactory()->createLock(self::stringifyContext($context)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param mixed $context |
|
109
|
|
|
* |
|
110
|
|
|
* @return null|Lock |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function acquire($context): ?Lock |
|
113
|
|
|
{ |
|
114
|
|
|
$lock = self::create($context); |
|
115
|
|
|
|
|
116
|
|
|
return $lock->acquire(false) ? $lock : null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param mixed $context |
|
121
|
|
|
* |
|
122
|
|
|
* @return Lock |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function blocking($context): Lock |
|
125
|
|
|
{ |
|
126
|
|
|
($lock = self::create($context))->acquire(true); |
|
127
|
|
|
|
|
128
|
|
|
return $lock; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param mixed $context |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
private static function stringifyContext($context): string |
|
137
|
|
|
{ |
|
138
|
|
|
return is_object($context) ? self::stringifyObjectContext($context) : (string) $context; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param object $object |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
private static function stringifyObjectContext($object): string |
|
147
|
|
|
{ |
|
148
|
|
|
if (method_exists($object, '__toString') && !empty($string = $object->__toString())) { |
|
149
|
|
|
return $string; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return sprintf('[%s]="%s"', get_class($object), $string ?? spl_object_hash($object)); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|