Connection.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * xunsearch Connection class file
  4. *
  5. * @author hightman
  6. * @link http://www.xunsearch.com/
  7. * @copyright Copyright &copy; 2014 HangZhou YunSheng Network Technology Co., Ltd.
  8. * @license http://www.xunsearch.com/license/
  9. * @version $Id$
  10. */
  11. namespace hightman\xunsearch;
  12. use Yii;
  13. use yii\base\Component;
  14. /**
  15. * xunsearch Connection is used to manage XS objects
  16. *
  17. * @property-read string $version the version of xunsearch sdk.
  18. *
  19. * @author xjflyttp <xjflyttp@gmail.com>
  20. * @author hightman <hightman@twomice.net>
  21. * @since 1.4.9
  22. */
  23. class Connection extends Component
  24. {
  25. /**
  26. * @event Event an event that is triggered before a XS instance is created
  27. */
  28. const EVENT_BEFORE_OPEN = 'beforeOpen';
  29. /**
  30. * @var string xunsearch ini file directory
  31. * Alias supportted, i.e: '@app/config' or '/path/to/config'
  32. * Default to: @vendor/hightman/xunsearch/app
  33. */
  34. public $iniDirectory = '@vendor/hightman/xunsearch/app';
  35. /**
  36. * @var string charset of current app, deafult to utf-8
  37. */
  38. public $charset = 'utf-8';
  39. /**
  40. * @var Database[]
  41. */
  42. private $_databases = [];
  43. /**
  44. * Initializes the object
  45. */
  46. public function init()
  47. {
  48. parent::init();
  49. if (substr($this->iniDirectory, 0, 1) === '@') {
  50. $this->iniDirectory = Yii::getAlias($this->iniDirectory);
  51. }
  52. }
  53. /**
  54. * Get database via calling object self
  55. * @param string $name database name
  56. * @return Database
  57. */
  58. public function __invoke($name)
  59. {
  60. return $this->getDatabase($name);
  61. }
  62. /**
  63. * @return string sdk version
  64. */
  65. public function getVersion()
  66. {
  67. if (!defined('XS_PACKAGE_VERSION')) {
  68. new \XSException('');
  69. }
  70. return XS_PACKAGE_VERSION;
  71. }
  72. /**
  73. * Get database
  74. * @param string $name database name
  75. * @param boolean $refresh whether to reestablish the database connection even if it is found in the cache.
  76. * @return Database
  77. * @throws \XSException
  78. */
  79. public function getDatabase($name, $refresh = false)
  80. {
  81. if ($refresh || !array_key_exists($name, $this->_databases)) {
  82. $this->_databases[$name] = $this->openDatabase($name);
  83. }
  84. return $this->_databases[$name];
  85. }
  86. /**
  87. * Open database
  88. * @param string $name database name.
  89. * @return Database
  90. * @throws \XSException
  91. */
  92. protected function openDatabase($name)
  93. {
  94. $this->trigger(self::EVENT_BEFORE_OPEN);
  95. $iniFile = $this->iniDirectory . '/' . $name . '.ini';
  96. return Yii::createObject([
  97. 'class' => Database::className(),
  98. 'charset' => $this->charset,
  99. 'iniFile' => $iniFile,
  100. ]);
  101. }
  102. }