18c9054f0b58ffb6f7e9f279dfaf443bb6ffd598.svn-base 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php namespace Maze\Data\Mongo;
  2. use Maze\Debug\Process as Debug;
  3. class Connect
  4. {
  5. /**
  6. * handle
  7. *
  8. * @var object
  9. */
  10. private $handle;
  11. /**
  12. * instance
  13. *
  14. * @var string
  15. */
  16. static protected $instance;
  17. /**
  18. * getInstance
  19. *
  20. * @return Maze\Data\Mongo\Connect;
  21. */
  22. static public function getInstance($config)
  23. {
  24. $key = $config['host'] . $config['database'];
  25. if(empty(self::$instance[$key]))
  26. {
  27. self::$instance[$key] = new self();
  28. self::$instance[$key]->init($config);
  29. }
  30. return self::$instance[$key];
  31. }
  32. /**
  33. * init
  34. *
  35. * @return mixd
  36. */
  37. private function init($config)
  38. {
  39. if(strpos($config['host'], ':') !== false) list($config['host'], $config['port']) = explode(':', $config['host']);
  40. try
  41. {
  42. $mongo = new \Mongo('mongodb://'.$config['host'].':'.$config['port'], array("connectTimeoutMS"=>1000));
  43. $this->handle = $mongo->selectDB($config['database']);
  44. Debug::log('mongodb ' . $config['host'] . ' connected');
  45. }
  46. catch(\PDOException $e)
  47. {
  48. echo $e->getMessage();die;
  49. }
  50. }
  51. /**
  52. * __destruct
  53. *
  54. * @return mixd
  55. */
  56. public function __destruct()
  57. {
  58. $this->close();
  59. }
  60. /**
  61. * table
  62. *
  63. * @return mixd
  64. */
  65. public function table($table)
  66. {
  67. return $this->handle->selectCollection($table);
  68. }
  69. /**
  70. * handle
  71. *
  72. * @return object
  73. */
  74. public function handle()
  75. {
  76. return $this->handle;
  77. }
  78. /**
  79. * close
  80. *
  81. * @return mixd
  82. */
  83. public function close()
  84. {
  85. $this->handle = null;
  86. }
  87. }