diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 19386d9721ae9..5b56526bcaecb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -69,6 +69,7 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\Lock\Factory; use Symfony\Component\Lock\Lock; +use Symfony\Component\Lock\LockableInterface; use Symfony\Component\Lock\LockInterface; use Symfony\Component\Lock\Store\FlockStore; use Symfony\Component\Lock\Store\StoreFactory; @@ -419,6 +420,8 @@ public function load(array $configs, ContainerBuilder $container) ->addTag('mime.mime_type_guesser'); $container->registerForAutoconfiguration(LoggerAwareInterface::class) ->addMethodCall('setLogger', [new Reference('logger')]); + $container->registerForAutoconfiguration(LockableInterface::class) + ->addMethodCall('setStore', [new Reference('lock.default.store')]); if (!$container->getParameter('kernel.debug')) { // remove tagged iterator argument for resource checkers diff --git a/src/Symfony/Component/Console/Command/LockableTrait.php b/src/Symfony/Component/Console/Command/LockableTrait.php index f4ebe45bf37c7..41072806f5fec 100644 --- a/src/Symfony/Component/Console/Command/LockableTrait.php +++ b/src/Symfony/Component/Console/Command/LockableTrait.php @@ -13,9 +13,10 @@ use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Lock\Factory; -use Symfony\Component\Lock\Lock; +use Symfony\Component\Lock\LockInterface; 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) + { + $this->store = $store; + } } diff --git a/src/Symfony/Component/Lock/LockableInterface.php b/src/Symfony/Component/Lock/LockableInterface.php new file mode 100644 index 0000000000000..de3b266ca29c6 --- /dev/null +++ b/src/Symfony/Component/Lock/LockableInterface.php @@ -0,0 +1,22 @@ + + * + * 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); +}