LockableTrait.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Command;
  11. use Symfony\Component\Console\Exception\LogicException;
  12. use Symfony\Component\Lock\LockFactory;
  13. use Symfony\Component\Lock\LockInterface;
  14. use Symfony\Component\Lock\Store\FlockStore;
  15. use Symfony\Component\Lock\Store\SemaphoreStore;
  16. /**
  17. * Basic lock feature for commands.
  18. *
  19. * @author Geoffrey Brier <geoffrey.brier@gmail.com>
  20. */
  21. trait LockableTrait
  22. {
  23. private ?LockInterface $lock = null;
  24. /**
  25. * Locks a command.
  26. */
  27. private function lock(?string $name = null, bool $blocking = false): bool
  28. {
  29. if (!class_exists(SemaphoreStore::class)) {
  30. throw new LogicException('To enable the locking feature you must install the symfony/lock component. Try running "composer require symfony/lock".');
  31. }
  32. if (null !== $this->lock) {
  33. throw new LogicException('A lock is already in place.');
  34. }
  35. if (SemaphoreStore::isSupported()) {
  36. $store = new SemaphoreStore();
  37. } else {
  38. $store = new FlockStore();
  39. }
  40. $this->lock = (new LockFactory($store))->createLock($name ?: $this->getName());
  41. if (!$this->lock->acquire($blocking)) {
  42. $this->lock = null;
  43. return false;
  44. }
  45. return true;
  46. }
  47. /**
  48. * Releases the command lock if there is one.
  49. */
  50. private function release(): void
  51. {
  52. if ($this->lock) {
  53. $this->lock->release();
  54. $this->lock = null;
  55. }
  56. }
  57. }