Created
May 11, 2012 13:19
-
-
Save zircote/2659562 to your computer and use it in GitHub Desktop.
A Mongo Zend Application Resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* @category Zend | |
* @package Zend_Application | |
* @subpackage Resource | |
*/ | |
/** | |
* @see Zend_Application_Resource_ResourceAbstract | |
*/ | |
require_once 'Zend/Application/Resource/ResourceAbstract.php'; | |
/** | |
* Resource for initializing a Mongo Connection | |
* | |
* <code> | |
* ;; See for options: http://us.php.net/manual/en/mongo.construct.php | |
* resources.mongo.server = "mongodb://localhost:27017" | |
* resources.mongo.options.replicaSet = "rs01" | |
* resources.mongo.options.username = "user1" | |
* resources.mongo.options.password = "sekret" | |
* resources.mongo.options.connect = true | |
* resources.mongo.options.database = "auth" | |
* resources.mongo.options.timeout = 10 | |
* </code> | |
* | |
* @uses Zend_Application_Resource_ResourceAbstract | |
* @category Zend | |
* @package Zend_Application | |
*/ | |
class Zend_Application_Resource_Mongo | |
extends Zend_Application_Resource_ResourceAbstract | |
{ | |
/** | |
* | |
* @var Mongo | |
*/ | |
protected $_mongo; | |
public function init() | |
{ | |
$this->getMongo(); | |
} | |
/** | |
* Attach logger | |
* | |
* @param Mongo $mongo | |
* @return Zend_Application_Resource_Mongo | |
*/ | |
public function setMongo(Mongo $mongo) | |
{ | |
$this->_mongo = $mongo; | |
return $this; | |
} | |
/** | |
* @return Mongo | |
*/ | |
public function getMongo() | |
{ | |
if (!$this->_mongo instanceof Mongo) { | |
if(!extension_loaded('mongo')){ | |
throw new RuntimeException('mongo extension is not loaded'); | |
} | |
$config = $this->getOptions(); | |
if(!isset($config['server'])){ | |
$server = "mongodb://localhost:27017"; | |
} else { | |
$server = $config['server']; | |
} | |
if(!isset($config['options']) || !is_array($config['options'])){ | |
$options = array(); | |
} else { | |
$options = $config['options']; | |
} | |
$mongo = new Mongo($server, $options); | |
$this->setMongo($mongo); | |
} | |
return $this->_mongo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment