setName('databox:create')
->addArgument('databox', InputArgument::REQUIRED, 'Database name for the databox', null)
->addArgument('owner', InputArgument::REQUIRED, 'Login of the databox admin user', null)
->addOption('connection', 'c', InputOption::VALUE_NONE, 'Flag to set new database settings')
->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host', 'localhost')
->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port', 3306)
->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user', 'phrasea')
->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'MySQL server password', null)
->addOption(
'db-template',
null,
InputOption::VALUE_OPTIONAL,
'Databox template',
null
);
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
/** @var DialogHelper $dialog */
$dialog = $this->getHelperSet()->get('dialog');
$databoxName = $input->getArgument('databox');
$connectionSettings = $input->getOption('connection') == false ? null : new DataboxConnectionSettings(
$input->getOption('db-host'),
$input->getOption('db-port'),
$input->getOption('db-user'),
$input->getOption('db-password')
);
/** @var UserRepository $userRepository */
$userRepository = $this->container['repo.users'];
$owner = $userRepository->findByLogin($input->getArgument('owner'));
if(!$owner) {
$output->writeln(sprintf("Unknown user \"%s\"", $input->getArgument('owner')));
return 1;
}
/** @var DataboxService $databoxService */
$databoxService = $this->container['databox.service'];
if($databoxService->exists($databoxName, $connectionSettings)) {
$output->writeln(sprintf("Database \"%s\" already exists", $databoxName));
return 1;
}
/** @var StructureTemplate $templates */
$templates = $this->container['phraseanet.structure-template'];
// if a template name is provided, check that this template exists
$templateName = $input->getOption('db-template');
if($templateName && !$templates->getByName($templateName)) {
throw new \Exception_InvalidArgument(sprintf("Databox template \"%s\" not found.", $templateName));
}
if(!$templateName) {
// propose a default template : the first available if "en-simple" does not exists.
$defaultDBoxTemplate = $templates->getDefault();
do {
$templateName = $dialog->ask($output, 'Choose a template from ('.$templates->toString().') for metadata structure [default: "'.$defaultDBoxTemplate.'"] : ', $defaultDBoxTemplate);
if(!$templates->getByName($templateName)){
$output->writeln(" Data-Box template : Template not found, try again.");
}
}
while (!$templates->getByName($templateName));
}
try {
$databoxService->createDatabox(
$databoxName,
$templateName,
$owner,
$connectionSettings
);
}
catch(\Exception $e) {
$output->writeln(sprintf("Failed to create database \"%s\", error=\"%s\""
, $databoxName
, $e->getMessage()
));
return 1;
}
$output->writeln('Databox created');
return 0;
}
}