This commit is contained in:
2020-02-01 16:47:12 +07:00
commit 4c619ad6e6
16739 changed files with 3329179 additions and 0 deletions

42
vendor/yiisoft/yii2/mutex/DbMutex.php vendored Normal file
View File

@@ -0,0 +1,42 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\di\Instance;
/**
* DbMutex is the base class for classes, which relies on database while implementing mutex "lock" mechanism.
*
* @see Mutex
*
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
abstract class DbMutex extends Mutex
{
/**
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the Mutex object is created, if you want to change this property, you should only assign
* it with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/
public $db = 'db';
/**
* Initializes generic database table based mutex implementation.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
}
}

180
vendor/yiisoft/yii2/mutex/FileMutex.php vendored Normal file
View File

@@ -0,0 +1,180 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
/**
* FileMutex implements mutex "lock" mechanism via local file system files.
*
* This component relies on PHP `flock()` function.
*
* Application configuration example:
*
* ```php
* [
* 'components' => [
* 'mutex' => [
* 'class' => 'yii\mutex\FileMutex'
* ],
* ],
* ]
* ```
*
* > Note: this component can maintain the locks only for the single web server,
* > it probably will not suffice in case you are using cloud server solution.
*
* > Warning: due to `flock()` function nature this component is unreliable when
* > using a multithreaded server API like ISAPI.
*
* @see Mutex
*
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
class FileMutex extends Mutex
{
/**
* @var string the directory to store mutex files. You may use [path alias](guide:concept-aliases) here.
* Defaults to the "mutex" subdirectory under the application runtime path.
*/
public $mutexPath = '@runtime/mutex';
/**
* @var int the permission to be set for newly created mutex files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var int the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;
/**
* @var resource[] stores all opened lock files. Keys are lock names and values are file handles.
*/
private $_files = [];
/**
* Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like
* operating systems.
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
$this->mutexPath = Yii::getAlias($this->mutexPath);
if (!is_dir($this->mutexPath)) {
FileHelper::createDirectory($this->mutexPath, $this->dirMode, true);
}
}
/**
* Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param int $timeout time (in seconds) to wait for lock to become released.
* @return bool acquiring result.
*/
protected function acquireLock($name, $timeout = 0)
{
$filePath = $this->getLockFilePath($name);
$waitTime = 0;
while (true) {
$file = fopen($filePath, 'w+');
if ($file === false) {
return false;
}
if ($this->fileMode !== null) {
@chmod($filePath, $this->fileMode);
}
if (!flock($file, LOCK_EX | LOCK_NB)) {
fclose($file);
if (++$waitTime > $timeout) {
return false;
}
sleep(1);
continue;
}
// Under unix we delete the lock file before releasing the related handle. Thus it's possible that we've acquired a lock on
// a non-existing file here (race condition). We must compare the inode of the lock file handle with the inode of the actual lock file.
// If they do not match we simply continue the loop since we can assume the inodes will be equal on the next try.
// Example of race condition without inode-comparison:
// Script A: locks file
// Script B: opens file
// Script A: unlinks and unlocks file
// Script B: locks handle of *unlinked* file
// Script C: opens and locks *new* file
// In this case we would have acquired two locks for the same file path.
if (DIRECTORY_SEPARATOR !== '\\' && fstat($file)['ino'] !== @fileinode($filePath)) {
clearstatcache(true, $filePath);
flock($file, LOCK_UN);
fclose($file);
continue;
}
$this->_files[$name] = $file;
return true;
}
// Should not be reached normally.
return false;
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return bool release result.
*/
protected function releaseLock($name)
{
if (!isset($this->_files[$name])) {
return false;
}
if (DIRECTORY_SEPARATOR === '\\') {
// Under windows it's not possible to delete a file opened via fopen (either by own or other process).
// That's why we must first unlock and close the handle and then *try* to delete the lock file.
flock($this->_files[$name], LOCK_UN);
fclose($this->_files[$name]);
@unlink($this->getLockFilePath($name));
} else {
// Under unix it's possible to delete a file opened via fopen (either by own or other process).
// That's why we must unlink (the currently locked) lock file first and then unlock and close the handle.
unlink($this->getLockFilePath($name));
flock($this->_files[$name], LOCK_UN);
fclose($this->_files[$name]);
}
unset($this->_files[$name]);
return true;
}
/**
* Generate path for lock file.
* @param string $name
* @return string
* @since 2.0.10
*/
protected function getLockFilePath($name)
{
return $this->mutexPath . DIRECTORY_SEPARATOR . md5($name) . '.lock';
}
}

