You are here

class AcquiaConnectorCommands in Acquia Connector 8

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

A Drush command file.

Drush integration for the Acquia Connector module.

Hierarchy

Expanded class hierarchy of AcquiaConnectorCommands

1 string reference to 'AcquiaConnectorCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses AcquiaConnectorCommands
acquia_connector.commands in ./drush.services.yml
Drupal\acquia_connector\Commands\AcquiaConnectorCommands

File

src/Commands/AcquiaConnectorCommands.php, line 15

Namespace

Drupal\acquia_connector\Commands
View source
class AcquiaConnectorCommands extends DrushCommands {

  /**
   * Output raw Acquia SPI data.
   *
   * @param array $options
   *   An associative array of options whose values come from cli, aliases,
   *   config, etc.
   *
   * @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.
   */
  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),
      ]);
    }
  }

  /**
   * A command callback and drush wrapper for custom test validation.
   *
   * @command acquia:connector:spi-test-validate
   *
   * @aliases acquia:connector:spi-tv
   *
   * @usage acquia:connector:spi-test-validate
   *   Perform a validation check on any modules with Acquia SPI custom tests.
   *
   * @validate-module-enabled acquia_connector
   */
  public function customTestValidate() {
    $modules = \Drupal::moduleHandler()
      ->getImplementations('acquia_connector_spi_test');
    if (!$modules) {
      $this->output
        ->writeln((string) dt('No Acquia SPI custom tests were detected.'));
      return;
    }
    $this->output
      ->writeln((string) dt('Acquia SPI custom tests were detected in: @modules ' . PHP_EOL, [
      '@modules' => implode(', ', $modules),
    ]));
    $pass = [];
    $failure = [];
    foreach ($modules as $module) {
      $function = $module . '_acquia_connector_spi_test';
      if (!function_exists($function)) {
        continue;
      }
      $testStatus = new TestStatusController();
      $result = $testStatus
        ->testValidate($function());
      if (!$result['result']) {
        $failure[] = $module;
        $this->output
          ->writeln((string) dt("[FAILED]  Validation failed for '@module' and has been logged.", [
          '@module' => $module,
        ]));
        foreach ($result['failure'] as $test_name => $test_failures) {
          foreach ($test_failures as $test_param => $test_value) {
            $variables = [
              '@module_name' => $module,
              '@message' => $test_value['message'],
              '@param_name' => $test_param,
              '@test_name' => $test_name,
              '@value' => $test_value['value'],
            ];
            $this->output
              ->writeln((string) dt("[DETAILS] @message for parameter '@param_name'; current value '@value'. (Test @test_name in module @module_name)", $variables));
            $this->logger
              ->error("<em>Custom test validation failed</em>: @message for parameter '@param_name'; current value '@value'. (<em>Test '@test_name' in module '@module_name'</em>)", $variables);
          }
        }
      }
      else {
        $pass[] = $module;
        $this->output
          ->writeln((string) dt("[PASSED]  Validation passed for '@module.'", [
          '@module' => $module,
        ]));
      }
      $this->output
        ->writeln('');
    }
    $this->output
      ->writeln((string) dt('Validation checks completed.'));
    $variables = [];
    if (count($pass) > 0) {
      $variables['@passes'] = implode(', ', $pass);
      $variables['@pass_count'] = count($pass);
      $this->output
        ->writeln((string) dt('@pass_count module(s) passed validation: @passes.'), $variables);
    }
    if (count($failure) > 0) {
      $variables['@failures'] = implode(', ', $failure);
      $variables['@fail_count'] = count($failure);
      $this->output
        ->writeln((string) dt('@fail_count module(s) failed validation: @failures.'), $variables);
    }
  }

  /**
   * A command to send Acquia SPI data.
   *
   * @command acquia:connector:spi-send
   *
   * @usage drush -l <host_uri> acquia:connector:spi-send
   *   Sends Acquia SPI data.
   */
  public function spiSend() {
    $config = \Drupal::config('acquia_connector.settings');
    $state_site_name = \Drupal::state()
      ->get('spi.site_name');
    $state_site_machine_name = \Drupal::state()
      ->get('spi.site_machine_name');

    // Don't send data if site is blocked or missing components.
    if ($config
      ->get('spi.blocked') || is_null($state_site_name) && is_null($state_site_machine_name)) {
      $this->logger
        ->error('Site is blocked or missing components.');
      return;
    }
    $response = \Drupal::service('acquia_connector.spi')
      ->sendFullSpi(ACQUIA_CONNECTOR_ACQUIA_SPI_METHOD_DRUSH);
    if ($response && isset($response['is_error']) && $response['is_error']) {
      $this->logger
        ->error('Failed to send SPI data.');
    }
  }

  /**
   * Helper method to include acquia_connector.module file.
   */
  protected function drushSpiGet() {
    global $conf;
    $conf['acquia_connector.settings']['spi']['ssl_verify'] = FALSE;
    $conf['acquia_connector.settings']['spi']['ssl_override'] = TRUE;
    $client = \Drupal::service('acquia_connector.client');
    $config = \Drupal::service('config.factory');
    $spi = new SpiController($client, $config);
    return $spi
      ->get();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaConnectorCommands::customTestValidate public function A command callback and drush wrapper for custom test validation.
AcquiaConnectorCommands::drushSpiGet protected function Helper method to include acquia_connector.module file.
AcquiaConnectorCommands::spiGet public function Output raw Acquia SPI data.
AcquiaConnectorCommands::spiSend public function A command to send Acquia SPI data.