10000 feature #23724 [Lock] Deprecate Filesystem/LockHandler (jderusse) · symfony/symfony@57a86fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 57a86fb

Browse files
committed
feature #23724 [Lock] Deprecate Filesystem/LockHandler (jderusse)
This PR was merged into the 3.4 branch. Discussion ---------- [Lock] Deprecate Filesystem/LockHandler | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#8243 This PR deprecate the `Filesystem\LockHandler` in favor of `Lock\SemaphoreStore` and `Lock\FlockStore`. Commits ------- 67ecc71 Deprecate Filesystem/LockHandler
2 parents c36262e + 67ecc71 commit 57a86fb

File tree

9 files changed

+77
-22
lines changed

9 files changed

+77
-22
lines changed

UPGRADE-3.4.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ Debug
1111

1212
* Support for stacked errors in the `ErrorHandler` is deprecated and will be removed in Symfony 4.0.
1313

14+
Filesystem
15+
----------
16+
17+
* The `Symfony\Component\Filesystem\LockHandler` class has been deprecated,
18+
use the `Symfony\Component\Lock\Store\FlockStore` class
19+
or the `Symfony\Component\Lock\Store\FlockStore\SemaphoreStore` class directly instead.
20+
1421
Finder
1522
------
1623

UPGRADE-4.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ ExpressionLanguage
147147
class has been removed. You should use the `CacheItemPoolInterface` interface
148148
instead.
149149

150+
Filesystem
151+
----------
152+
153+
* The `Symfony\Component\Filesystem\LockHandler` has been removed,
154+
use the `Symfony\Component\Lock\Store\FlockStore` class
155+
or the `Symfony\Component\Lock\Store\FlockStore\SemaphoreStore` class directly instead.
156+
150157
Finder
151158
------
152159

src/Symfony/Component/Console/Command/LockableTrait.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
use Symfony\Component\Console\Exception\LogicException;
1515
use Symfony\Component\Console\Exception\RuntimeException;
16-
use Symfony\Component\Filesystem\LockHandler;
16+
use Symfony\Component\Lock\Factory;
17+
use Symfony\Component\Lock\Lock;
18+
use Symfony\Component\Lock\Store\FlockStore;
19+
use Symfony\Component\Lock\Store\SemaphoreStore;
1720

