You are here

class JsonQueryMapQueryProvider in GraphQL 8.3

Hierarchy

Expanded class hierarchy of JsonQueryMapQueryProvider

1 string reference to 'JsonQueryMapQueryProvider'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses JsonQueryMapQueryProvider
graphql.query_provider.query_map.json in ./graphql.services.yml
Drupal\graphql\GraphQL\QueryProvider\JsonQueryMapQueryProvider

File

src/GraphQL/QueryProvider/JsonQueryMapQueryProvider.php, line 10

Namespace

Drupal\graphql\GraphQL\QueryProvider
View source
class JsonQueryMapQueryProvider implements QueryProviderInterface {

  /**
   * The cache backend for storing query map file paths.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackend;

  /**
   * The paths to use for finding query maps.
   *
   * @var string[]
   */
  protected $lookupPaths;

  /**
   * QueryProvider constructor.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
   *   The cache backend for storing query map file paths.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   */
  public function __construct(CacheBackendInterface $cacheBackend, ConfigFactoryInterface $configFactory) {
    $this->lookupPaths = $configFactory
      ->get('graphql.query_map_json.config')
      ->get('lookup_paths') ?: [];
    $this->cacheBackend = $cacheBackend;
  }

  /**
   * {@inheritdoc}
   */
  public function getQuery($id, OperationParams $operation) {
    list($version, $id) = explode(':', $id);

    // Check that the id is properly formatted.
    if (empty($version) || empty($id)) {
      return NULL;
    }
    if (!(($cache = $this->cacheBackend
      ->get('graphql_query_map_json_versions')) && ($versions = $cache->data) !== NULL)) {
      $this->cacheBackend
        ->set('graphql_query_map_json_versions', $versions = $this
        ->discoverQueryMaps());
    }
    if (isset($versions) && isset($versions[$version]) && file_exists($versions[$version])) {
      $contents = json_decode(file_get_contents($versions[$version]), TRUE);
      if ($query = array_search($id, $contents)) {
        return $query;
      }
    }
    return NULL;
  }

  /**
   * Discovers the available query maps within the configured lookup paths.
   *
   * @return array
   *   An associative array of query maps with the query map versions as keys.
   */
  protected function discoverQueryMaps() {
    $maps = [];
    foreach ($this->lookupPaths as $path) {
      if (is_dir($path)) {
        $iterator = new RegexDirectoryIterator($path, '/\\.json/i');

        /** @var \SplFileInfo $file */
        foreach ($iterator as $file) {
          $hash = sha1(file_get_contents($file
            ->getPathname()));
          $maps[$hash] = $file
            ->getPathname();
        }
      }
      if (is_file($path)) {
        $file = new \SplFileInfo($path);
        $hash = sha1(file_get_contents($file
          ->getPathname()));
        $maps[$hash] = $file
          ->getPathname();
      }
    }
    return $maps;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonQueryMapQueryProvider::$cacheBackend protected property The cache backend for storing query map file paths.
JsonQueryMapQueryProvider::$lookupPaths protected property The paths to use for finding query maps.
JsonQueryMapQueryProvider::discoverQueryMaps protected function Discovers the available query maps within the configured lookup paths.
JsonQueryMapQueryProvider::getQuery public function Returns a query string given the query parameters. Overrides QueryProviderInterface::getQuery
JsonQueryMapQueryProvider::__construct public function QueryProvider constructor.