114
vendor/yiisoft/yii2/mutex/Mutex.php vendored Normal file
View File

@@ -0,0 +1,114 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use yii\base\Component;
/**
* The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions".
*
* This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring
* a lock before accessing the corresponding data.
*
* Usage example:
*
* ```
* if ($mutex->acquire($mutexName)) {
* // business logic execution
* } else {
* // execution is blocked!
* }
* ```
*
* This is a base class, which should be extended in order to implement the actual lock mechanism.
*
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
abstract class Mutex extends Component
{
/**
* @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically
* before finishing script execution. Defaults to true. Setting this property to true means that all locks
* acquired in this process must be released (regardless of errors or exceptions).
*/
public $autoRelease = true;
/**
* @var string[] names of the locks acquired by the current PHP process.
*/
private $_locks = [];
/**
* Initializes the Mutex component.
*/
public function init()
{
if ($this->autoRelease) {
$locks = &$this->_locks;
register_shutdown_function(function () use (&$locks) {
foreach ($locks as $lock) {
$this->release($lock);
}
});
}
}
/**
* Acquires a lock by name.
* @param string $name of the lock to be acquired. Must be unique.
* @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return
* false immediately in case lock was already acquired.
* @return bool lock acquiring result.
*/
public function acquire($name, $timeout = 0)
{
if ($this->acquireLock($name, $timeout)) {
$this->_locks[] = $name;
return true;
}
return false;
}
/**
* Releases acquired lock. This method will return false in case the lock was not found.
* @param string $name of the lock to be released. This lock must already exist.
* @return bool lock release result: false in case named lock was not found..
*/
public function release($name)
{
if ($this->releaseLock($name)) {
$index = array_search($name, $this->_locks);
if ($index !== false) {
unset($this->_locks[$index]);
}
return true;
}
return false;
}
/**
* This method should be extended by a concrete Mutex implementations. Acquires lock by name.
* @param string $name of the lock to be acquired.
* @param int $timeout time (in seconds) to wait for the lock to be released.
* @return bool acquiring result.
*/
abstract protected function acquireLock($name, $timeout = 0);
/**
* This method should be extended by a concrete Mutex implementations. Releases lock by given name.
* @param string $name of the lock to be released.
* @return bool release result.
*/
abstract protected function releaseLock($name);
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use yii\base\InvalidConfigException;
/**
* MysqlMutex implements mutex "lock" mechanism via MySQL locks.
*
* Application configuration example:
*
* ```
* [
* 'components' => [
* 'db' => [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
* ]
* 'mutex' => [
* 'class' => 'yii\mutex\MysqlMutex',
* ],
* ],
* ]
* ```
*
* @see Mutex
*
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
class MysqlMutex extends DbMutex
{
/**
* Initializes MySQL specific mutex component implementation.
* @throws InvalidConfigException if [[db]] is not MySQL connection.
*/
public function init()
{
parent::init();
if ($this->db->driverName !== 'mysql') {
throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
}
}
/**
* Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param int $timeout time (in seconds) to wait for lock to become released.
* @return bool acquiring result.
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
*/
protected function acquireLock($name, $timeout = 0)
{
return $this->db->useMaster(function ($db) use ($name, $timeout) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT GET_LOCK(:name, :timeout)',
[':name' => $name, ':timeout' => $timeout]
)->queryScalar();
});
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return bool release result.
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
*/
protected function releaseLock($name)
{
return $this->db->useMaster(function ($db) use ($name) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT RELEASE_LOCK(:name)',
[':name' => $name]
)->queryScalar();
});
}
}

View File

