You are here

protected function SubdivisionRepository::loadDefinitions in Address 8

File

src/Repository/SubdivisionRepository.php, line 53

Class

SubdivisionRepository
Provides subdivisions.

Namespace

Drupal\address\Repository

Code

protected function loadDefinitions(array $parents) {
  $group = $this
    ->buildGroup($parents);
  if (isset($this->definitions[$group])) {
    return $this->definitions[$group];
  }

  // If there are predefined subdivisions at this level, try to load them.
  $this->definitions[$group] = [];
  if ($this
    ->hasData($parents)) {
    $cache_key = 'address.subdivisions.' . $group;
    $filename = $this->definitionPath . $group . '.json';

    // Loading priority: event -> cache -> filesystem.
    $event = new SubdivisionsEvent($parents);
    $this->eventDispatcher
      ->dispatch(AddressEvents::SUBDIVISIONS, $event);
    if ($definitions = $event
      ->getDefinitions()) {
      $this->definitions[$group] = $this
        ->processDefinitions($definitions);
    }
    elseif ($cached = $this->cache
      ->get($cache_key)) {
      $this->definitions[$group] = $cached->data;
    }
    elseif ($raw_definition = @file_get_contents($filename)) {
      $this->definitions[$group] = json_decode($raw_definition, TRUE);
      $this->definitions[$group] = $this
        ->processDefinitions($this->definitions[$group]);
      $this->cache
        ->set($cache_key, $this->definitions[$group], CacheBackendInterface::CACHE_PERMANENT, [
        'subdivisions',
      ]);
    }
  }
  return $this->definitions[$group];
}