You are here

class CountryRepository in Address 8

Defines the country repository.

Countries are stored on disk in JSON and cached inside Drupal.

Hierarchy

  • class \Drupal\address\Repository\CountryRepository extends \CommerceGuys\Addressing\Country\CountryRepository

Expanded class hierarchy of CountryRepository

1 string reference to 'CountryRepository'
address.services.yml in ./address.services.yml
address.services.yml
1 service uses CountryRepository
address.country_repository in ./address.services.yml
Drupal\address\Repository\CountryRepository

File

src/Repository/CountryRepository.php, line 14

Namespace

Drupal\address\Repository
View source
class CountryRepository extends ExternalCountryRepository {

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * Creates a CountryRepository instance.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
    parent::__construct();
    $this->cache = $cache;

    // The getCurrentLanguage() fallback is a workaround for core bug #2684873.
    $language = $language_manager
      ->getConfigOverrideLanguage() ?: $language_manager
      ->getCurrentLanguage();
    $this->defaultLocale = $language
      ->getId();
  }

  /**
   * {@inheritdoc}
   */
  protected function loadDefinitions($locale) {
    if (isset($this->definitions[$locale])) {
      return $this->definitions[$locale];
    }
    $cache_key = 'address.countries.' . $locale;
    if ($cached = $this->cache
      ->get($cache_key)) {
      $this->definitions[$locale] = $cached->data;
    }
    else {
      $filename = $this->definitionPath . $locale . '.json';
      $this->definitions[$locale] = json_decode(file_get_contents($filename), TRUE);
      $this->cache
        ->set($cache_key, $this->definitions[$locale], CacheBackendInterface::CACHE_PERMANENT, [
        'countries',
      ]);
    }
    return $this->definitions[$locale];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CountryRepository::$cache protected property The cache backend.
CountryRepository::loadDefinitions protected function
CountryRepository::__construct public function Creates a CountryRepository instance.