Files

65 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2017-01-30 16:16:31 -05:00
<?php
/* HOW TO USE
$lockFile = Lock::getLock('lock-name');
if (is_resource($lockFile)) {
// you have a lock here, do whatever you want
Lock::freeLock($lockFile);
}
else {
// you could not get the lock. show a message or throw an exception or whatever
}
*/
class Lock
{
2018-05-21 11:49:15 -04:00
/**
* Creates a lockfile and acquires an exclusive lock on it.
*
* @param string $name The name of the lockfile.
* @param boolean $blocking Block until lock becomes available (default: don't block, just fail)
* @return mixed Returns the lockfile, or FALSE if a lock could not be acquired.
*/
public static function getLock($name, $blocking = false)
2017-01-30 16:16:31 -05:00
{
2018-05-21 11:49:15 -04:00
if (!preg_match('/^[A-Za-z0-9\.\-_]+$/', $name)) {
throw new InvalidArgumentException('Invalid lock name: "' . $name . '"');
}
$filename = static::getLockDir() . '/' . $name;
if (!preg_match('/\.lo?ck$/', $filename)) {
$filename .= '.lck';
}
if (!file_exists($filename)) {
file_put_contents($filename, '');
chmod($filename, 0666); // if the file cant be opened for writing later, getting the lock will fail
}
$lockFile = fopen($filename, 'w+');
if (!flock($lockFile, $blocking ? LOCK_EX : LOCK_EX|LOCK_NB)) {
fclose($lockFile);
return false;
}
return $lockFile;
2017-01-30 16:16:31 -05:00
}
2018-05-21 11:49:15 -04:00
/**
* Free a lock.
*
* @param resource $lockFile
*/
public static function freeLock($lockFile)
2017-01-30 16:16:31 -05:00
{
2018-05-21 11:49:15 -04:00
if ($lockFile) {
flock($lockFile, LOCK_UN);
fclose($lockFile);
}
2017-01-30 16:16:31 -05:00
}
2018-05-21 11:49:15 -04:00
public static function getLockDir()
2017-01-30 16:16:31 -05:00
{
2018-05-21 11:49:15 -04:00
return sys_get_temp_dir();
2017-01-30 16:16:31 -05:00
}
2018-05-21 11:49:15 -04:00
}