DbMutex.php 1.05 KB
Newer Older
resurtm committed
1
<?php
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
resurtm committed
7

resurtm committed
8
namespace yii\mutex;
resurtm committed
9 10 11 12 13

use Yii;
use yii\db\Connection;
use yii\base\InvalidConfigException;

14 15 16 17
/**
 * @author resurtm <resurtm@gmail.com>
 * @since 2.0
 */
resurtm committed
18
abstract class DbMutex extends Mutex
resurtm committed
19 20
{
	/**
21 22 23
	 * @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.
resurtm committed
24 25 26
	 */
	public $db = 'db';

27 28 29 30
	/**
	 * Initializes generic database table based mutex implementation.
	 * @throws InvalidConfigException if [[db]] is invalid.
	 */
resurtm committed
31 32 33
	public function init()
	{
		parent::init();
34 35 36
		if (is_string($this->db)) {
			$this->db = Yii::$app->getComponent($this->db);
		}
resurtm committed
37
		if (!$this->db instanceof Connection) {
38
			throw new InvalidConfigException('Mutex::db must be either a DB connection instance or the application component ID of a DB connection.');
resurtm committed
39 40 41
		}
	}
}