/** * @param string $subject * @param boolean $exclusiveLock TRUE to, acquire an exclusive (write) lock, FALSE for a shared (read) lock. * @throws LockNotAcquiredException * @throws \TYPO3\Flow\Utility\Exception * @return void */ public function acquire($subject, $exclusiveLock) { if ($this->isWindowsOS()) { return; } if (self::$temporaryDirectory === null) { if (Bootstrap::$staticObjectManager === null || !Bootstrap::$staticObjectManager->isRegistered(\TYPO3\Flow\Utility\Environment::class)) { throw new LockNotAcquiredException('Environment object could not be accessed', 1386680952); } $environment = Bootstrap::$staticObjectManager->get(\TYPO3\Flow\Utility\Environment::class); $temporaryDirectory = Files::concatenatePaths(array($environment->getPathToTemporaryDirectory(), 'Lock')); Files::createDirectoryRecursively($temporaryDirectory); self::$temporaryDirectory = $temporaryDirectory; } $this->lockFileName = Files::concatenatePaths(array(self::$temporaryDirectory, md5($subject))); if (($this->filePointer = @fopen($this->lockFileName, 'r')) === false) { if (($this->filePointer = @fopen($this->lockFileName, 'w')) === false) { throw new LockNotAcquiredException(sprintf('Lock file "%s" could not be opened', $this->lockFileName), 1386520596); } } if ($exclusiveLock === false && flock($this->filePointer, LOCK_SH) === true) { // Shared lock acquired } elseif ($exclusiveLock === true && flock($this->filePointer, LOCK_EX) === true) { // Exclusive lock acquired } else { throw new LockNotAcquiredException(sprintf('Could not lock file "%s"', $this->lockFileName), 1386520597); } }
/** * Sets the temporaryDirectory as static variable for the lock class. * * @throws LockNotAcquiredException * @throws \TYPO3\Flow\Utility\Exception * return void; */ protected function configureTemporaryDirectory() { if (Bootstrap::$staticObjectManager === null || !Bootstrap::$staticObjectManager->isRegistered(\TYPO3\Flow\Utility\Environment::class)) { throw new LockNotAcquiredException('Environment object could not be accessed', 1386680952); } $environment = Bootstrap::$staticObjectManager->get('TYPO3\\Flow\\Utility\\Environment'); $temporaryDirectory = Files::concatenatePaths([$environment->getPathToTemporaryDirectory(), 'Lock']); Files::createDirectoryRecursively($temporaryDirectory); self::$temporaryDirectory = $temporaryDirectory; }