From a15c70f414c67ca0bc46fc8148af988432d594ea Mon Sep 17 00:00:00 2001 From: resurtm Date: Mon, 24 Jun 2013 23:42:11 +0600 Subject: [PATCH] composer.json file and proper class files location. --- extensions/mutex/Mutex.php | 140 -------------------------------- extensions/mutex/composer.json | 27 ++++++ extensions/mutex/db/Mutex.php | 51 ------------ extensions/mutex/db/mssql/Mutex.php | 56 ------------- extensions/mutex/db/mysql/Mutex.php | 72 ---------------- extensions/mutex/unix/Mutex.php | 99 ---------------------- extensions/mutex/yii/Mutex.php | 140 ++++++++++++++++++++++++++++++++ extensions/mutex/yii/db/Mutex.php | 51 ++++++++++++ extensions/mutex/yii/db/mssql/Mutex.php | 56 +++++++++++++ extensions/mutex/yii/db/mysql/Mutex.php | 72 ++++++++++++++++ extensions/mutex/yii/unix/Mutex.php | 99 ++++++++++++++++++++++ 11 files changed, 445 insertions(+), 418 deletions(-) delete mode 100644 extensions/mutex/Mutex.php create mode 100644 extensions/mutex/composer.json delete mode 100644 extensions/mutex/db/Mutex.php delete mode 100644 extensions/mutex/db/mssql/Mutex.php delete mode 100644 extensions/mutex/db/mysql/Mutex.php delete mode 100644 extensions/mutex/unix/Mutex.php create mode 100644 extensions/mutex/yii/Mutex.php create mode 100644 extensions/mutex/yii/db/Mutex.php create mode 100644 extensions/mutex/yii/db/mssql/Mutex.php create mode 100644 extensions/mutex/yii/db/mysql/Mutex.php create mode 100644 extensions/mutex/yii/unix/Mutex.php diff --git a/extensions/mutex/Mutex.php b/extensions/mutex/Mutex.php deleted file mode 100644 index 5268d86..0000000 --- a/extensions/mutex/Mutex.php +++ /dev/null @@ -1,140 +0,0 @@ - - * @since 2.0 - */ -abstract class Mutex extends Component -{ - /** - * @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically - * before finishing script execution. Defaults to true. Setting this property to true - */ - public $autoRelease = true; - /** - * @var string[] names of the locks acquired in the current PHP process. - */ - private $_locks = array(); - - - /** - * Initializes the mutex component. - */ - public function init() - { - if ($this->autoRelease) { - register_shutdown_function(array($this, 'shutdownFunction')); - } - } - - /** - * Never call this method directly under any circumstances. This method is intended for internal use only. - */ - public function shutdownFunction() - { - foreach ($this->_locks as $lock) { - $this->release($lock); - } - } - - /** - * @param string $name of the lock to be acquired. Must be unique. - * @param integer $timeout to wait for lock to be released. Defaults to zero meaning that method will return - * false immediately in case lock was already acquired. - * @return boolean lock acquiring result. - */ - public function acquireLock($name, $timeout = 0) - { - if ($this->acquire($name, $timeout)) { - $this->_locks[] = $name; - return true; - } else { - return false; - } - } - - /** - * Release acquired lock. - * @param string $name of the lock to be released. This lock must be already created. - * @return boolean lock release result. - */ - public function releaseLock($name) - { - if ($this->release($name)) { - unset($this->_locks[array_search($name, $this->_locks)]); - return true; - } else { - return false; - } - } - - /** - * Checks whether named lock was already opened. - * @param string $name of the lock to be checked. This lock must be already created. - * @return boolean|null whether named lock was already opened. Returns `null` value in case concrete - * mutex implementation does not support this operation. - */ - public function getIsLockAcquired($name) - { - if (in_array($name, $this->_locks)) { - return true; - } else { - return $this->getIsAcquired($name); - } - } - - /** - * Checks whether given lock is local. In other words local lock means that it was opened in the current - * PHP process. - * @param string $name of the lock to be checked. This lock must be already created. - * @return boolean whether named lock was locally acquired. - */ - public function getIsLockLocal($name) - { - return in_array($name, $this->_locks); - } - - /** - * This method should be extended by concrete mutex implementations. Acquires lock by given name. - * @param string $name of the lock to be acquired. - * @param integer $timeout to wait for lock to become released. - * @return boolean acquiring result. - */ - abstract protected function acquire($name, $timeout = 0); - - /** - * This method should be extended by concrete mutex implementations. Releases lock by given name. - * @param string $name of the lock to be released. - * @return boolean release result. - */ - abstract protected function release($name); - - /** - * This method may optionally be extended by concrete mutex implementations. Checks whether lock has been - * already acquired by given name. - * @param string $name of the lock to be released. - * @return null|boolean whether lock has been already acquired. Returns `null` in case this feature - * is not supported by concrete mutex implementation. - */ - protected function getIsAcquired($name) - { - return null; - } - - /** - * This method should be extended by concrete mutex implementations. Returns whether current mutex - * implementation can be used in a distributed environment. - * @return boolean whether current mutex implementation can be used in a distributed environment. - */ - abstract public function getIsDistributed(); -} diff --git a/extensions/mutex/composer.json b/extensions/mutex/composer.json new file mode 100644 index 0000000..4499543 --- /dev/null +++ b/extensions/mutex/composer.json @@ -0,0 +1,27 @@ +{ + "name": "yiisoft/yii2-mutex", + "description": "Mutual exclusion extension for the Yii framework", + "keywords": ["yii", "mutex"], + "type": "library", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?state=open", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "resurtm", + "email": "resurtm@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-0": { "yii\\smarty": "" } + } +} diff --git a/extensions/mutex/db/Mutex.php b/extensions/mutex/db/Mutex.php deleted file mode 100644 index 17dd68a..0000000 --- a/extensions/mutex/db/Mutex.php +++ /dev/null @@ -1,51 +0,0 @@ - - * @since 2.0 - */ -abstract class Mutex extends \yii\mutex\Mutex -{ - /** - * @var Connection|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. - */ - public $db = 'db'; - - /** - * Initializes generic database table based mutex implementation. - * @throws InvalidConfigException if [[db]] is invalid. - */ - public function init() - { - parent::init(); - if (is_string($this->db)) { - $this->db = Yii::$app->getComponent($this->db); - } - if (!$this->db instanceof Connection) { - throw new InvalidConfigException('Mutex::db must be either a DB connection instance or the application component ID of a DB connection.'); - } - } - - /** - * This method should be extended by concrete mutex implementations. Returns whether current mutex - * implementation can be used in a distributed environment. - * @return boolean whether current mutex implementation can be used in a distributed environment. - */ - public function getIsDistributed() - { - return true; - } -} diff --git a/extensions/mutex/db/mssql/Mutex.php b/extensions/mutex/db/mssql/Mutex.php deleted file mode 100644 index 374100a..0000000 --- a/extensions/mutex/db/mssql/Mutex.php +++ /dev/null @@ -1,56 +0,0 @@ - - * @since 2.0 - */ -class Mutex extends \yii\mutex\db\Mutex -{ - /** - * Initializes Microsoft SQL Server specific mutex component implementation. - * @throws InvalidConfigException if [[db]] is not Microsoft SQL Server connection. - */ - public function init() - { - parent::init(); - $driverName = $this->db->driverName; - if ($driverName !== 'sqlsrv' && $driverName !== 'dblib' && $driverName !== 'mssql') { - throw new InvalidConfigException(''); - } - } - - /** - * This method should be extended by concrete mutex implementations. Acquires lock by given name. - * @param string $name of the lock to be acquired. - * @param integer $timeout to wait for lock to become released. - * @return boolean acquiring result. - * @throws \BadMethodCallException - * @see http://msdn.microsoft.com/en-us/library/ms189823.aspx - */ - protected function acquire($name, $timeout = 0) - { - throw new \BadMethodCallException('Not implemented yet.'); - } - - /** - * This method should be extended by concrete mutex implementations. Releases lock by given name. - * @param string $name of the lock to be released. - * @return boolean release result. - * @throws \BadMethodCallException - * @see http://msdn.microsoft.com/en-us/library/ms178602.aspx - */ - protected function release($name) - { - throw new \BadMethodCallException('Not implemented yet.'); - } -} diff --git a/extensions/mutex/db/mysql/Mutex.php b/extensions/mutex/db/mysql/Mutex.php deleted file mode 100644 index 7153d38..0000000 --- a/extensions/mutex/db/mysql/Mutex.php +++ /dev/null @@ -1,72 +0,0 @@ - - * @since 2.0 - */ -class Mutex extends \yii\mutex\db\Mutex -{ - /** - * 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(''); - } - } - - /** - * This method should be extended by concrete mutex implementations. Acquires lock by given name. - * @param string $name of the lock to be acquired. - * @param integer $timeout to wait for lock to become released. - * @return boolean acquiring result. - * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock - */ - protected function acquire($name, $timeout = 0) - { - return (boolean)$this->db - ->createCommand('SELECT GET_LOCK(:name, :timeout)', array(':name' => $name, ':timeout' => $timeout)) - ->queryScalar(); - } - - /** - * This method should be extended by concrete mutex implementations. Releases lock by given name. - * @param string $name of the lock to be released. - * @return boolean release result. - * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock - */ - protected function release($name) - { - return (boolean)$this->db - ->createCommand('SELECT RELEASE_LOCK(:name)', array(':name' => $name)) - ->queryScalar(); - } - - /** - * This method may optionally be extended by concrete mutex implementations. Checks whether lock has been - * already acquired by given name. - * @param string $name of the lock to be released. - * @return null|boolean whether lock has been already acquired. Returns `null` in case this feature - * is not supported by concrete mutex implementation. - * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_is-free-lock - */ - protected function getIsAcquired($name) - { - return (boolean)$this->db - ->createCommand('SELECT IS_FREE_LOCK(:name)', array(':name' => $name)) - ->queryScalar(); - } -} diff --git a/extensions/mutex/unix/Mutex.php b/extensions/mutex/unix/Mutex.php deleted file mode 100644 index ca18a49..0000000 --- a/extensions/mutex/unix/Mutex.php +++ /dev/null @@ -1,99 +0,0 @@ - - * @since 2.0 - */ -class Mutex extends \yii\mutex\Mutex -{ - /** - * @var resource[] stores all opened lock files. Keys are lock names and values are file handles. - */ - private $_files = array(); - - - /** - * Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like - * operating systems. - * @throws InvalidConfigException - */ - public function init() - { - if (stripos(php_uname('s'), 'win') === 0) { - throw new InvalidConfigException(''); - } - } - - /** - * This method should be extended by concrete mutex implementations. Acquires lock by given name. - * @param string $name of the lock to be acquired. - * @param integer $timeout to wait for lock to become released. - * @return boolean acquiring result. - */ - protected function acquire($name, $timeout = 0) - { - $file = fopen(Yii::$app->getRuntimePath() . '/mutex.' . md5($name) . '.lock', 'w+'); - if ($file === false) { - return false; - } - $waitTime = 0; - while (!flock($file, LOCK_EX | LOCK_NB)) { - $waitTime++; - if ($waitTime > $timeout) { - fclose($file); - return false; - } - sleep(1); - } - $this->_files[$name] = $file; - return true; - } - - /** - * This method should be extended by concrete mutex implementations. Releases lock by given name. - * @param string $name of the lock to be released. - * @return boolean release result. - */ - protected function release($name) - { - if (!isset($this->_files[$name]) || !flock($this->_files[$name], LOCK_UN)) { - return false; - } else { - fclose($this->_files[$name]); - unset($this->_files[$name]); - return true; - } - } - - /** - * This method may optionally be extended by concrete mutex implementations. Checks whether lock has been - * already acquired by given name. - * @param string $name of the lock to be released. - * @return null|boolean whether lock has been already acquired. Returns `null` in case this feature - * is not supported by concrete mutex implementation. - */ - protected function getIsAcquired($name) - { - return false; - } - - /** - * This method should be extended by concrete mutex implementations. Returns whether current mutex - * implementation can be used in a distributed environment. - * @return boolean whether current mutex implementation can be used in a distributed environment. - */ - public function getIsDistributed() - { - return false; - } -} diff --git a/extensions/mutex/yii/Mutex.php b/extensions/mutex/yii/Mutex.php new file mode 100644 index 0000000..5268d86 --- /dev/null +++ b/extensions/mutex/yii/Mutex.php @@ -0,0 +1,140 @@ + + * @since 2.0 + */ +abstract class Mutex extends Component +{ + /** + * @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically + * before finishing script execution. Defaults to true. Setting this property to true + */ + public $autoRelease = true; + /** + * @var string[] names of the locks acquired in the current PHP process. + */ + private $_locks = array(); + + + /** + * Initializes the mutex component. + */ + public function init() + { + if ($this->autoRelease) { + register_shutdown_function(array($this, 'shutdownFunction')); + } + } + + /** + * Never call this method directly under any circumstances. This method is intended for internal use only. + */ + public function shutdownFunction() + { + foreach ($this->_locks as $lock) { + $this->release($lock); + } + } + + /** + * @param string $name of the lock to be acquired. Must be unique. + * @param integer $timeout to wait for lock to be released. Defaults to zero meaning that method will return + * false immediately in case lock was already acquired. + * @return boolean lock acquiring result. + */ + public function acquireLock($name, $timeout = 0) + { + if ($this->acquire($name, $timeout)) { + $this->_locks[] = $name; + return true; + } else { + return false; + } + } + + /** + * Release acquired lock. + * @param string $name of the lock to be released. This lock must be already created. + * @return boolean lock release result. + */ + public function releaseLock($name) + { + if ($this->release($name)) { + unset($this->_locks[array_search($name, $this->_locks)]); + return true; + } else { + return false; + } + } + + /** + * Checks whether named lock was already opened. + * @param string $name of the lock to be checked. This lock must be already created. + * @return boolean|null whether named lock was already opened. Returns `null` value in case concrete + * mutex implementation does not support this operation. + */ + public function getIsLockAcquired($name) + { + if (in_array($name, $this->_locks)) { + return true; + } else { + return $this->getIsAcquired($name); + } + } + + /** + * Checks whether given lock is local. In other words local lock means that it was opened in the current + * PHP process. + * @param string $name of the lock to be checked. This lock must be already created. + * @return boolean whether named lock was locally acquired. + */ + public function getIsLockLocal($name) + { + return in_array($name, $this->_locks); + } + + /** + * This method should be extended by concrete mutex implementations. Acquires lock by given name. + * @param string $name of the lock to be acquired. + * @param integer $timeout to wait for lock to become released. + * @return boolean acquiring result. + */ + abstract protected function acquire($name, $timeout = 0); + + /** + * This method should be extended by concrete mutex implementations. Releases lock by given name. + * @param string $name of the lock to be released. + * @return boolean release result. + */ + abstract protected function release($name); + + /** + * This method may optionally be extended by concrete mutex implementations. Checks whether lock has been + * already acquired by given name. + * @param string $name of the lock to be released. + * @return null|boolean whether lock has been already acquired. Returns `null` in case this feature + * is not supported by concrete mutex implementation. + */ + protected function getIsAcquired($name) + { + return null; + } + + /** + * This method should be extended by concrete mutex implementations. Returns whether current mutex + * implementation can be used in a distributed environment. + * @return boolean whether current mutex implementation can be used in a distributed environment. + */ + abstract public function getIsDistributed(); +} diff --git a/extensions/mutex/yii/db/Mutex.php b/extensions/mutex/yii/db/Mutex.php new file mode 100644 index 0000000..17dd68a --- /dev/null +++ b/extensions/mutex/yii/db/Mutex.php @@ -0,0 +1,51 @@ + + * @since 2.0 + */ +abstract class Mutex extends \yii\mutex\Mutex +{ + /** + * @var Connection|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. + */ + public $db = 'db'; + + /** + * Initializes generic database table based mutex implementation. + * @throws InvalidConfigException if [[db]] is invalid. + */ + public function init() + { + parent::init(); + if (is_string($this->db)) { + $this->db = Yii::$app->getComponent($this->db); + } + if (!$this->db instanceof Connection) { + throw new InvalidConfigException('Mutex::db must be either a DB connection instance or the application component ID of a DB connection.'); + } + } + + /** + * This method should be extended by concrete mutex implementations. Returns whether current mutex + * implementation can be used in a distributed environment. + * @return boolean whether current mutex implementation can be used in a distributed environment. + */ + public function getIsDistributed() + { + return true; + } +} diff --git a/extensions/mutex/yii/db/mssql/Mutex.php b/extensions/mutex/yii/db/mssql/Mutex.php new file mode 100644 index 0000000..374100a --- /dev/null +++ b/extensions/mutex/yii/db/mssql/Mutex.php @@ -0,0 +1,56 @@ + + * @since 2.0 + */ +class Mutex extends \yii\mutex\db\Mutex +{ + /** + * Initializes Microsoft SQL Server specific mutex component implementation. + * @throws InvalidConfigException if [[db]] is not Microsoft SQL Server connection. + */ + public function init() + { + parent::init(); + $driverName = $this->db->driverName; + if ($driverName !== 'sqlsrv' && $driverName !== 'dblib' && $driverName !== 'mssql') { + throw new InvalidConfigException(''); + } + } + + /** + * This method should be extended by concrete mutex implementations. Acquires lock by given name. + * @param string $name of the lock to be acquired. + * @param integer $timeout to wait for lock to become released. + * @return boolean acquiring result. + * @throws \BadMethodCallException + * @see http://msdn.microsoft.com/en-us/library/ms189823.aspx + */ + protected function acquire($name, $timeout = 0) + { + throw new \BadMethodCallException('Not implemented yet.'); + } + + /** + * This method should be extended by concrete mutex implementations. Releases lock by given name. + * @param string $name of the lock to be released. + * @return boolean release result. + * @throws \BadMethodCallException + * @see http://msdn.microsoft.com/en-us/library/ms178602.aspx + */ + protected function release($name) + { + throw new \BadMethodCallException('Not implemented yet.'); + } +} diff --git a/extensions/mutex/yii/db/mysql/Mutex.php b/extensions/mutex/yii/db/mysql/Mutex.php new file mode 100644 index 0000000..7153d38 --- /dev/null +++ b/extensions/mutex/yii/db/mysql/Mutex.php @@ -0,0 +1,72 @@ + + * @since 2.0 + */ +class Mutex extends \yii\mutex\db\Mutex +{ + /** + * 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(''); + } + } + + /** + * This method should be extended by concrete mutex implementations. Acquires lock by given name. + * @param string $name of the lock to be acquired. + * @param integer $timeout to wait for lock to become released. + * @return boolean acquiring result. + * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock + */ + protected function acquire($name, $timeout = 0) + { + return (boolean)$this->db + ->createCommand('SELECT GET_LOCK(:name, :timeout)', array(':name' => $name, ':timeout' => $timeout)) + ->queryScalar(); + } + + /** + * This method should be extended by concrete mutex implementations. Releases lock by given name. + * @param string $name of the lock to be released. + * @return boolean release result. + * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock + */ + protected function release($name) + { + return (boolean)$this->db + ->createCommand('SELECT RELEASE_LOCK(:name)', array(':name' => $name)) + ->queryScalar(); + } + + /** + * This method may optionally be extended by concrete mutex implementations. Checks whether lock has been + * already acquired by given name. + * @param string $name of the lock to be released. + * @return null|boolean whether lock has been already acquired. Returns `null` in case this feature + * is not supported by concrete mutex implementation. + * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_is-free-lock + */ + protected function getIsAcquired($name) + { + return (boolean)$this->db + ->createCommand('SELECT IS_FREE_LOCK(:name)', array(':name' => $name)) + ->queryScalar(); + } +} diff --git a/extensions/mutex/yii/unix/Mutex.php b/extensions/mutex/yii/unix/Mutex.php new file mode 100644 index 0000000..ca18a49 --- /dev/null +++ b/extensions/mutex/yii/unix/Mutex.php @@ -0,0 +1,99 @@ + + * @since 2.0 + */ +class Mutex extends \yii\mutex\Mutex +{ + /** + * @var resource[] stores all opened lock files. Keys are lock names and values are file handles. + */ + private $_files = array(); + + + /** + * Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like + * operating systems. + * @throws InvalidConfigException + */ + public function init() + { + if (stripos(php_uname('s'), 'win') === 0) { + throw new InvalidConfigException(''); + } + } + + /** + * This method should be extended by concrete mutex implementations. Acquires lock by given name. + * @param string $name of the lock to be acquired. + * @param integer $timeout to wait for lock to become released. + * @return boolean acquiring result. + */ + protected function acquire($name, $timeout = 0) + { + $file = fopen(Yii::$app->getRuntimePath() . '/mutex.' . md5($name) . '.lock', 'w+'); + if ($file === false) { + return false; + } + $waitTime = 0; + while (!flock($file, LOCK_EX | LOCK_NB)) { + $waitTime++; + if ($waitTime > $timeout) { + fclose($file); + return false; + } + sleep(1); + } + $this->_files[$name] = $file; + return true; + } + + /** + * This method should be extended by concrete mutex implementations. Releases lock by given name. + * @param string $name of the lock to be released. + * @return boolean release result. + */ + protected function release($name) + { + if (!isset($this->_files[$name]) || !flock($this->_files[$name], LOCK_UN)) { + return false; + } else { + fclose($this->_files[$name]); + unset($this->_files[$name]); + return true; + } + } + + /** + * This method may optionally be extended by concrete mutex implementations. Checks whether lock has been + * already acquired by given name. + * @param string $name of the lock to be released. + * @return null|boolean whether lock has been already acquired. Returns `null` in case this feature + * is not supported by concrete mutex implementation. + */ + protected function getIsAcquired($name) + { + return false; + } + + /** + * This method should be extended by concrete mutex implementations. Returns whether current mutex + * implementation can be used in a distributed environment. + * @return boolean whether current mutex implementation can be used in a distributed environment. + */ + public function getIsDistributed() + { + return false; + } +}