Example #1
0
File: Sync.php Project: n2n/n2n
 /**
  * @param unknown $class
  * @param unknown $key
  * @param unknown $operation
  * @throws IllegalStateException
  * @return \n2n\core\Lock
  */
 private static function createLock($class, $key, $operation)
 {
     if (self::$fsPath === null) {
         throw new IllegalStateException('Sync not initialized.');
     }
     $id = '';
     if ($class !== null) {
         $id .= 'c:' . self::determineClassName($class);
     }
     if ($key !== null) {
         $id .= '-k:' . $key;
     }
     $fileName = (string) self::$fsPath->ext(HashUtils::base36Md5Hash($id) . self::FILE_SUFFIX);
     $resource = IoUtils::fopen($fileName, 'c');
     $blocked = null;
     if ($operation & LOCK_NB) {
         if (!flock($resource, $operation, $blocked)) {
             return null;
         }
     } else {
         IoUtils::flock($resource, $operation, $blocked);
     }
     return new Lock($resource, $operation & LOCK_EX, $blocked, $fileName);
 }