You are here

public function AcquiaConnectorCommands::spiGet in Acquia Connector 8

Same name and namespace in other branches
  1. 8.2 src/Commands/AcquiaConnectorCommands.php \Drupal\acquia_connector\Commands\AcquiaConnectorCommands::spiGet()
  2. 3.x src/Commands/AcquiaConnectorCommands.php \Drupal\acquia_connector\Commands\AcquiaConnectorCommands::spiGet()

Output raw Acquia SPI data.

@option outfile Optional. A file to write data to in the current directory. If omitted Drush will output to stdout. @option format Optional. Format may be json, print_r, or var_dump. Defaults to print_r.

@command acquia:connector:spi-get

@usage acquia:connector:spi-get --format=json --outfile=spi.json Write JSON encoded SPI data to spi.json in current directory.

Parameters

array $options: An associative array of options whose values come from cli, aliases, config, etc.

File

src/Commands/AcquiaConnectorCommands.php, line 35

Class

AcquiaConnectorCommands
A Drush command file.

Namespace

Drupal\acquia_connector\Commands

Code

public function spiGet(array $options = [
  'outfile' => NULL,
  'format' => NULL,
]) {
  $raw_spi = $this
    ->drushSpiGet();
  switch ($options['format']) {
    case 'json':
      $spi = Json::encode($raw_spi);
      break;
    case 'var_dump':
    case 'var_export':
      $spi = var_export($raw_spi, TRUE);
      break;
    case 'print_r':
    default:
      $spi = print_r($raw_spi, TRUE);
      break;
  }
  if (!$options['outfile']) {
    $this->output
      ->writeln($spi);
    return;
  }
  $file = $options['outfile'];

  // Path is relative.
  if (strpos($file, DIRECTORY_SEPARATOR) !== 0) {
    $file = ($_SERVER['PWD'] ?? getcwd()) . DIRECTORY_SEPARATOR . $file;
  }
  if (file_put_contents($file, $spi)) {
    $this->logger
      ->info('SPI Data written to @outfile.', [
      '@outfile' => realpath($file),
    ]);
  }
  else {
    $this->logger
      ->error('Unable to write SPI Data into @outfile.', [
      '@outfile' => realpath($file),
    ]);
  }
}