示例#1
0
function getConnection($cache = false)
{
    static $first;
    $conf = new \ActiveMongo2\Configuration(__DIR__ . "/tmp/mapper.php");
    $conf->addModelPath(__DIR__ . '/docs')->development();
    if (!empty($_SERVER["NAMESPACE"])) {
        if (!$first) {
            print "Using namespace {$_SERVER['NAMESPACE']}\n";
        }
        $conf->SetNamespace($_SERVER["NAMESPACE"]);
    }
    if (!$first) {
        $mongo = new MongoClient();
        $mongo->selectDB('activemongo2_tests')->drop();
        $mongo->selectDB('activemongo2_tests_foobar')->drop();
        $config = new ActiveMongo2\Configuration();
        unlink($config->getLoader());
    }
    if (empty($_SERVER["NAMESPACE"])) {
        $zconn = new \ActiveMongo2\Client(new MongoClient(), 'activemongo2_tests', __DIR__ . '/docs');
    } else {
        $zconn = new \ActiveMongo2\Connection($conf, new MongoClient(), 'activemongo2_tests');
    }
    $zconn->AddConnection('foobar', new MongoClient(), 'activemongo2_tests_foobar', 'zzzz');
    if ($cache) {
        $zconn->setCacheStorage(new \ActiveMongo2\Cache\Storage\Memory());
    }
    $first = true;
    return $zconn;
}
示例#2
0
/**
 *  @Cli("db:create_index", "Create indexes in activemongo2")
 *  @Arg("docdir", REQUIRED, "Directory where activemongo2 classes are defined")
 *  @Arg("db", REQUIRED, "Database name")
 */
function create_index($input, $output)
{
    $args = $input->getArguments();
    if (!is_dir($args['docdir'])) {
        throw new \RuntimeException("{$args['docdir']} is not not a directory");
    }
    $conf = new \ActiveMongo2\Configuration(tempnam(sys_get_temp_dir(), "activemongo2"));
    $conf->addModelPath($args['docdir'])->development();
    $mongo = new \MongoClient();
    $zconn = new \ActiveMongo2\Connection($conf, $mongo, $args['db']);
    $zconn->ensureIndex();
    $output->writeLn("creating were created");
}
示例#3
0
/**
 *  @Service(activemongo, {
 *      host: {default: 'localhost'},
 *      user: {default: NULL},
 *      pass: {default: NULL},
 *      replicaSet: {default: NULL},
 *      db: {required: true},
 *      opts: { default:{}, type: 'hash'},
 *      path: { require: true, type: array_dir},
 *      temp_dir: { default: '/tmp', type: dir },
 *      w: {default: 1},
 *      devel: {default: true}
 *  }, { shared: true })
 */
function activemongo2_service($config)
{
    if (!$config['replicaSet']) {
        $conn = new \MongoClient($config['host'], $config['opts']);
    } else {
        $conn = new \MongoClient($config['host'], array("replicaSet" => $config['replicaSet']), $config['opts']);
        $conn->setReadPreference(\MongoClient::RP_SECONDARY);
        \MongoCursor::$slaveOkay = true;
    }
    $db = $conn->selectDB($config['db']);
    if ($config['user'] || $config['pass']) {
        $db->authenticate($config['user'], $config['pass']);
    }
    $conf = new \ActiveMongo2\Configuration($config['temp_dir'] . "/activemongo2__" . $db . ".php");
    foreach ((array) $config['path'] as $path) {
        $conf->addModelPath($path);
    }
    if ($config['devel']) {
        $conf->development();
    }
    $conf->setWriteConcern($config['w']);
    $mongo = new \ActiveMongo2\Connection($conf, $conn, $db);
    return $mongo;
}
示例#4
0
<?php

require __DIR__ . "/../vendor/autoload.php";
$conf = new \ActiveMongo2\Configuration(__DIR__ . "/tmp/foo.php");
$conf->addModelPath(__DIR__ . '/docs')->development();
$mongo = new MongoClient();
$zconn = new \ActiveMongo2\Connection($conf, $mongo, 'activemongo2_tests');
$zconn->worker();