123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php namespace Maze\Data\Mongo;
- use Maze\Debug\Process as Debug;
- class Connect
- {
- /**
- * handle
- *
- * @var object
- */
- private $handle;
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * getInstance
- *
- * @return Maze\Data\Mongo\Connect;
- */
- static public function getInstance($config)
- {
- $key = $config['host'] . $config['database'];
- if(empty(self::$instance[$key]))
- {
- self::$instance[$key] = new self();
- self::$instance[$key]->init($config);
- }
- return self::$instance[$key];
- }
-
- /**
- * init
- *
- * @return mixd
- */
- private function init($config)
- {
- if(strpos($config['host'], ':') !== false) list($config['host'], $config['port']) = explode(':', $config['host']);
- try
- {
- $mongo = new \Mongo('mongodb://'.$config['host'].':'.$config['port'], array("connectTimeoutMS"=>1000));
-
- $this->handle = $mongo->selectDB($config['database']);
- Debug::log('mongodb ' . $config['host'] . ' connected');
- }
- catch(\PDOException $e)
- {
- echo $e->getMessage();die;
- }
- }
- /**
- * __destruct
- *
- * @return mixd
- */
- public function __destruct()
- {
- $this->close();
- }
-
- /**
- * table
- *
- * @return mixd
- */
- public function table($table)
- {
- return $this->handle->selectCollection($table);
- }
- /**
- * handle
- *
- * @return object
- */
- public function handle()
- {
- return $this->handle;
- }
- /**
- * close
- *
- * @return mixd
- */
- public function close()
- {
- $this->handle = null;
- }
- }
|