1821
/**
1922
* Basic lock feature for commands.
@@ -22,7 +25,8 @@
2225
*/
2326
trait LockableTrait
2427
{
25-
private $lockHandler;
28+
/** @var Lock */
29+
private $lock;
2630

2731
/**
2832
* Locks a command.
@@ -31,18 +35,23 @@ trait LockableTrait
3135
*/
3236
private function lock($name = null, $blocking = false)
3337
{
34-
if (!class_exists(LockHandler::class)) {
35-
throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.');
38+
if (!class_exists(SemaphoreStore::class)) {
39+
throw new RuntimeException('To enable the locking feature you must install the symfony/lock component.');
3640
}
3741

38-
if (null !== $this->lockHandler) {
42+
if (null !== $this->lock) {
3943
throw new LogicException('A lock is already in place.');
4044
}
4145

42-
$this->lockHandler = new LockHandler($name ?: $this->getName());
46+
if (SemaphoreStore::isSupported($blocking)) {
47+
$store = new SemaphoreStore();
48+
} else {
49+
$store = new FlockStore(sys_get_temp_dir());
50+
}
4351

44-
if (!$this->lockHandler->lock($blocking)) {
45-
$this->lockHandler = null;
52+
$this->lock = (new Factory($store))->createLock($name ?: $this->getName());
53+
if (!$this->lock->acquire($blocking)) {
54+
$this->lock = null;
4655

4756
return false;
4857
}
@@ -55,9 +64,9 @@ private function lock($name = null, $blocking = false)
5564
*/
5665
private function release()
5766
{
58-
if ($this->lockHandler) {
59-
$this->lockHandler->release();
60-
$this->lockHandler = null;
67+
if ($this->lock) {
68+
$this->lock->release();
69+
$this->lock = null;
6170
}
6271
}
6372
}

src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Tester\CommandTester;
16-
use Symfony\Component\Filesystem\LockHandler;
16+
use Symfony\Component\Lock\Factory;
17+
use Symfony\Component\Lock\Store\FlockStore;
18+
use Symfony\Component\Lock\Store\SemaphoreStore;
1719

1820
class LockableTraitTest extends TestCase
1921
{
@@ -39,8 +41,14 @@ public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
3941
{
4042
$command = new \FooLockCommand();
4143

42-
$lock = new LockHandler($command->getName());
43-
$lock->lock();
44+
if (SemaphoreStore::isSupported(false)) {
45+
$store = new SemaphoreStore();
46+
} else {
47+
$store = new FlockStore(sys_get_temp_dir());
48+
}
49+
50+
$lock = (new Factory($store))->createLock($command->getName());
51+
$lock->acquire();
4452

4553
$tester = new CommandTester($command);
4654
$this->assertSame(1, $tester->execute(array()));

src/Symfony/Component/Console/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
"symfony/http-kernel": "~2.8|~3.0|~4.0",
2626
"symfony/event-dispatcher": "~2.8|~3.0|~4.0",
2727
"symfony/dependency-injection": "~3.3|~4.0",
28-
"symfony/filesystem": "~2.8|~3.0|~4.0",
28+
"symfony/lock": "~3.4|~4.0",
2929
"symfony/process": "~3.3|~4.0",
3030
"psr/log": "~1.0"
3131
},
3232
"suggest": {
3333
"symfony/event-dispatcher": "",
34-
"symfony/filesystem": "",
34+
"symfony/lock": "",
3535
"symfony/process": "",
3636
"psr/log": "For using the console logger"
3737
},

src/Symfony/Component/Filesystem/LockHandler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
namespace Symfony\Component\Filesystem;
1313

14+
@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use %s or %s instead.', LockHandler::class, SemaphoreStore::class, FlockStore::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Filesystem\Exception\IOException;
17+
use Symfony\Component\Lock\Store\FlockStore;
18+
use Symfony\Component\Lock\Store\SemaphoreStore;
1519

1620
/**
1721
* LockHandler class provides a simple abstraction to lock anything by means of
@@ -25,6 +29,8 @@
2529
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2630
* @author Romain Neutron <imprec@gmail.com>
2731
* @author Nicolas Grekas <p@tchwork.com>
32+
*
33+
* @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\Lock\Store\SemaphoreStore or Symfony\Component\Lock\Store\FlockStore instead.
2834
*/
2935
class LockHandler
3036
{

src/Symfony/Component/Filesystem/Tests/LockHandlerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Component\Filesystem\Filesystem;
1717
use Symfony\Component\Filesystem\LockHandler;
1818

19+
/**
20+
* @group legacy
21+
*/
1922
class LockHandlerTest extends TestCase
2023
{
2124
/**

src/Symfony/Component/Lock/Store/SemaphoreStore.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,24 @@
2424
*/
2525
class SemaphoreStore implements StoreInterface
2626
{
27-
public static function isSupported()
27+
/**
28+
* Returns whether or not the store is supported.
29+
*
30+
* @param bool|null $blocking When not null, checked again the blocking mode.
31+
*
32+
* @return bool
33+
*/
34+
public static function isSupported($blocking = null)
2835
{
29-
return extension_loaded('sysvsem');
36+
if (!extension_loaded('sysvsem')) {
37+
return false;
38+
}
39+
40+
if ($blocking === false && \PHP_VERSION_ID < 50601) {
41+
return false;
42+
}
43+
44+
return true;
3045
}
3146

3247
public function __construct()

src/Symfony/Component/Lock/Tests/Store/BlockingStoreTestTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract protected function getStore();
3535
public function testBlockingLocks()
3636
{
3737
// Amount a microsecond used to order async actions
38-
$clockDelay = 50000;
38+
$clockDelay = 200000;
3939

4040
if (\PHP_VERSION_ID < 50600 || defined('HHVM_VERSION_ID')) {
4141
$this->markTestSkipped('The PHP engine does not keep resource in child forks');
A5A9
@@ -49,7 +49,7 @@ public function testBlockingLocks()
4949

5050
if ($childPID1 = pcntl_fork()) {
5151
// give time to fork to start
52-
usleep(2 * $clockDelay);
52+
usleep(1 * $clockDelay);
5353

5454
try {
5555
// This call should failed given the lock should already by acquired by the child #1
@@ -69,8 +69,8 @@ public function testBlockingLocks()
6969
} else {
7070
try {
7171
$store->save($key);
72-
// Wait 3 ClockDelay to let parent process to finish
73-
usleep(3 * $clockDelay);
72+
// Wait 2 ClockDelay to let parent process to finish
73+
usleep(2 * $clockDelay);
7474
$store->delete($key);
7575
exit(0);
7676
} catch (\Exception $e) {

0 commit comments

Comments
 (0)
0