protected function execute(InputInterface $input, OutputInterface $output)
{
$dm = $this->getHelper('couchdb')->getDocumentManager();
$couchDbClient = $dm->getCouchDBClient();
$config = $dm->getConfiguration();
//If a docname is provided update only that document,
//otherwise update all modified/new docs.
if (is_string($inputDoc = $input->getArgument('docname'))) {
$designDocNames = array($inputDoc);
} else {
$designDocNames = $config->getDesignDocumentNames();
}
$foundChanges = false;
foreach ($designDocNames as $docName) {
$designDocData = $config->getDesignDocument($docName);
$localDesignDoc = new $designDocData['className']($designDocData['options']);
$localDocBody = $localDesignDoc->getData();
$remoteDocBody = $couchDbClient->findDocument('_design/' . $docName)->body;
if ($this->isMissingOrDifferent($localDocBody, $remoteDocBody)) {
$response = $couchDbClient->createDesignDocument($docName, $localDesignDoc);
$foundChanges = true;
if ($response->status < 300) {
$output->writeln("Succesfully updated: " . $docName);
} else {
$output->writeln("Error updating {$docName}: {$response->body['reason']}");
}
}
}
if (!$foundChanges) {
$output->writeln("No changes found; nothing to do.");
}
}