getApplication() public static method

Get a single application by name.
public static getApplication ( string | null $name, string $directory, CliConfig $config = null ) : LocalApplication
$name string | null The application name.
$directory string The absolute path to a directory.
$config Platformsh\Cli\CliConfig CLI configuration.
return LocalApplication
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->validateInput($input);
     $drushCommand = $input->getArgument('cmd');
     $sshOptions = '';
     // Pass through options that the CLI shares with Drush and SSH.
     foreach (['yes', 'no', 'quiet'] as $option) {
         if ($input->getOption($option)) {
             $drushCommand .= " --{$option}";
         }
     }
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
         $drushCommand .= " --debug";
         $sshOptions .= ' -vv';
     } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
         $drushCommand .= " --verbose";
         $sshOptions .= ' -v';
     } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $drushCommand .= " --verbose";
     } elseif ($output->getVerbosity() == OutputInterface::VERBOSITY_QUIET) {
         $drushCommand .= " --quiet";
         $sshOptions .= ' -q';
     }
     $appName = $this->selectApp($input, function (LocalApplication $app) {
         return Drupal::isDrupal($app->getRoot());
     });
     $selectedEnvironment = $this->getSelectedEnvironment();
     $sshUrl = $selectedEnvironment->getSshUrl($appName);
     // Get the LocalApplication object for the specified application, if
     // available.
     $projectRoot = $this->getProjectRoot();
     if ($projectRoot && $this->selectedProjectIsCurrent()) {
         $app = LocalApplication::getApplication($appName, $projectRoot, self::$config);
     }
     // Use the local application configuration (if available) to determine
     // the correct Drupal root.
     if (isset($app)) {
         $drupalRoot = '/app/' . $app->getDocumentRoot();
     } else {
         // Fall back to the PLATFORM_DOCUMENT_ROOT environment variable,
         // which is usually correct, except where the document_root was
         // specified as '/'.
         $documentRootEnvVar = self::$config->get('service.env_prefix') . 'DOCUMENT_ROOT';
         $drupalRoot = '${' . $documentRootEnvVar . ':-/app/public}';
         $this->debug('<comment>Warning:</comment> using $' . $documentRootEnvVar . ' for the Drupal root. This fails in cases where the document_root is /.');
     }
     $dimensions = $this->getApplication()->getTerminalDimensions();
     $columns = $dimensions[0] ?: 80;
     $sshDrushCommand = "COLUMNS={$columns} drush --root=\"{$drupalRoot}\"";
     if ($environmentUrl = $selectedEnvironment->getLink('public-url')) {
         $sshDrushCommand .= " --uri=" . escapeshellarg($environmentUrl);
     }
     $sshDrushCommand .= ' ' . $drushCommand . ' 2>&1';
     $command = 'ssh' . $sshOptions . ' ' . escapeshellarg($sshUrl) . ' ' . escapeshellarg($sshDrushCommand);
     return $this->getHelper('shell')->executeSimple($command);
 }
Ejemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->validateInput($input);
     $projectRoot = $this->getProjectRoot();
     if (!$projectRoot) {
         throw new RootNotFoundException();
     }
     $appName = $this->selectApp($input);
     $sshUrl = $this->getSelectedEnvironment()->getSshUrl($appName);
     // Get and parse app config.
     $app = LocalApplication::getApplication($appName, $projectRoot);
     $appConfig = $app->getConfig();
     if (empty($appConfig['relationships'])) {
         $this->stdErr->writeln('No application relationships found.');
         return 1;
     }
     $util = new RelationshipsUtil($this->stdErr);
     $database = $util->chooseDatabase($sshUrl, $input);
     if (empty($database)) {
         $this->stdErr->writeln('No database selected.');
         return 1;
     }
     // Find the database's service name in the relationships.
     $dbServiceName = false;
     foreach ($appConfig['relationships'] as $relationshipName => $relationship) {
         if ($database['_relationship_name'] === $relationshipName) {
             list($dbServiceName, ) = explode(':', $relationship, 2);
             break;
         }
     }
     if (!$dbServiceName) {
         $this->stdErr->writeln('Service name not found for relationship: ' . $database['_relationship_name']);
         return 1;
     }
     // Load services yaml.
     $services = Yaml::parse(file_get_contents($projectRoot . '/.platform/services.yaml'));
     if (!empty($services[$dbServiceName]['disk'])) {
         $allocatedDisk = $services[$dbServiceName]['disk'];
     } else {
         $this->stdErr->writeln('The allocated disk size could not be determined for service: ' . $dbServiceName);
         return 1;
     }
     $this->stdErr->write('Querying database <comment>' . $dbServiceName . '</comment> to estimate disk usage. ');
     $this->stdErr->writeln('This might take a while.');
     /** @var ShellHelper $shellHelper */
     $shellHelper = $this->getHelper('shell');
     $command = ['ssh'];
     $command[] = $sshUrl;
     switch ($database['scheme']) {
         case 'pgsql':
             $command[] = $this->psqlQuery($database);
             $result = $shellHelper->execute($command);
             $resultArr = explode(PHP_EOL, $result);
             $estimatedUsage = array_sum($resultArr) / 1048576;
             break;
         default:
             $command[] = $this->mysqlQuery($database);
             $estimatedUsage = $shellHelper->execute($command);
             break;
     }
     $percentsUsed = $estimatedUsage * 100 / $allocatedDisk;
     $table = new Table($input, $output);
     $propertyNames = ['max', 'used', 'percent_used'];
     $machineReadable = $table->formatIsMachineReadable();
     $values = [(int) $allocatedDisk . ($machineReadable ? '' : 'MB'), (int) $estimatedUsage . ($machineReadable ? '' : 'MB'), (int) $percentsUsed . '%'];
     $table->renderSimple($values, $propertyNames);
     return 0;
 }