You are here

class DebuggingReviewForm in Lightweight Directory Access Protocol (LDAP) 8.4

Form to allow for debugging review.

Hierarchy

Expanded class hierarchy of DebuggingReviewForm

1 string reference to 'DebuggingReviewForm'
ldap_servers.routing.yml in ldap_servers/ldap_servers.routing.yml
ldap_servers/ldap_servers.routing.yml

File

ldap_servers/src/Form/DebuggingReviewForm.php, line 19

Namespace

Drupal\ldap_servers\Form
View source
class DebuggingReviewForm extends FormBase {

  /**
   * Config Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * Module Handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function getFormId() : string {
    return 'ldap_servers_debugging_review';
  }

  /**
   * Class constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   Module handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandler $module_handler, EntityTypeManagerInterface $entity_type_manager) {
    $this->config = $config_factory;
    $this->moduleHandler = $module_handler;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : DebuggingReviewForm {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('entity_type.manager'));
  }

  /**
   * Returns raw data of configuration.
   *
   * @param string $configName
   *   Configuration name.
   *
   * @return string
   *   Raw configuration data.
   */
  private function printConfig(string $configName) : string {
    return '<pre>' . Yaml::encode($this
      ->config($configName)
      ->getRawData()) . '</pre>';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) : array {
    $form['title'] = [
      '#markup' => '<h1>' . $this
        ->t('LDAP Debugging Review') . '</h1>',
    ];
    if (!extension_loaded('ldap')) {
      $this
        ->messenger()
        ->addError($this
        ->t('PHP LDAP extension not loaded.'));
    }
    else {
      $form['heading_modules'] = [
        '#markup' => '<h2>' . $this
          ->t('PHP LDAP module') . '</h2>',
      ];
      $form['modules'] = [
        '#markup' => '<pre>' . Yaml::encode($this
          ->parsePhpModules()['ldap']) . '</pre>',
      ];
    }
    $form['heading_ldap'] = [
      '#markup' => '<h2>' . $this
        ->t('Drupal LDAP modules') . '</h2>',
    ];
    if ($this->moduleHandler
      ->moduleExists('ldap_user')) {
      $form['config_users'] = [
        '#markup' => '<h3>' . $this
          ->t('The LDAP user configuration') . '</h3>' . $this
          ->printConfig('ldap_user.settings'),
      ];
    }
    $user_register = $this
      ->config('user.settings')
      ->get('register');
    $form['config_users_registration'] = [
      '#markup' => $this
        ->t('Currently active Drupal user registration setting: @setting', [
        '@setting' => $user_register,
      ]),
    ];
    if ($this->moduleHandler
      ->moduleExists('ldap_authentication')) {
      $form['config_authentication'] = [
        '#markup' => '<h3>' . $this
          ->t('The LDAP authentication configuration') . '</h3>' . $this
          ->printConfig('ldap_authentication.settings'),
      ];
    }
    $form['config_help'] = [
      '#markup' => '<h3>' . $this
        ->t('The LDAP help configuration') . '</h3>' . $this
        ->printConfig('ldap_servers.settings'),
    ];
    $form['heading_servers'] = [
      '#markup' => '<h2>' . $this
        ->t('Drupal LDAP servers') . '</h2>',
    ];
    $storage = $this->entityTypeManager
      ->getStorage('ldap_server');
    $servers = $storage
      ->getQuery()
      ->execute();
    foreach ($storage
      ->loadMultiple($servers) as $sid => $server) {

      /** @var \Drupal\ldap_servers\Entity\Server $server */
      $form['config_server_' . $sid] = [
        '#markup' => '<h3>' . $this
          ->t('Server @name:', [
          '@name' => $server
            ->label(),
        ]) . '</h3>' . $this
          ->printConfig('ldap_servers.server.' . $sid),
      ];
    }
    if ($this->moduleHandler
      ->moduleExists('authorization') && $this->moduleHandler
      ->moduleExists('ldap_authorization')) {
      $form['heading_profiles'] = [
        '#markup' => '<h2>' . $this
          ->t('Configured authorization profiles') . '</h2>',
      ];
      $profiles = $this->entityTypeManager
        ->getStorage('authorization_profile')
        ->getQuery()
        ->execute();
      foreach ($profiles as $profile) {
        $form['authorization_profile_' . $profile] = [
          '#markup' => '<h3>' . $this
            ->t('Profile @name:', [
            '@name' => $profile,
          ]) . '</h3>' . $this
            ->printConfig('authorization.authorization_profile.' . $profile),
        ];
      }
    }
    if ($this->moduleHandler
      ->moduleExists('ldap_query')) {
      $form['heading_queries'] = [
        '#markup' => '<h2>' . $this
          ->t('Configured LDAP queries') . '</h2>',
      ];
      $queries_found = $this->entityTypeManager
        ->getStorage('ldap_query_entity')
        ->getQuery()
        ->execute();
      foreach ($this->entityTypeManager
        ->getStorage('ldap_query_entity')
        ->loadMultiple($queries_found) as $query) {

        /** @var \Drupal\ldap_query\Entity\QueryEntity $query */
        $form['query_' . $query
          ->id()] = [
          '#markup' => '<h3>' . $this
            ->t('Query @name:', [
            '@name' => $query
              ->label(),
          ]) . '</h3>' . $this
            ->printConfig('ldap_query.ldap_query_entity.' . $query
            ->id()),
        ];
      }
    }
    return $form;
  }

