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_dynamic_key_value / src / DynamicKeyValueStorage.php
Size: Mime:
<?php
namespace Drupal\dds_dynamic_key_value;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueFactory;
use Drupal\Component\Discovery\YamlDiscovery;

/**
 * Class DynamicKeyValueStorage.
 *
 * @package Drupal\dds_dynamic_key_value
 */
class DynamicKeyValueStorage {

  /**
   * Variable holding the key-value factory.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueFactory
   */
  private $keyValue;

  /**
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  private $themeHandler;

  /**
   * YamlConfigStorage constructor.
   *
   * @param \Drupal\Core\KeyValueStore\KeyValueFactory $keyValueFactory
   *   The key value factory.
   */
  public function __construct(KeyValueFactory $keyValueFactory, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
    $this->keyValue = $keyValueFactory;
    $this->moduleHandler = $module_handler;
    $this->themeHandler = $theme_handler;
  }

  /**
   * Save the configuration into the key-value store.
   *
   * @param array $values
   *   The values to save.
   */
  public function save(array $values): void {
    foreach ($values as $key => $value) {
      $this->keyValue->get('dynamic_key_value')->set($key, $value);
    }
  }

  /**
   * The the complete configuration for key.
   *
   * @param string $key
   *   The key to get the config for.
   *
   * @return array
   *   The complete configuration for the provided key.
   */
  public function getCollection(string $key): array {
    /** if collection cannot be found, try rebuilding the cache first */
    if (!$this->keyValue->get('dynamic_key_value')->has($key)) {
      $this->rebuild();
    }
    $collection =  $this->keyValue->get('dynamic_key_value')->get($key);
    return $collection ?? [];
  }

  /**
   * Get a specific configuration.
   *
   * @param string $key
   *   The key to get the config from.
   * @param string $name
   *   The name of the config to retrieve.
   *
   * @return mixed|null
   *   The value of the config.
   */
  public function get(string $key, string $name) {
    $configs = $this->getCollection($key);
    return $configs[$name] ?? NULL;
  }

  /**
   * Get the names of the collections.
   *
   * @return array
   *   Array containing the names of all the collections.
   */
  public function getCollectionLabels(): array {
    $collections = $this->keyValue->get('dynamic_key_value')->getAll();
    if (empty($collections)) {
      $this->rebuild();
      $collections = $this->keyValue->get('dynamic_key_value')->getAll();
    }

    if (!empty($collections)) {
      $keys = array_keys($collections);
      return array_combine($keys, $keys);
    }

    return [];
  }

  /**
   * Retrieve config as key-value for use in ex. select field.
   *
   * @param $key
   *   The key of the collection to retrieve.
   *
   * @param string $property
   *   Name of property to use as option
   *
   * @return array
   *  Array containing the options names
   */
  public function getOptions($key, $property = 'name'): array {

    if (is_string($key)) {
      $collection = $this->getCollection($key);

      $config = [];
      if (!empty($collection)) {
        foreach ($collection as $collection_key => $value) {
          if (isset($value[$property])) {
            $config[$collection_key] = $value[$property];
          }
        }
      }

      return $config;
    }

    return [];
  }

  /**
   * Get config that needs to bee exposed through drupalSettings.
   *
   * @param $key
   *   The key of the collection to retrieve.
   *
   * @return array
   *   Array containing the settings that needs to be exposed to js.
   */
  public function getJSOptions($key) : array {

    if (is_string($key)) {
      $collection = $this->getCollection($key);

      $config = [];
      if (!empty($collection)) {
        foreach ($collection as $collection_key => $value) {
          if(isset($value['js']) && $value['js'] === TRUE) {
            unset($value['js']);
            $config[$collection_key] = $value;
          }
        }
      }

      return $config;
    }

    return [];
  }

  /**
   *  Rebuild the definitions cache
   */
  public function rebuild() {
    $keyValueDefinitionsDefinitions = $this->getKeyValueDefinitions();
    $config_definitions = reset($keyValueDefinitionsDefinitions);

    if(!empty($config_definitions)) {
      $this->save($config_definitions);
    }
  }

  /**
   * Get the definitions
   * @return array
   */
  public function getKeyValueDefinitions() {
    // 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('key_value', $directories);
    return $discovery->findAll();
  }

}