LockableTrait.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Attribute\AsCommand;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. use Symfony\Component\Lock\LockFactory;
  14. use Symfony\Component\Lock\LockInterface;
  15. use Symfony\Component\Lock\Store\FlockStore;
  16. use Symfony\Component\Lock\Store\SemaphoreStore;
  17. /**
  18. * Basic lock feature for commands.
  19. *
  20. * @author Geoffrey Brier <geoffrey.brier@gmail.com>
  21. */
  22. trait LockableTrait
  23. {
  24. private ?LockInterface $lock = null;
  25. private ?LockFactory $lockFactory = null;
  26. /**
  27. * Locks a command.
  28. */
  29. private function lock(?string $name = null, bool $blocking = false): bool
  30. {
  31. if (!class_exists(SemaphoreStore::class)) {
  32. throw new LogicException('To enable the locking feature you must install the symfony/lock component. Try running "composer require symfony/lock".');
  33. }
  34. if (null !== $this->lock) {
  35. throw new LogicException('A lock is already in place.');
  36. }
  37. if (null === $this->lockFactory) {
  38. if (SemaphoreStore::isSupported()) {
  39. $store = new SemaphoreStore();
  40. } else {
  41. $store = new FlockStore();
  42. }
  43. $this->lockFactory = new LockFactory($store);
  44. }
  45. if (!$name) {
  46. if ($this instanceof Command) {
  47. $name = $this->getName();
  48. } elseif ($attribute = (new \ReflectionClass($this::class))->getAttributes(AsCommand::class)) {
  49. $name = $attribute[0]->newInstance()->name;
  50. } else {
  51. throw new LogicException(\sprintf('Lock name missing: provide it via "%s()", #[AsCommand] attribute, or by extending Command class.', __METHOD__));
  52. }
  53. }
  54. $this->lock = $this->lockFactory->createLock($name);
  55. if (!$this->lock->acquire($blocking)) {
  56. $this->lock = null;
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * Releases the command lock if there is one.
  63. */
  64. private function release(): void
  65. {
  66. if ($this->lock) {
  67. $this->lock->release();
  68. $this->lock = null;
  69. }
  70. }
  71. }