  /**
   * Generates an array of values from phpinfo().
   *
   * @return array
   *   Module list.
   */
  private function parsePhpModules() : array {
    ob_start();
    phpinfo(INFO_MODULES);
    $output = ob_get_clean();
    $output = strip_tags($output, '<h2><th><td>');
    $output = preg_replace('/<th[^>]*>([^<]+)<\\/th>/', "<info>\\1</info>", $output);
    $output = preg_replace('/<td[^>]*>([^<]+)<\\/td>/', "<info>\\1</info>", $output);

    /** @var string[] $rows */
    $rows = preg_split('/(<h2>[^<]+<\\/h2>)/', $output, -1, PREG_SPLIT_DELIM_CAPTURE);
    $modules = [];
    if (is_array($rows)) {
      $rowCount = count($rows);

      // First line with CSS can be ignored.
      for ($i = 1; $i < $rowCount - 1; $i++) {
        $this
          ->extractModule($rows[$i], $rows[$i + 1], $modules);
      }
    }
    return $modules;
  }

  /**
   * Extract module information.
   *
   * @param string $row
   *   Row.
   * @param string $nextRow
   *   Next row.
   * @param array $modules
   *   Extracted modules data.
   */
  private function extractModule(string $row, string $nextRow, array &$modules) : void {
    if (preg_match('/<h2>([^<]+)<\\/h2>/', $row, $headingMatches)) {
      $moduleName = trim($headingMatches[1]);
      $moduleInfos = explode("\n", $nextRow);
      foreach ($moduleInfos as $info) {
        $infoPattern = '<info>([^<]+)<\\/info>';

        // 3 columns.
        if (preg_match("/{$infoPattern}\\s*{$infoPattern}\\s*{$infoPattern}/", $info, $infoMatches)) {
          $modules[$moduleName][trim($infoMatches[1])] = [
            trim($infoMatches[2]),
            trim($infoMatches[3]),
          ];
        }
        elseif (preg_match("/{$infoPattern}\\s*{$infoPattern}/", $info, $infoMatches)) {
          $modules[$moduleName][trim($infoMatches[1])] = trim($infoMatches[2]);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) : void {

    // Nothing to submit.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DebuggingReviewForm::$config protected property Config Factory.
DebuggingReviewForm::$entityTypeManager protected property Entity type manager.
DebuggingReviewForm::$moduleHandler protected property Module Handler.
DebuggingReviewForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
DebuggingReviewForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
DebuggingReviewForm::extractModule private function Extract module information.
DebuggingReviewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DebuggingReviewForm::parsePhpModules private function Generates an array of values from phpinfo().
DebuggingReviewForm::printConfig private function Returns raw data of configuration.
DebuggingReviewForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DebuggingReviewForm::__construct public function Class constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.