DbMutex.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\mutex;
  8. use Yii;
  9. use yii\db\Connection;
  10. use yii\base\InvalidConfigException;
  11. /**
  12. * @author resurtm <[email protected]>
  13. * @since 2.0
  14. */
  15. abstract class DbMutex extends Mutex
  16. {
  17. /**
  18. * @var Connection|string the DB connection object or the application component ID of the DB connection.
  19. * After the Mutex object is created, if you want to change this property, you should only assign
  20. * it with a DB connection object.
  21. */
  22. public $db = 'db';
  23. /**
  24. * Initializes generic database table based mutex implementation.
  25. * @throws InvalidConfigException if [[db]] is invalid.
  26. */
  27. public function init()
  28. {
  29. parent::init();
  30. if (is_string($this->db)) {
  31. $this->db = Yii::$app->getComponent($this->db);
  32. }
  33. if (!$this->db instanceof Connection) {
  34. throw new InvalidConfigException('Mutex::db must be either a DB connection instance or the application component ID of a DB connection.');
  35. }
  36. }
  37. }