/** * @param EventDispatcher $eventDispatcher */ public function attach(EventDispatcher $eventDispatcher) { $studentClient = CouchDBClient::create(['dbname' => static::$studentDb]); $solutionClient = CouchDBClient::create(['dbname' => static::$solutionDb]); $studentClient->createDatabase($studentClient->getDatabase()); $solutionClient->createDatabase($solutionClient->getDatabase()); $eventDispatcher->listen('verify.start', function (Event $e) use($studentClient, $solutionClient) { $e->getParameter('exercise')->seed($studentClient); $this->replicateDbFromStudentToSolution($studentClient, $solutionClient); }); $eventDispatcher->listen('run.start', function (Event $e) use($studentClient) { $e->getParameter('exercise')->seed($studentClient); }); $eventDispatcher->listen('cli.verify.reference-execute.pre', function (CliExecuteEvent $e) { $e->prependArg('phpschool'); }); $eventDispatcher->listen(['cli.verify.student-execute.pre', 'cli.run.student-execute.pre'], function (CliExecuteEvent $e) { $e->prependArg('phpschool-student'); }); $eventDispatcher->insertVerifier('verify.finish', function (Event $e) use($studentClient) { $verifyResult = $e->getParameter('exercise')->verify($studentClient); if (false === $verifyResult) { return Failure::fromNameAndReason($this->getName(), 'Database verification failed'); } return Success::fromCheck($this); }); $eventDispatcher->listen(['cli.verify.reference-execute.fail', 'verify.finish', 'run.finish'], function (Event $e) use($studentClient, $solutionClient) { $studentClient->deleteDatabase(static::$studentDb); $solutionClient->deleteDatabase(static::$solutionDb); }); }
/** * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return \Doctrine\CouchDB\Connection */ public function createService(ServiceLocatorInterface $serviceLocator) { /** @var $options \DoctrineCouchODMModule\Options\Connection */ $options = $this->getOptions($serviceLocator, 'connection'); $optionsArray = $options->toArray(); $optionsArray['logging'] = $options->getLogging(); return CouchDBClient::create($optionsArray); }
/** * Construct and returns a CouchDBClient * * @param array $options * * Options are: * * @option string $dbname The name of the database * @option string $type The connection type, "socket" or "stream" * @option string $host * @option int $port * @option string $user * @option string $password * @option string $ip * @option bool $logging * * @return \Doctrine\CouchDB\CouchDBClient */ public static function couchDbClient($options = array()) { $_key = Option::get($options, 'host', 'localhost') . ':' . Option::get($options, 'port', 5984); if (!isset(self::$_clients[$_key])) { self::$_clients[$_key] = \Doctrine\CouchDB\CouchDBClient::create($options); } return self::$_clients[$_key]; }
/** * couchDB - Couch DB Connection * - Uses Doctrine CouchDBClient * @return Object $connection */ public function couchDB() { $host = $GLOBALS['couchdb_host']; $port = $GLOBALS['couchdb_port']; $usename = $GLOBALS['couchdb_user']; $password = $GLOBALS['couchdb_pass']; $database = $GLOBALS['couchdb_dbase']; $enable_log = $GLOBALS['couchdb_log'] == 1 ? true : false; $options = array('host' => $host, 'port' => $port, 'user' => $usename, 'password' => $password, 'logging' => $enable_log, 'dbname' => $database); $connection = \Doctrine\CouchDB\CouchDBClient::create($options); return $connection; }
/** * Execute the console command. * * @return mixed */ public function handle() { $database = $this->argument('database'); $couch = CouchDBClient::create(array('dbname' => $database, 'user' => env('COUCH_APP_USER'), 'password' => env('COUCH_APP_PASS'))); $type = $this->argument('type'); $doc_req = $couch->allDocs(); if ($doc_req->status == 200 && $doc_req->body) { foreach ($doc_req->body['rows'] as $row) { $doc = $row['doc']; if ($doc['_id'][0] == '_') { // special docs continue; } if ($this->option('all')) { $this->info("deleting " . $doc['_id']); $couch->deleteDocument($doc['_id'], $doc['_rev']); } elseif ($type && @$doc['type'] == $type) { $this->info("deleting " . $doc['_id']); $couch->deleteDocument($doc['_id'], $doc['_rev']); } } } }
/** * Create a new database connection instance. * * @param array $config */ public function __construct(array $config) { $this->config = $config; $this->db = CouchDBClient::create($config); }
public function login(Request $request) { try { // Logging Response from Facebook into DB $couch = CouchDBClient::create(array('dbname' => 'careapp_log_db', 'user' => env('COUCH_APP_USER'), 'password' => env('COUCH_APP_PASS'))); $fb_response = $request->input('fb_response'); $fb_response['type'] = 'fb_response'; $fb_response['timestamp'] = time(); $couch->postDocument($fb_response); $response = []; // Validate response from FB if (empty($fb_response['status']) || $fb_response['status'] != 'connected' || empty($fb_response['authResponse']['accessToken']) || empty($fb_response['authResponse']['userID']) || empty($fb_response['authResponse']['expiresIn'])) { Log::error('FB Status: Not Connected / Invalid'); $response['status'] = 'error'; return $response; } // Get User Profile $fb = new Facebook\Facebook(['app_id' => env('FB_APP_ID'), 'app_secret' => env('FB_APP_SECRET'), 'default_graph_version' => 'v2.2']); $access_token = $fb_response['authResponse']['accessToken']; $fb_query = $fb->get('/me?fields=id,name,email,gender,picture', $access_token); $fb_user = $fb_query->getGraphUser(); // Check if requested UserID and AuthToken are for same FB User if ($fb_response['authResponse']['userID'] != $fb_user['id']) { // Possible forgery Log::warning('Possible threat: UC59'); $response['status'] = 'error'; return $response; } // Check if user exists in DB $couch = CouchDBClient::create(array('dbname' => '_users', 'user' => env('COUCH_ADMIN_USER'), 'password' => env('COUCH_ADMIN_PASS'))); $username = '******' . $fb_user['id']; $user_id = "org.couchdb.user:{$username}"; $user_req = $couch->findDocument($user_id); $is_existing_user = false; if ($user_req->status == 200 && $user_req->body['_id']) { // User Exists $is_existing_user = true; $user = $user_req->body; } else { // New User $user = ['_id' => $user_id, 'type' => 'user', 'roles' => [], 'fb_id' => $fb_user['id'], 'display_name' => $fb_user['name'], 'email' => $fb_user['email'], 'name' => $username, 'gender' => $fb_user['gender']]; } $user['password'] = $this->_generate_password(); $user['fb_access_token'] = $fb_response['authResponse']['accessToken']; $user['fb_expires_in'] = $fb_response['authResponse']['expiresIn']; $user['fb_expire_time'] = time() + $fb_response['authResponse']['expiresIn']; // Create/Update User if ($fb_user['picture']['url'] && !$fb_user['picture']['is_silhouette']) { $user['fb_picture'] = $fb_user['picture']['url']; $response['fb_picture'] = $user['fb_picture']; } $couch->putDocument($user, $user['_id']); $response['gender'] = $user['gender']; $response['display_name'] = $user['display_name']; $response['status'] = 'success'; $response['token'] = $user['password']; // If existing user, fetch profile too if ($is_existing_user) { $couch = CouchDBClient::create(array('dbname' => 'careapp_profiles_db', 'user' => env('COUCH_APP_USER'), 'password' => env('COUCH_APP_PASS'))); $profile_req = $couch->findDocument($username); if ($profile_req->status == 200 && $profile_req->body['_id']) { $response['profile'] = $profile_req->body; } } return $response; } catch (Exception $e) { Log::error($e->getMessage()); $response['status'] = 'error'; return $response; } }
<?php namespace Relaxed\ReplicationDemo; require "../vendor/autoload.php"; use Doctrine\CouchDB\CouchDBClient; use Relaxed\Replicator\ReplicationTask; use Relaxed\Replicator\Replicator; $sourceClient = CouchDBClient::create(array('dbname' => 'sourcedb')); $targetClient = CouchDBClient::create(array('dbname' => 'targetdb1')); // Add docs to the source db. $id = 'id'; $docs = array(array('_id' => $id . '1', 'foo' => 'bar1', '_rev' => '1-abc'), array('_id' => $id . '2', 'foo' => 'bar2', '_rev' => '1-bcd'), array('_id' => $id . '3', 'foo' => 'bar3', '_rev' => '1-cde')); $updater = $sourceClient->createBulkUpdater(); $updater->updateDocuments($docs); // Set newedits to false to use the supplied _rev instead of assigning // new ones. $updater->setNewEdits(false); $response = $updater->execute(); // Create the replication task. $task = new ReplicationTask(); // Enable target creation. $task->setCreateTarget(true); // Create the replicator. $replicator = new Replicator($sourceClient, $targetClient, $task); // Get the replication report as an array. var_dump($replicator->startReplication(false, true)); /* * array(3) { ["multipartResponse"]=> array(0) {
<?php require '../vendor/autoload.php'; set_time_limit(0); $client = \Doctrine\CouchDB\CouchDBClient::create(array('dbname' => 'pokedex')); //pokemon $pokedex = file_get_contents('http://pokeapi.co/api/v1/pokedex/1/'); $pokedex_data = json_decode($pokedex, true); foreach ($pokedex_data['pokemon'] as $row) { $name = $row['name']; $pokemon = file_get_contents('http://pokeapi.co/' . $row['resource_uri']); $pokemon = json_decode($pokemon, true); $pokemon_description = file_get_contents('http://pokeapi.co/' . $pokemon['descriptions'][0]['resource_uri']); $pokemon['description'] = json_decode($pokemon_description, true)['description']; $pokemon_sprites = file_get_contents('http://pokeapi.co' . $pokemon['sprites'][0]['resource_uri']); $pokemon_sprites = json_decode($pokemon_sprites, true); $pokemon['small_photo'] = 'http://pokeapi.co' . $pokemon_sprites['image']; $pokemon['photo'] = 'http://img.pokemondb.net/artwork/' . strtolower($pokemon['name']) . '.jpg'; $client->postDocument($pokemon); }
public static function init() { $couch = parent::create(array('dbname' => Config::get('database.couch.default.database'), 'host' => Config::get('database.couch.default.host'), 'port' => (int) Config::get('database.couch.default.port'))); return $couch; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage There is no client implementation registered for foo, valid options are: socket, stream */ public function testCreateClientMissingClientException() { CouchDBClient::create(array('dbname' => 'test', 'type' => 'foo')); }
/** * Factory method for a Document Manager. * * @param Configuration $config * @param EventManager $evm * @return DocumentManager */ public static function create($couchParams, Configuration $config = null, EventManager $evm = null) { if (is_array($couchParams)) { $couchClient = CouchDBClient::create($couchParams); } else { if ($couchParams instanceof CouchDBClient) { $couchClient = $couchParams; } else { throw new \InvalidArgumentException("Expecting array of instance of CouchDBClient as first argument to DocumentManager::create()."); } } return new DocumentManager($couchClient, $config, $evm); }
public function _add_countries() { $couch = CouchDBClient::create(array('dbname' => 'careapp_passions_db', 'user' => env('COUCH_APP_USER'), 'password' => env('COUCH_APP_PASS'))); $countries_json = "http://vocab.nic.in/rest.php/country/json"; $countries_text = file_get_contents($countries_json); $countries = json_decode($countries_text, true); if (empty($countries)) { $this->info("Warning: No countries."); return; } $count = 0; $errors = 0; foreach ($countries['countries'] as $item) { if ($errors > 10) { break; } $country = ['name' => ucwords(strtolower($item['country']['country_name'])), 'code' => strtolower($item['country']['country_id']), 'type' => 'country']; $country['_id'] = 'cty-' . $country['code']; try { $couch->putDocument($country, $country['_id']); $count++; } catch (Exception $e) { $errors++; } } $this->info("{$count} countries added. {$errors} errors."); }