You are here

public static function Utility::getServerFiles in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::getServerFiles()
  2. 8.2 src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::getServerFiles()
  3. 4.x src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::getServerFiles()

Retrieves a list of all config files of a server's Solr backend.

Parameters

\Drupal\search_api\ServerInterface $server: The Solr server whose files should be retrieved.

string $dir_name: (optional) The directory that should be searched for files. Defaults to the root config directory.

Return value

array An associative array of all config files in the given directory. The keys are the file names, values are arrays with information about the file. The files are returned in alphabetical order and breadth-first.

Throws

\Drupal\search_api\SearchApiException If a problem occurred while retrieving the files.

2 calls to Utility::getServerFiles()
search_api_solr_requirements in ./search_api_solr.install
Implements hook_requirements().
SolrConfigForm::buildForm in src/Form/SolrConfigForm.php
Form constructor.

File

src/Utility/Utility.php, line 160

Class

Utility
Provides various helper functions for Solr backends.

Namespace

Drupal\search_api_solr\Utility

Code

public static function getServerFiles(ServerInterface $server, $dir_name = NULL) {

  /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
  $backend = $server
    ->getBackend();
  $response = $backend
    ->getSolrConnector()
    ->getFile($dir_name);

  // Search for directories and recursively merge directory files.
  $files_data = json_decode($response
    ->getBody(), TRUE);
  $files_list = $files_data['files'];
  $dir_length = strlen($dir_name) + 1;
  $result = [
    '' => [],
  ];
  foreach ($files_list as $file_name => $file_info) {

    // Annoyingly, Solr 4.7 changed the way the admin/file handler returns
    // the file names when listing directory contents: the returned name is
    // now only the base name, not the complete path from the config root
    // directory. We therefore have to check for this case.
    if ($dir_name && substr($file_name, 0, $dir_length) !== "{$dir_name}/") {
      $file_name = "{$dir_name}/" . $file_name;
    }
    if (empty($file_info['directory'])) {
      $result[''][$file_name] = $file_info;
    }
    else {
      $result[$file_name] = static::getServerFiles($server, $file_name);
    }
  }
  ksort($result);
  ksort($result['']);
  return array_reduce($result, 'array_merge', []);
}