/** * Get mongo client instance. * @return array $config */ public static function getClient() { // get configurations from config file here. $config = file_get_contents(__DIR__ . '/../config.json'); $config = (array) json_decode($config); $client = new Client($config['dns'] . '/' . $config['database']); $client->useDatabase($config['database']); return $client; }
public function testUseDatabase() { $collection = $this->client->useDatabase('test')->getCollection('some-collection'); $this->assertInstanceOf('\\Sokil\\Mongo\\Collection', $collection); }
$app->url = Url::createFromServer($_SERVER); } // Error handling $app->error(function (\Exception $e) { $code = $e->getCode(); if ($code < 100) { $code = 500; } Resource::error($code, $e->getMessage()); }); // Database layer setup $app->hook('slim.before', function () use($app) { $app->container->singleton('mongo', function () use($app) { $client = new Client($app->config('database')['host_uri']); $client->map([$app->config('database')['db_name'] => '\\API\\Collection']); $client->useDatabase($app->config('database')['db_name']); return $client; }); }); // CORS compatibility layer (Internet Explorer) $app->hook('slim.before.router', function () use($app) { if ($app->request->isPost() && $app->request->get('method')) { $method = $app->request->get('method'); $app->environment()['REQUEST_METHOD'] = strtoupper($method); mb_parse_str($app->request->getBody(), $postData); $parameters = new Set($postData); if ($parameters->has('content')) { $content = $parameters->get('content'); $app->environment()['slim.input'] = $content; $parameters->remove('content'); } else {