sitemap_status-1.0.0-alpha4/src/Commands/SitemapStatusCommands.php

src/Commands/SitemapStatusCommands.php
<?php

namespace Drupal\sitemap_status\Commands;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\sitemap_status\SitemapStatus;
use Drupal\sitemap_status\SitemapStatusInterface;
use Drush\Commands\DrushCommands;

/**
 * Class SitemapStatusCommands.
 *
 * @package Drupal\sitemap_status\Commands
 */
class SitemapStatusCommands extends DrushCommands {

  use StringTranslationTrait;

  /**
   * @var \Drupal\sitemap_status\SitemapStatus
   */
  protected $sitemapStatus;

  /**
   * SitemapStatusCommands constructor.
   *
   * @param \Drupal\simple_sitemap\Simplesitemap $sitemap_status
   */
  public function __construct(SitemapStatus $sitemap_status) {
    $this->sitemapStatus = $sitemap_status;
  }

  /**
   * Checks the locations statuses from a sitemap.
   *
   * @command sitemap-status:check
   *
   * @usage drush sitemap-status:check
   *   Checks the sitemap locations http status.
   *
   * @usage drush sitemap-status:check --errors=true
   *   By default, only the http status is checked.
   *   Optionally check if a Drupal error is being displayed.
   *   Defaults to '.messages--error' or get extra classes from the configuration.
   *
   * @usage drush sitemap-status:check --elements=true
   *   By default, only the http status is checked.
   *   Optionally check if the page contains elements
   *   Defaults to '<h1>' or get extra elements from the configuration.
   *
   * @usage drush sitemap-status:check --basic-auth=username:password
   *   Appends basic auth credentials to the locations status check.
   *   It is not uncommon to have pull request environments behind basic auth and
   *   sometimes, it also applies to the CLI.
   *
   * @usage drush sitemap-status:check --path-prefix=europe
   *   When a path prefix is being used. Example: a specific directory or a region based redirect
   *   to be able to distinguish other 301 redirects.
   *
   * @usage drush sitemap-status:check --async=true
   *   Fetches location status asynchronously, faster but uses more resources.
   *
   * @usage drush sitemap-status:check --concurrent-requests=10
   *   Maximum amount of concurrent requests while using async.
   *
   * @usage drush sitemap-status:check --excluded-urls=https://mysite.org,https://nginx-production.mysite.org
   *   Do not run the locations check for these environments.
   *   Without a trailing slash and with the scheme. Comma separated.
   *
   * @usage drush sitemap-status:check --stop-on-error=true
   *   Stops the status check on the first error met.
   *
   * @validate-module-enabled sitemap_status
   *
   * @aliases ssc, sitemap-status-check
   */
  public function check(array $options = [
    'errors' => '',
    'elements' => '',
    'path-prefix' => '',
    'basic-auth' => '',
    'async' => '',
    'concurrent-requests' => '',
    'excluded-urls' => '',
    'stop-on-error' => '',
  ]) {
    // Using state to set the options from Drush.
    // Passing the parameters to check locations won't work in this case
    // as we want to override the default configuration via Drush options.
    // The configuration is being used by several contexts (e.g. queue workers)
    // that will not be aware of these overrides then.
    // State has the benefit to leave the configuration unchanged.
    $configurationOverride = [];

    // @todo dependency injection.
    $messenger = \Drupal::messenger();
    $currentHost = \Drupal::request()->getSchemeAndHttpHost();
    $state = \Drupal::state();

    $excludedHosts = array_map('trim', array_filter(explode(',', $options['excluded-urls'])));
    // Possibly strip scheme.
    if (!empty($excludedHosts) && in_array($currentHost, $excludedHosts)) {
      $messenger->addMessage($this->t('The current host @current_host is excluded, skipping locations checks.', [
        '@current_host' => $currentHost,
      ]));
    }

    if (!empty($options['errors']) && $options['errors'] === 'true') {
      $configurationOverride['has_error_check'] = TRUE;
    }
    else {
      $configurationOverride['has_error_check'] = FALSE;
    }

    if (!empty($options['elements']) && $options['elements'] === 'true') {
      $configurationOverride['has_element_check'] = TRUE;
    }
    else {
      $configurationOverride['has_element_check'] = FALSE;
    }

    if (!empty($options['basic-auth'])) {
      $configurationOverride['basic_auth'] = $options['basic-auth'];
    }
    else {
      $configurationOverride['basic_auth'] = '';
    }

    if (!empty($options['path-prefix'])) {
      $configurationOverride['path_prefix'] = $options['path-prefix'];
    }
    else {
      $configurationOverride['path_prefix'] = '';
    }

    if (!empty($options['async']) && $options['async'] === 'true') {
      $configurationOverride['async'] = TRUE;
      if (!empty($options['concurrent-requests']) && is_numeric($options['concurrent-requests'])) {
        $configurationOverride['concurrent_requests'] = (int) $options['concurrent-requests'];
      }
    }
    else {
      $configurationOverride['async'] = FALSE;
    }

    if (!empty($options['stop-on-error']) && $options['stop-on-error'] === 'true') {
      $configurationOverride['stop_on_error'] = TRUE;
    }
    else {
      $configurationOverride['stop_on_error'] = FALSE;
    }

    $state->set(SitemapStatusInterface::STATE_CONFIG_OVERRIDE, $configurationOverride);
    $this->sitemapStatus->overridePropertiesWithState();
    $this->sitemapStatus->checkLocations();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc