Skip to content

Commit

Permalink
Auth support and replica sets almost ready, better configuration for …
Browse files Browse the repository at this point in the history
…connections (requires PHP 5.3+)
  • Loading branch information
gatesvp committed Jun 21, 2011
1 parent 14633b9 commit 3516c77
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 90 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
MongoModel
----------

MongoModel is a simple set of object wrappers classes for storing PHP objects in MongoDB.

MongoModel is comprised of two major classes (in the lib directory).
* MongoEntity: the base class with all of the CRUD operations from which other classes will inherit.
* MongoFactory: a simple factory class for loading an array of MongoEntity objects. Can be inherited to provide a specific factory for you specific needs.

Basic Usage
===========

class User extends MongoEntity {

protected static $_mongo_database = "users"; # Effectively the database name
protected static $_mongo_collection = "user"; # Effectively the table name

}

This allows you to store objects in the 'users' database and the 'user' collection.

$user = new User();

$user_data = array("first_name" => "Gates", "last_name" => "VP");
$user = new User($user_data);
if($user->save()){
print("saved the user");
}

Shorten field names
===================
....

More details to come.

103 changes: 77 additions & 26 deletions lib/MongoEntity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class MongoEntity {
protected static $_mongo_collection = "test"; # Effectively the table name
protected static $_mongo_connection_timeout = 5000; # 5 second default timeout on connect
protected static $_mongo_query_timeout = 1000;
protected static $_is_replica_set = false;
protected static $_mongo_is_replica_set = false;
protected static $_mongo_user_name = '';
protected static $_mongo_password = '';

protected $_id;
protected $_data = array();
Expand Down Expand Up @@ -58,63 +60,93 @@ private function _set_data($data = array()){
$this->_data = $data;
}

protected static function getConnectionString($server,$port) {
protected static function getConnectionString($server, $port, $userName, $password) {

$auth_info = '';
$db_info = '';
$server_port = '';
if($userName != '' && $password != ''){
$auth_info = "$userName:$password";
$db_info = static::$_mongo_database;
}

if(is_array($server) && is_array($port) && count($server) == count($port)){
$connections = array();
for($i = 0; $i < count($server); $i++){
$connections[] = $server[$i].":".$port[$i];
}
return join(",", $connections);
$server_port = join(",", $connections);
}
elseif(!is_array($server) && !is_array($port)){
return "$server:$port";
$server_port = "$server:$port";
}
else{
throw new Exception('Invalid Connection String');
}

if($auth_info){
return "mongodb://".$auth_info.'@'.$server_port.'/'.$db_info;
}
else {
return "mongodb://".$server_port;
}

}

protected static function getConnection($server = null, $port = null){
protected static function getConnection($server = null, $port = null, $userName = null, $password = null){
if ($server == null) {
$server = self::$_mongo_server;
$server = static::$_mongo_server;
}
if ($port == null) {
$port = self::$_mongo_port;
$port = static::$_mongo_port;
}
if ($userName == null) {
$userName = static::$_mongo_user_name;
}
if ($password == null) {
$password = static::$_mongo_password;
}

// determine the connection string for this server/port
$connectionString = self::getConnectionString($server,$port);
$connectionOptions = array("conect" => true, "timeout" => self::$_mongo_connection_timeout);
$connectionString = self::getConnectionString($server, $port, $userName, $password);
$connectionOptions = array("conect" => true, "timeout" => static::$_mongo_connection_timeout);

if(self::$_is_replica_set){
if(static::$_mongo_is_replica_set){
$connectionOptions['replicaSet'] = true;
}

$mongo = new Mongo($connectionString, $connectionOptions);
return $mongo;
}

public static function loadCollection($collectionName = null,$databaseName = null,$serverName = null,$portNumber = null) {
public static function loadCollection($collectionName = null,$databaseName = null,$serverName = null,$portNumber = null,$userName = null, $password = null) {
if (!isset($collectionName)) {
$collectionName = self::$_mongo_collection;
$collectionName = static::$_mongo_collection;
}
if (!isset($serverName)) {
$serverName = self::$_mongo_server;
$serverName = static::$_mongo_server;
}
if (!isset($portNumber)) {
$portNumber = self::$_mongo_port;
$portNumber = static::$_mongo_port;
}
if (!isset($databaseName)) {
$databaseName = self::$_mongo_database;
$databaseName = static::$_mongo_database;
}
if (!isset($userName)) {
$userName = static::$_mongo_user_name;
}
if (!isset($password)) {
$password = static::$_mongo_password;
}
try {
$mongo = self::getConnection($serverName,$portNumber);
$mongo = self::getConnection($serverName, $portNumber, $userName, $password);
$db = $mongo->selectDB($databaseName);
$collection = $db->selectCollection($collectionName);
return $collection;
}
catch (Exception $e) {
/* Add logging */
print $e->getMessage()."\n";
}
return FALSE;
}
Expand Down Expand Up @@ -302,7 +334,7 @@ public function load_single($id = null, $fields = array()){
* @return Success of the save (based on assumptions)
*/
public function save($safe = false, $upsert = true){
$collection = $this->loadCollection();
$collection = self::loadCollection();

if($collection){

Expand Down Expand Up @@ -411,16 +443,21 @@ public function increment($field, $amount = 1){

$field = $this->_remap_field($field);

$this->_data[$field] += $amount;
$this->_increment[$field] = $amount;
if(isset($this->_data[$field])){
$this->_data[$field] += $amount;
}
else{
$this->_data[$field] = $amount;
}

$this->_increment[$field] = $amount;
}

public function push($field, $value){

$field = $this->_remap_field($field);

if(is_array($this->_data[$field])){
if(isset($this->_data[$field]) && is_array($this->_data[$field])){
array_push($this->_data[$field], $value);
}
else if(isset($this->_data[$field])){
Expand All @@ -438,7 +475,7 @@ public function pop($field, $back = true){

$field = $this->_remap_field($field);

if(is_array($this->_data[$field])){
if(isset($this->_data[$field]) && is_array($this->_data[$field])){
if($back){
array_pop($this->_data[$field]);
$this->_pop[$field] = 1;
Expand Down Expand Up @@ -466,7 +503,7 @@ public function pushAll($field, $values = array()){
$this->$field = array($value);
}

if(is_array($this->_pushAll[$field])){
if(isset($this->_pushAll[$field]) && is_array($this->_pushAll[$field])){
$this->_pushAll[$field] = array_merge($this->_pushAll[$field], $values);
}
else{
Expand Down Expand Up @@ -498,7 +535,7 @@ public function pull($field, $value){
So we append the existing data and new data to "pullAll", then we unset "pull".
*/
if(isset($this->_pull[$field])){
if(is_array($this->_pullAll[$field])){
if(isset($this->_pullAll[$field]) && is_array($this->_pullAll[$field])){
$this->_pullAll[$field] = array_merge($this->_pullAll[$field], array($this->_pull[$field], $value));
}
else{
Expand All @@ -522,10 +559,24 @@ public function pullAll($field, $values = array()){
}

public function addToSet($field, $values = array()){

/*
This method has four cases:
- field exists vs. field DNE
- values is array vs. !is array
*/
$field = $this->_remap_field($field);

if(is_array($values)){
if(!isset($this->_data[$field])){
if(is_array($values)){
$this->_addToSet[$field] = $values;
$this->_data[$field] = $values;
}
else{
$this->_addToSet[$field] = array($values);
$this->_data[$field] = array($values);
}
}
else if(is_array($values)){

foreach($values as $v){
$this->_addToSet[$field][] = $v;
Expand Down Expand Up @@ -574,7 +625,7 @@ public function addToSet($field, $values = array()){
$this->_data[$field] = array($this->_data[$field], $values);
}
else{
$this->_set[$field] = array($values);
$this->_addToSet[$field] = array($values);
$this->_data[$field] = array($values);
}

Expand Down
26 changes: 18 additions & 8 deletions lib/MongoFactory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@ class MongoFactory {
public static function LoadObjectById($type, $id){

if(class_exists($type)){

$obj = new $type;
$obj->load_single($id);
return $obj;

}
else{

return null;

}

}

public static function LoadObjectsByQuery($type, $query = array()){

if(class_exists($type)){

$result_set = array();

try{

$collection = call_user_func(array($type, "loadCollection"));
$collection = self::GetEntityCollection($type);
$results = $collection->find($query);

while($results->hasNext()){
Expand All @@ -49,9 +43,25 @@ public static function LoadObjectsByQuery($type, $query = array()){
}
}
else {

return null;
}

}

public static function GetEntityCollection($type){

if(class_exists($type)){
try{
$collection = call_user_func(array($type, "loadCollection"));
return $collection;
}
catch(Exception $e){
/* TODO: Handle exception */
return null;
}
}
else {
return null;
}

}
Expand Down
48 changes: 45 additions & 3 deletions test/all_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ function AllTests(){

$this->start_mongo_basic();
$this->start_mongo_replica();
$this->start_mongo_auth();

$this->addFile('test_mongo_entity_basic.php');
$this->addFile('test_mongo_entity_increment.php');
$this->addFile('test_mongo_entity_arrays.php');
$this->addFile('test_mongo_entity_hash.php');
$this->addFile('test_mongo_entity_replica.php');
$this->addFile('test_mongo_entity_dynamic_collection.php');
# $this->addFile('test_mongo_entity_dynamic_collection.php');
$this->addFile('test_mongo_entity_auth.php');
$this->addFile('test_mongo_factory.php');

}
Expand All @@ -32,13 +34,14 @@ function __destruct(){

$this->stop_mongo_basic();
$this->stop_mongo_replica();
$this->stop_mongo_auth();

}

function start_mongo_basic(){

print "Starting server basic\n";
$output = shell_exec('/home/pubuntu/mongo/code/MongoModel/test/start_mongo_basic.sh');
$output = shell_exec('./start_mongo_basic.sh');
print "Waiting for server to boot\n";
do{
$start_check = false;
Expand Down Expand Up @@ -74,7 +77,7 @@ private function stop_mongo_basic(){
function start_mongo_replica(){

print "Starting server replica\n";
$output = shell_exec('/home/pubuntu/mongo/code/MongoModel/test/start_mongo_replica.sh');
$output = shell_exec('./start_mongo_replica.sh');
print "Waiting for server to boot\n";
do{
$start_check = false;
Expand Down Expand Up @@ -110,6 +113,45 @@ private function stop_mongo_replica(){
}
}

function start_mongo_auth(){

print "Starting server auth\n";
$output = shell_exec('./start_mongo_auth.sh');
print "Waiting for server to boot\n";
do{
$start_check = false;
try {
$mongo = new Mongo('mongodb://theadmin:anadminpassword@localhost:6904/admin');
#$mongo = new Mongo("localhost:6904");
$start_check = true;
}
catch (Exception $e){
$start_check = false;
}
print("...\n");
sleep(1);
} while (!$start_check);

if(preg_match('/forked process: (\d*)/', $output, $matches) !== false){
$pid = $matches[1];
return $pid;
}

return 0;

}

private function stop_mongo_auth(){
print "Stopping server auth\n";

try {
$this->stop_mongo_node(new Mongo('localhost:6904'));
}
catch (Exception $e) {
print $e->getMessage();
}
}

private function stop_mongo_node($mongo){
$db = $mongo->selectDB("admin");
$db->command(array("fsync" => 1));
Expand Down

0 comments on commit 3516c77

Please sign in to comment.