Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/dds_global_content / src / GlobalContentBuilder.php
Size: Mime:
<?php

namespace Drupal\dds_global_content;

use Drupal\Component\Discovery\YamlDiscovery;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;

class GlobalContentBuilder {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  protected $themeHandler;

  protected $connection;

  protected $manager;

  protected $storage;

  protected $logger;

  static protected $types;

  /**
   * Constructs the RouteBuilder using the passed MatcherDumperInterface.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler
   * @param \Drupal\Core\Database\Connection $connection
   * @param GlobalContentManager $manager
   * @param LoggerChannelFactoryInterface $loggerChannelFactory
   * @param \Drupal\dds_global_content\GlobalContentStorage $storage
   */
  public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $themeHandler, Connection $connection, GlobalContentManager $manager, LoggerChannelFactoryInterface $loggerChannelFactory, GlobalContentStorage $storage) {
    $this->moduleHandler = $module_handler;
    $this->themeHandler = $themeHandler;
    $this->connection = $connection;
    $this->manager = $manager;
    $this->storage = $storage;
    $this->logger = $loggerChannelFactory->get('global_content');
  }

  /**
   * Retrieves all defined routes from .routing.yml files.
   *
   * @return array
   *   The defined routes, keyed by provider.
   */
  protected function getGlobalContentDefinitions() {
    // Always instantiate a new YamlDiscovery object so that we always search on
    // the up-to-date list of modules.
    $directories = array_merge($this->moduleHandler->getModuleDirectories(), $this->themeHandler->getThemeDirectories());
    $discovery = new YamlDiscovery('globalcontent', $directories);
    $discoveries = $discovery->findAll();

    \Drupal::moduleHandler()->alter('global_content', $discoveries);

    return $discoveries;
  }

  /**
   * Rebuild the  data in the database based on data retrieved from
   * the *.global_content.yml files
   *
   * @throws \Drupal\dds_global_content\GlobalContentStorageException
   */
  public function rebuild() {
    $language_manager = \Drupal::languageManager();
    $new_names = [];
    $machine_names = $this->storage->getMachineNames();
    $definitions = $this->getGlobalContentDefinitions();

    foreach ($definitions as $module => $items) {
      foreach ($items as $machine_name => $data) {
        if (!$this->typeExists($data['type'])) {
          continue;
        }

        if (empty($data['category'])) {
          $data['category'] = 'Miscellaneous';
        }
        if (empty($data['group'])) {
          $data['group'] = $data['category'];
          $data['category'] = 'Miscellaneous';
          if (isset($data['subcategory'])) {
            $data['subgroup'] = 'subcategory';
            unset($data['subcategory']);
          }
        }
        if (empty($data['description'])) {
          $data['description'] = '';
        }

        $new_names[] = $machine_name;
        $global_content = $this->storage->createGlobalContent([
          'machine_name' => $machine_name,
          'type' => $data['type'],
          'title' => $data['title'],
          'description' => $data['description'],
          'category' => $data['category'],
          'group_name' => $data['group'],
        ]);

        if (in_array($machine_name, $machine_names)) {
          $global_content->setIsNew(FALSE);
        } else {
          $machine_names[] = $machine_name;
        }

        if (isset($data['default'])) {
          $global_content->setDefault($data['default']);
        } else {
          $global_content->setDefault([
            $language_manager->getDefaultLanguage()->getId() => ''
          ]);
        }

        if (!isset($data['langcode']) || empty($data['langcode'])) {
          $data['langcode'] = $language_manager->getDefaultLanguage()->getId();
        }

        if (isset($data['langcode'])) {
          $global_content->setLangcode($data['langcode']);
        }

        if (isset($data['options']) && is_array($data['options'])) {
          $global_content->setOptions($data['options']);
        }

        if (isset($data['subgroup'])) {
          $global_content->setSubgroupName($data['subgroup']);
        }

        if (isset($data['weight'])) {
          $global_content->setWeight(intval($data['weight']));
        }

        $global_content->rebuild();
      }
    }

    $forgotten_names = array_diff($machine_names, $new_names);
    if (!empty($forgotten_names)) {
      foreach ($forgotten_names as $machine_name) {
        $this->storage->delete($machine_name);
      }
    }
  }

  private function typeExists($type) {
    if (empty(self::$types)) {
      $definitions = $this->manager->getDefinitions();
      foreach ($definitions as $key => $definition) {
        foreach ($definition['types'] as $type) {
          self::$types[$type] = $key;
        }
      }
    }

    return isset(self::$types[$type]);
  }

  private function prepareRebuild(): void {
    $this->connection->truncate('global_content')->execute();
  }

}