You are here

function drush_site_audit_audit_all in Site Audit 8.2

Same name and namespace in other branches
  1. 7 site_audit.drush.inc \drush_site_audit_audit_all()

Execute every single report.

File

./site_audit.drush.inc, line 491
Drupal site auditing commands.

Code

function drush_site_audit_audit_all($url = '', $key = '') {
  $reports_to_skip = array();
  if (drush_get_option('skip')) {
    $reports_to_skip = explode(',', drush_get_option('skip'));
  }
  $command = drush_get_command();
  drush_command_invoke_all_ref('drush_command_alter', $command);
  $report_names = $command['reports'];
  if (!empty($report_names)) {
    $reports_to_render = array();
    foreach ($report_names as $report_name) {
      if (is_array($report_name)) {
        require_once $report_name['location'];
        $report_name = $report_name['name'];
      }
      $report_class = 'SiteAuditReport' . $report_name;

      // Allow a specific report to be skipped.
      if (!in_array(strtolower($report_name), $reports_to_skip)) {

        // Special case for arguments.
        if ($report_name == 'FrontEnd') {
          $report = new $report_class($url, $key);
        }
        else {
          $report = new $report_class();
        }
        $reports_to_render[get_class($report)] = $report;
      }
    }
    if (empty($reports_to_render)) {
      return drush_set_error('SITE_AUDIT_NO_REPORTS', dt('No reports are available!'));
    }
    if (drush_get_option('bootstrap')) {
      echo file_get_contents(SITE_AUDIT_BASE_PATH . '/html/header.html');
    }

    // Header.
    $report_time = dt('Generated on @time', array(
      '@time' => date('r'),
    ));

    // Prepare JSON format.
    if (drush_get_option('json')) {
      $report_all = array(
        'time' => time(),
        'reports' => array(),
      );
    }
    elseif (drush_get_option('html')) {
      $site_audit_url = Url::fromUri('https://drupal.org/project/site_audit');
      $report_title = \Drupal::l(dt('Site Audit'), $site_audit_url);
      if ($url) {
        $report_title .= ' ' . dt('report for !url', array(
          '!url' => Url::fromUri($url),
        ));
      }
      echo '<div class="page-header">';
      echo '<h1>' . $report_title . '<br/><small>' . $report_time . '</small></h1>';
      echo '</div>';
    }
    else {
      if ($url) {
        drush_print(dt('https://drupal.org/project/site_audit report for @url', array(
          '@url' => $url,
        )));
      }
      else {
        drush_print(dt('https://drupal.org/project/site_audit report'));
      }
      drush_print($report_time);
      drush_print();
    }

    // Table of Contents; HTML only.
    if (drush_get_option('html') && !drush_get_option('json')) {
      echo '<h2 id="top">' . dt('Summary') . '</h2>';
      $buttons = array();
      foreach ($reports_to_render as $report) {
        $button_text = $report
          ->getLabel();
        if ($report
          ->getPercent() != SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO) {
          $button_text .= ' (' . $report
            ->getPercent() . '%)';
        }
        else {
          $button_text .= ' (' . dt('Info') . ')';
        }
        $buttons[] = \Drupal::l($button_text, Url::fromUserInput('#'), array(
          'fragment' => get_class($report),
          'external' => TRUE,
          'attributes' => array(
            'class' => array(
              'btn',
              'btn-' . $report
                ->getPercentCssClass(),
              'btn-mini',
            ),
            'style' => 'margin-bottom: 5px;',
          ),
        ));
      }
      echo '<p>' . implode('&nbsp;', $buttons) . '</p>';
    }

    // Render reports.
    foreach ($reports_to_render as $report) {
      if (drush_get_option('json')) {
        $report_all['reports'][get_class($report)] = json_decode($report
          ->toJson());
      }
      else {
        $report
          ->render();
      }
      if (drush_get_option('html') && !drush_get_option('json')) {
        echo '<div>' . \Drupal::l(dt('Back to top'), Url::fromUserInput('#'), array(
          'fragment' => 'top',
          'external' => TRUE,
          'attributes' => array(
            'class' => array(
              'btn',
              'btn-default',
              'btn-mini',
            ),
          ),
        )) . '</div>';
      }
    }
    if (drush_get_option('bootstrap')) {
      echo file_get_contents(SITE_AUDIT_BASE_PATH . '/html/footer.html');
    }
  }
  if (drush_get_option('json')) {
    echo json_encode($report_all);
    return;
  }
}