You are here

public static function UpgradeStatusForm::parseProject in Upgrade Status 8

Same name and namespace in other branches
  1. 8.3 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::parseProject()
  2. 8.2 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::parseProject()

Batch callback to analyse a project.

Parameters

\Drupal\Core\Extension\Extension $extension: The extension to analyse.

array $context: Batch context.

File

src/Form/UpgradeStatusForm.php, line 491

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

public static function parseProject(Extension $extension, &$context) {
  $context['message'] = t('Completed @project.', [
    '@project' => $extension
      ->getName(),
  ]);
  $error = $file_name = FALSE;

  // Prepare for a POST request to scan this project. The separate HTTP
  // request is used to separate any PHP errors found from this batch process.
  // We can store any errors and gracefully continue if there was any PHP
  // errors in parsing.
  $url = Url::fromRoute('upgrade_status.analyse', [
    'type' => $extension
      ->getType(),
    'project_machine_name' => $extension
      ->getName(),
  ]);

  // Pass over authentication information because access to this functionality
  // requires administrator privileges.

  /** @var \Drupal\Core\Session\SessionConfigurationInterface $session_config */
  $session_config = \Drupal::service('session_configuration');
  $request = \Drupal::request();
  $session_options = $session_config
    ->getOptions($request);

  // Unfortunately DrupalCI testbot does not have a domain that would normally
  // be considered valid for cookie setting, so we need to work around that
  // by manually setting the cookie domain in case there was none. What we
  // care about is we get actual results, and cookie on the host level should
  // suffice for that.
  $cookie_domain = empty($session_options['cookie_domain']) ? '.' . $request
    ->getHost() : $session_options['cookie_domain'];
  $cookie_jar = new CookieJar();
  $cookie = new SetCookie([
    'Name' => $session_options['name'],
    'Value' => $request->cookies
      ->get($session_options['name']),
    'Domain' => $cookie_domain,
    'Secure' => $session_options['cookie_secure'],
  ]);
  $cookie_jar
    ->setCookie($cookie);
  $options = [
    'cookies' => $cookie_jar,
    'timeout' => 0,
  ];

  // Try a POST request with the session cookie included. We expect valid JSON
  // back. In case there was a PHP error before that, we log that.
  try {
    $response = \Drupal::httpClient()
      ->post($url
      ->setAbsolute()
      ->toString(), $options);
    $data = json_decode((string) $response
      ->getBody(), TRUE);
    if (!$data) {
      $error = (string) $response
        ->getBody();
      $file_name = 'PHP Fatal Error';
    }
  } catch (\Exception $e) {
    $error = $e
      ->getMessage();
    $file_name = 'Scanning exception';
  }
  if ($error !== FALSE) {

    /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value */
    $key_value = \Drupal::service('keyvalue')
      ->get('upgrade_status_scan_results');
    $result = [];
    $result['date'] = REQUEST_TIME;
    $result['data'] = [
      'totals' => [
        'errors' => 1,
        'file_errors' => 1,
        'upgrade_status_split' => [
          'warning' => 1,
        ],
      ],
      'files' => [],
    ];
    $result['data']['files'][$file_name] = [
      'errors' => 1,
      'messages' => [
        [
          'message' => $error,
          'line' => 0,
        ],
      ],
    ];
    $key_value
      ->set($extension
      ->getName(), json_encode($result));
  }
}