Platformsh\Cli\Util\RelationshipsUtil::chooseDatabase PHP Метод

chooseDatabase() публичный Метод

public chooseDatabase ( string $sshUrl, Symfony\Component\Console\Input\InputInterface $input ) : array | false
$sshUrl string
$input Symfony\Component\Console\Input\InputInterface
Результат array | false
    public function chooseDatabase($sshUrl, InputInterface $input)
    {
        $relationships = $this->getRelationships($sshUrl);
        // Filter to find database (mysql and pgsql) relationships.
        $relationships = array_filter($relationships, function (array $relationship) {
            foreach ($relationship as $key => $service) {
                if ($service['scheme'] === 'mysql' || $service['scheme'] === 'pgsql') {
                    return true;
                }
            }
            return false;
        });
        if (empty($relationships)) {
            $this->output->writeln('No databases found');
            return false;
        }
        $questionHelper = new QuestionHelper($input, $this->output);
        $choices = [];
        $separator = '.';
        foreach ($relationships as $name => $relationship) {
            $serviceCount = count($relationship);
            foreach ($relationship as $key => $service) {
                $choices[$name . $separator . $key] = $name . ($serviceCount > 1 ? '.' . $key : '');
            }
        }
        $choice = $questionHelper->choose($choices, 'Enter a number to choose a database:');
        list($name, $key) = explode($separator, $choice, 2);
        $database = $relationships[$name][$key];
        // Add metadata about the database.
        $database['_relationship_name'] = $name;
        $database['_relationship_key'] = $key;
        return $database;
    }

Usage Example

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!$this->validateInput($input, $output)) {
         return 1;
     }
     $sshOptions = '';
     $sshUrl = $this->getSelectedEnvironment()->getSshUrl($input->getOption('app'));
     $util = new RelationshipsUtil($output);
     $database = $util->chooseDatabase($sshUrl, $input);
     if (empty($database)) {
         return 1;
     }
     $sqlCommand = "mysql --no-auto-rehash --database={$database['path']}" . " --host={$database['host']} --port={$database['port']}" . " --user={$database['username']} --password={$database['password']}";
     $query = $input->getArgument('query');
     if ($query) {
         $sqlCommand .= ' --execute ' . escapeshellarg($query) . ' 2>&1';
     } else {
         $sshOptions .= ' -qt';
     }
     $command = 'ssh' . $sshOptions . ' ' . escapeshellarg($sshUrl) . ' ' . escapeshellarg($sqlCommand);
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $output->writeln("Running command: <info>{$command}</info>");
     }
     passthru($command, $return_var);
     return $return_var;
 }
All Usage Examples Of Platformsh\Cli\Util\RelationshipsUtil::chooseDatabase