Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Command; Chris@0: Chris@0: use Symfony\Component\Console\Exception\LogicException; Chris@0: use Symfony\Component\Console\Exception\RuntimeException; Chris@0: use Symfony\Component\Filesystem\LockHandler; Chris@0: Chris@0: /** Chris@0: * Basic lock feature for commands. Chris@0: * Chris@0: * @author Geoffrey Brier Chris@0: */ Chris@0: trait LockableTrait Chris@0: { Chris@0: private $lockHandler; Chris@0: Chris@0: /** Chris@0: * Locks a command. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: private function lock($name = null, $blocking = false) Chris@0: { Chris@0: if (!class_exists(LockHandler::class)) { Chris@0: throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.'); Chris@0: } Chris@0: Chris@0: if (null !== $this->lockHandler) { Chris@0: throw new LogicException('A lock is already in place.'); Chris@0: } Chris@0: Chris@0: $this->lockHandler = new LockHandler($name ?: $this->getName()); Chris@0: Chris@0: if (!$this->lockHandler->lock($blocking)) { Chris@0: $this->lockHandler = null; Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Releases the command lock if there is one. Chris@0: */ Chris@0: private function release() Chris@0: { Chris@0: if ($this->lockHandler) { Chris@0: $this->lockHandler->release(); Chris@0: $this->lockHandler = null; Chris@0: } Chris@0: } Chris@0: }