Esempio n. 1
0
 public function preDispatch()
 {
     $this->_helper->layout->setLayout('live');
     // Initialize mongodb
     $db = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application-cli.ini', 'mongodb');
     Shanty_Mongo::addConnections($db);
 }
Esempio n. 2
0
 public function tearDown()
 {
     $this->_restoreIncludePath();
     $this->_connection->selectDb(TESTS_SHANTY_MONGO_DB)->selectCollection('user')->drop();
     $this->_connection->selectDb(TESTS_SHANTY_MONGO_DB)->selectCollection('article')->drop();
     Shanty_Mongo::makeClean();
     parent::tearDown();
 }
Esempio n. 3
0
 public function __construct($connectionString = null, array $options = array())
 {
     Shanty_Mongo::init();
     // Set the server to local host if one was not provided
     if (is_null($connectionString)) {
         $connectionString = '127.0.0.1';
     }
     $options['connect'] = false;
     $this->_connectionInfo = $options;
     $this->_connectionInfo['connectionString'] = $connectionString;
     return parent::__construct($connectionString, $options);
 }
Esempio n. 4
0
 public function __construct($connectionString = null, array $options = array())
 {
     Shanty_Mongo::init();
     // Set the server to local host if one was not provided
     if (is_null($connectionString)) {
         $connectionString = '127.0.0.1';
     }
     // Force mongo to connect only when we need to
     $options['connect'] = false;
     $connectionInfo = self::parseConnectionString($connectionString);
     $this->_connectionInfo = array_merge($options, $connectionInfo);
     return parent::__construct($connectionString, $options);
 }
Esempio n. 5
0
 /**
  * Add an operation
  * 
  * @param string $operation
  * @param array $data
  */
 public function addOperation($operation, $property = null, $value = null)
 {
     // Make sure the operation is valid
     if (!Shanty_Mongo::isValidOperation($operation)) {
         require_once 'Shanty/Mongo/Exception.php';
         throw new Shanty_Mongo_Exception("'{$operation}' is not valid operation");
     }
     // Prime the specific operation
     if (!array_key_exists($operation, $this->_operations)) {
         $this->_operations[$operation] = array();
     }
     // Save the operation
     if (is_null($property)) {
         $path = $this->getPathToDocument();
     } else {
         $path = $this->getPathToProperty($property);
     }
     // Mix operation with existing operations if needed
     switch ($operation) {
         case '$pushAll':
         case '$pullAll':
             if (!array_key_exists($path, $this->_operations[$operation])) {
                 break;
             }
             $value = array_merge($this->_operations[$operation][$path], $value);
             break;
     }
     $this->_operations[$operation][$path] = $value;
 }
Esempio n. 6
0
 public function testGetConnection()
 {
     $connection = new Shanty_Mongo_Connection('localhost');
     Shanty_Mongo::addSlave($connection);
     $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, My_ShantyMongo_User::getConnection());
     $this->assertEquals(TESTS_SHANTY_MONGO_CONNECTIONSTRING, My_ShantyMongo_User::getConnection()->getActualConnectionString());
     $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, My_ShantyMongo_User::getConnection(false));
     $this->assertEquals('localhost', My_ShantyMongo_User::getConnection(false)->getActualConnectionString());
 }
Esempio n. 7
0
 public static function getConnection($writable = true)
 {
     if ($writable) {
         $connection = Shanty_Mongo::getWriteConnection(static::getConnectionGroupName());
     } else {
         $connection = Shanty_Mongo::getReadConnection(static::getConnectionGroupName());
     }
     return $connection;
 }
Esempio n. 8
0
 public function test_GetMongoDb()
 {
     $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, $this->_bob->_getMongoDb());
     $this->assertEquals(TESTS_SHANTY_MONGO_DB, $this->_bob->_getMongoDb()->__toString());
     $connection = new Shanty_Mongo_Connection('localhost');
     Shanty_Mongo::addSlave($connection);
     $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, $this->_bob->_getMongoDb(false));
     $this->assertEquals(TESTS_SHANTY_MONGO_DB, $this->_bob->_getMongoDb(false)->__toString());
 }
Esempio n. 9
0
 /**
  * @dataProvider validOperations
  */
 public function testValidOperation($operation)
 {
     $this->assertTrue(Shanty_Mongo::isValidOperation($operation));
     $this->assertFalse(Shanty_Mongo::isValidOperation('non valid op'));
 }
Esempio n. 10
0
 public function referralAction()
 {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(TRUE);
     $db = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application-cli.ini', 'mongodb');
     Shanty_Mongo::addConnections($db);
     $ref = App_Model_Mongodb_RequestLog::referral();
     print_r($ref);
 }
Esempio n. 11
0
     * Get a read connection
     * 
     * @param string $connectionGroupName The connection group name
     * @return Shanty_Mongo_Connection
     */
    public static function getReadConnection($connectionGroupName = 'default')
    {
        $connectionGroup = static::getConnectionGroup($connectionGroupName);
        if ($connectionGroupName == 'default' && count($connectionGroup->getSlaves()) === 0 && count($connectionGroup->getMasters()) === 0) {
            // Add a connection to localhost if no connections currently exist for the default connection group
            $connectionGroup->addMaster(new Shanty_Mongo_Connection('127.0.0.1'));
        }
        if (!($connection = $connectionGroup->getReadConnection($connectionGroupName))) {
            require_once 'Shanty/Mongo/Exception.php';
            throw new Shanty_Mongo_Exception("No read connection available for the '{$connectionGroupName}' connection group");
        }
        return $connection;
    }
    /**
     * Return Shanty_Mongo to pre-init status
     */
    public static function makeClean()
    {
        static::removeConnectionGroups();
        static::removeRequirements();
        static::removeRequirementCreators();
        static::$_initialised = false;
    }
}
Shanty_Mongo::init();
Esempio n. 12
0
 /**
  * Get an instance of MongoDb
  * 
  * @return MongoDb
  * @param boolean $useSlave
  */
 public static function getMongoDb($writable = true)
 {
     if (!static::hasDbName()) {
         require_once 'Shanty/Mongo/Exception.php';
         throw new Shanty_Mongo_Exception(get_called_class() . '::$_db is null');
     }
     if ($writable) {
         $connection = Shanty_Mongo::getWriteConnection(static::getConnectionGroupName());
     } else {
         $connection = Shanty_Mongo::getReadConnection(static::getConnectionGroupName());
     }
     return $connection->selectDB(static::getDbName());
 }