/** * Try to backup data in memory * * @return string Returns a message describing the result */ public function rescueData() { $resultMessageParts = array(); $data = $this->coordinator instanceof Coordinator ? $this->coordinator->getObjectStore() : array(); $backupDirectory = $this->getRescueDirectory(); if ($data) { foreach ($data as $databaseIdentifier => $database) { $currentData = NULL; if ($database instanceof Database) { $currentData = $database->getRawData(); } else { if ($database instanceof \Iterator) { $currentData = iterator_to_array($database); } } if (!$currentData) { $resultMessageParts[] = sprintf('Can not rescue database %s', $databaseIdentifier); continue; } $backupData = NULL; $jsonData = json_encode($currentData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); if ($jsonData) { $backupData = $jsonData; } else { $backupData = serialize($currentData); } $backupPath = $backupDirectory . $databaseIdentifier . '.' . ($jsonData ? 'json' : 'bin'); if (file_put_contents($backupPath, $backupData)) { $resultMessageParts[] = sprintf('Created backup of database %s at %s', $databaseIdentifier, $backupPath); } else { $resultMessageParts[] = sprintf('Can not rescue database %s', $databaseIdentifier); } } } else { $resultMessageParts[] = sprintf('Can not find any data to rescue'); } return implode(PHP_EOL, $resultMessageParts); }
/** * Returns the database for the given request or NULL if it is not specified * * @param RequestInfo $requestInfo * @return DatabaseInterface|NULL */ public function getDatabaseForRequestInfo(RequestInfo $requestInfo) { if (!$requestInfo->getDatabaseIdentifier()) { return NULL; } $databaseIdentifier = $requestInfo->getDatabaseIdentifier(); // if (!$this->coordinator->databaseExists($databaseIdentifier)) { // return NULL; // } try { return $this->coordinator->getDatabase($databaseIdentifier); } catch (ReaderException $exception) { return NULL; } }
/** * Executes a subcommand * * @param string $subcommand * @param array $arguments * @param OutputInterface $output */ public function executeSubCommand($subcommand, $arguments, OutputInterface $output) { try { switch ($subcommand) { case 'create': $databaseIdentifier = array_shift($arguments); $options = $arguments; if (!$databaseIdentifier) { throw new \InvalidArgumentException('Missing database identifier argument', 1412524227); } $database = $this->coordinator->createDatabase($databaseIdentifier, $options); if ($database) { $output->writeln(sprintf('<info>Created database %s</info>', $databaseIdentifier)); } break; case 'drop': $databaseIdentifier = array_shift($arguments); if (!$databaseIdentifier) { throw new \InvalidArgumentException('Missing database identifier argument', 1412524227); } $this->coordinator->dropDatabase($databaseIdentifier); $output->writeln(sprintf('<info>Dropped database %s</info>', $databaseIdentifier)); break; case 'list': case 'll': $databases = $this->coordinator->listDatabases(); if ($databases) { $output->writeln('<info>Databases:</info>'); foreach ($databases as $databaseIdentifier) { $output->writeln($databaseIdentifier); } } else { $output->writeln('<info>No databases found</info>'); } break; default: throw new \InvalidArgumentException(sprintf('Undefined sub command "%s"', $subcommand), 1412525230); } } catch (\Exception $exception) { $output->writeln('<error>' . $exception->getMessage() . '</error>'); } }
/** * A maintenance task that will be performed when the server becomes idle */ public function runMaintenance() { $this->coordinator->commitDatabases(); Manager::cleanup(); }