drupalorg-1.0.x-dev/src/Utilities/ComposerNamespace.php

src/Utilities/ComposerNamespace.php
<?php

namespace Drupal\drupalorg\Utilities;

use Drupal\drupalorg\Traits\UpdateXML;
use Drupal\node\Entity\Node;

/**
 * Utility functions to calculate and populate the composer namespace values.
 */
class ComposerNamespace {

  use UpdateXML;

  /**
   * Calculate the composer namespace for the projects.
   */
  public static function calculateNamespace($set_default_if_none_found = TRUE) {
    $memory_cache = \Drupal::service('entity.memory_cache');
    $memory_cache_clear_counter = 0;
    $ids = self::getProjectsWithNoComposerNamespace();
    foreach ($ids as $id) {
      $updated = FALSE;
      $project = Node::load($id);
      $machine_name = $project->get('field_project_machine_name')->getValue()[0]['value'] ?? NULL;
      $info = $machine_name ? self::getProjectInformation($machine_name) : NULL;
      if (!empty($info)) {
        $composer_namespace = $info['composer_namespace'] ?? NULL;
        if ($composer_namespace) {
          $updated = TRUE;
          $project
            ->set('field_composer_namespace', $composer_namespace)
            ->save();
        }
      }

      if ($set_default_if_none_found && !$updated && $machine_name) {
        $project
          ->set('field_composer_namespace', 'drupal/' . $machine_name)
          ->save();
      }

      // Reset the cache in order to free memory as we progress.
      $memory_cache_clear_counter++;
      if ($memory_cache_clear_counter > 100) {
        $memory_cache->deleteAll();
        $memory_cache_clear_counter = 0;
      }
    }
  }

  /**
   * Get projects with no composer namespace.
   *
   * @return array
   *   List of projects that match the condition.
   */
  protected static function getProjectsWithNoComposerNamespace(): array {
    return \Drupal::entityQuery('node')
      ->accessCheck(FALSE)
      ->condition('type', 'project_module')
      ->notExists('field_composer_namespace')
      ->execute();
  }

}

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

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