/** * @param string $urlPattern * @param array $urlPatternValues * @param array $options * @param string $viewResultClass * @return ViewResultInterface */ protected function executeDesignDocument($urlPattern, array $urlPatternValues, array $options = array(), $viewResultClass = null) { $con = $this->getConnection(); if ($viewResultClass === null) { $viewResultClass = 'phpcouch\\record\\ViewResult'; } $boolCleanup = function ($value) { return var_export((bool) $value, true); }; $cleanup = array('keys' => function ($value) { return json_encode(array('keys' => (array) $value)); }, 'key' => 'json_encode', 'startkey' => 'json_encode', 'start_key' => 'json_encode', 'endkey' => 'json_encode', 'end_key' => 'json_encode', 'limit' => 'intval', 'stale' => function ($value) { if ($value) { return 'ok'; } }, 'descending' => $boolCleanup, 'skip' => 'intval', 'group' => $boolCleanup, 'group_level' => 'intval', 'reduce' => $boolCleanup, 'include_docs' => $boolCleanup); array_walk($options, function (&$value, $key, $cleanup) { if (isset($cleanup[$key])) { $value = $cleanup[$key]($value); } }, $cleanup); $request = new HttpRequest(); if (isset($options['keys'])) { $request->setContent($options['keys']); $request->setMethod(HttpRequest::METHOD_POST); $request->setContentType('application/json'); unset($options['keys']); } $request->setDestination($con->buildUrl($urlPattern, $urlPatternValues, $options)); $viewResult = new $viewResultClass($this); $viewResult->hydrate($con->sendRequest($request)); return $viewResult; }
/** * Replicate two databases across instances of CouchDB * * @param string either a url to the remote db or the name of the local db * @param string either a url to the remote db or the name of the local db * @param array An associative array of options * * @throws HttpErrorException * * @author Simon Thulbourn <*****@*****.**> * @since 1.0.0 */ public function replicate($source, $target, array $options = array()) { if (!filter_var($source, FILTER_VALIDATE_URL)) { $source = $this->buildUrl(self::URL_PATTERN_DATABASE, array($source)); } if (!filter_var($target, FILTER_VALIDATE_URL)) { $target = $this->buildUrl(self::URL_PATTERN_DATABASE, array($target)); } try { $request = new HttpRequest($this->buildUrl(self::URL_PATTERN_REPLICATE), HttpRequest::METHOD_POST); $values = array('source' => $source, 'target' => $target); foreach ($options as $key => $value) { $values[$key] = $value; } $request->setContent(json_encode($values)); $request->setContentType('application/json'); $result = new \phpcouch\record\Record($this); $result->hydrate($this->sendRequest($request)); if (!isset($result->ok) && $result->ok !== true) { throw new \Exception('Cannot replicate these items'); } } catch (\Exception $e) { throw $e; } }