-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console][Lock] Allow LockableTrait to use custom stores #31914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
38e7a8a
7b375db
c097950
51626d9
dff0863
ac74dde
cf89732
300bcc8
588d6d9
9605bdc
c1f3749
bbfb8aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,10 @@ | |
|
||
use Symfony\Component\Console\Exception\LogicException; | ||
use Symfony\Component\Lock\Factory; | ||
use Symfony\Component\Lock\Lock; | ||
use Symfony\Component\Lock\LockInterfa 8000 ce; | ||
use Symfony\Component\Lock\Store\FlockStore; | ||
use Symfony\Component\Lock\Store\SemaphoreStore; | ||
use Symfony\Component\Lock\StoreInterface; | ||
|
||
/** | ||
* Basic lock feature for commands. | ||
|
@@ -24,9 +25,16 @@ | |
*/ | ||
trait LockableTrait | ||
{ | ||
/** @var Lock */ | ||
/** | ||
* @var LockInterface | ||
*/ | ||
private $lock; | ||
|
||
/** | ||
* @var StoreInterface | ||
*/ | ||
private $store; | ||
|
||
/** | ||
* Locks a command. | ||
* | ||
|
@@ -42,13 +50,15 @@ private function lock($name = null, $blocking = false) | |
throw new LogicException('A lock is already in place.'); | ||
} | ||
|
||
if (SemaphoreStore::isSupported()) { | ||
$store = new SemaphoreStore(); | ||
} else { | ||
$store = new FlockStore(); | ||
if (null === $this->store) { | ||
if (SemaphoreStore::isSupported()) { | ||
$this->store = new SemaphoreStore(); | ||
} else { | ||
$this->store = new FlockStore(); | ||
} | ||
} | ||
|
||
$this->lock = (new Factory($store))->createLock($name ?: $this->getName()); | ||
$this->lock = (new Factory($this->store))->createLock($name ?: $this->getName()); | ||
if (!$this->lock->acquire($blocking)) { | ||
$this->lock = null; | ||
|
||
|
@@ -68,4 +78,12 @@ private function release() | |
$this->lock = null; | ||
} | ||
} | ||
|
||
/** | ||
* Sets the internal store for the command to be used. | ||
*/ | ||
public function setStore(StoreInterface $store) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about passing a non-default store as optional 3rd arg in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. meaning the user injects his own store if needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that's the case then this change wouldn't be needed because you can implement the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right ... the user can also set We should favor pure DI / service usage IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to solve a configurable store that can be used in commands, this can either be done by:
Why? Because currently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is that a real problem? IMHO it's the explicit way.. compared to calling an arbitrary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we should be using a marker interface. I've updated the PR. |
||
{ | ||
$this->store = $store; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Lock; | ||
|
||
interface LockableInterface | ||
{ | ||
/** | ||
* Sets the current store. | ||
* | ||
* @param StoreInterface $store The store to be used | ||
*/ | ||
public function setStore(StoreInterface $store); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beware, the interface has been renamed in #32198