@@ -0,0 +1,135 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use PDO;
use yii\base\InvalidConfigException;
/**
* OracleMutex implements mutex "lock" mechanism via Oracle locks.
*
* Application configuration example:
*
* ```
* [
* 'components' => [
* 'db' => [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'oci:dbname=LOCAL_XE',
* ...
* ]
* 'mutex' => [
* 'class' => 'yii\mutex\OracleMutex',
* 'lockMode' => 'NL_MODE',
* 'releaseOnCommit' => true,
* ...
* ],
* ],
* ]
* ```
*
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm
* @see Mutex
*
* @author Alexander Zlakomanov <zlakomanoff@gmail.com>
* @since 2.0.10
*/
class OracleMutex extends DbMutex
{
/** available lock modes */
const MODE_X = 'X_MODE';
const MODE_NL = 'NL_MODE';
const MODE_S = 'S_MODE';
const MODE_SX = 'SX_MODE';
const MODE_SS = 'SS_MODE';
const MODE_SSX = 'SSX_MODE';
/**
* @var string lock mode to be used.
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm#CHDBCFDI
*/
public $lockMode = self::MODE_X;
/**
* @var bool whether to release lock on commit.
*/
public $releaseOnCommit = false;
/**
* Initializes Oracle specific mutex component implementation.
* @throws InvalidConfigException if [[db]] is not Oracle connection.
*/
public function init()
{
parent::init();
if (strncmp($this->db->driverName, 'oci', 3) !== 0 && strncmp($this->db->driverName, 'odbc', 4) !== 0) {
throw new InvalidConfigException('In order to use OracleMutex connection must be configured to use Oracle database.');
}
}
/**
* Acquires lock by given name.
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm
* @param string $name of the lock to be acquired.
* @param int $timeout time (in seconds) to wait for lock to become released.
* @return bool acquiring result.
*/
protected function acquireLock($name, $timeout = 0)
{
$lockStatus = null;
// clean vars before using
$releaseOnCommit = $this->releaseOnCommit ? 'TRUE' : 'FALSE';
$timeout = abs((int) $timeout);
// inside pl/sql scopes pdo binding not working correctly :(
$this->db->useMaster(function ($db) use ($name, $timeout, $releaseOnCommit, &$lockStatus) {
/** @var \yii\db\Connection $db */
$db->createCommand(
'DECLARE
handle VARCHAR2(128);
BEGIN
DBMS_LOCK.ALLOCATE_UNIQUE(:name, handle);
:lockStatus := DBMS_LOCK.REQUEST(handle, DBMS_LOCK.' . $this->lockMode . ', ' . $timeout . ', ' . $releaseOnCommit . ');
END;',
[':name' => $name]
)
->bindParam(':lockStatus', $lockStatus, PDO::PARAM_INT, 1)
->execute();
});
return $lockStatus === 0 || $lockStatus === '0';
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return bool release result.
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm
*/
protected function releaseLock($name)
{
$releaseStatus = null;
$this->db->useMaster(function ($db) use ($name, &$releaseStatus) {
/** @var \yii\db\Connection $db */
$db->createCommand(
'DECLARE
handle VARCHAR2(128);
BEGIN
DBMS_LOCK.ALLOCATE_UNIQUE(:name, handle);
:result := DBMS_LOCK.RELEASE(handle);
END;',
[':name' => $name]
)
->bindParam(':result', $releaseStatus, PDO::PARAM_INT, 1)
->execute();
});
return $releaseStatus === 0 || $releaseStatus === '0';
}
}

100
vendor/yiisoft/yii2/mutex/PgsqlMutex.php vendored Normal file
View File

@@ -0,0 +1,100 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
/**
* PgsqlMutex implements mutex "lock" mechanism via PgSQL locks.
*
* Application configuration example:
*
* ```
* [
* 'components' => [
* 'db' => [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'pgsql:host=127.0.0.1;dbname=demo',
* ]
* 'mutex' => [
* 'class' => 'yii\mutex\PgsqlMutex',
* ],
* ],
* ]
* ```
*
* @see Mutex
*
* @author nineinchnick <janek.jan@gmail.com>
* @since 2.0.8
*/
class PgsqlMutex extends DbMutex
{
/**
* Initializes PgSQL specific mutex component implementation.
* @throws InvalidConfigException if [[db]] is not PgSQL connection.
*/
public function init()
{
parent::init();
if ($this->db->driverName !== 'pgsql') {
throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.');
}
}
/**
* Converts a string into two 16 bit integer keys using the SHA1 hash function.
* @param string $name
* @return array contains two 16 bit integer keys
*/
private function getKeysFromName($name)
{
return array_values(unpack('n2', sha1($name, true)));
}
/**
* Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param int $timeout time (in seconds) to wait for lock to become released.
* @return bool acquiring result.
* @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
*/
protected function acquireLock($name, $timeout = 0)
{
if ($timeout !== 0) {
throw new InvalidArgumentException('PgsqlMutex does not support timeout.');
}
list($key1, $key2) = $this->getKeysFromName($name);
return $this->db->useMaster(function ($db) use ($key1, $key2) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT pg_try_advisory_lock(:key1, :key2)',
[':key1' => $key1, ':key2' => $key2]
)->queryScalar();
});
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return bool release result.
* @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
*/
protected function releaseLock($name)
{
list($key1, $key2) = $this->getKeysFromName($name);
return $this->db->useMaster(function ($db) use ($key1, $key2) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT pg_advisory_unlock(:key1, :key2)',
[':key1' => $key1, ':key2' => $key2]
)->queryScalar();
});
}
}