CountryRepository.php in Address 8
File
src/Repository/CountryRepository.php
View source
<?php
namespace Drupal\address\Repository;
use CommerceGuys\Addressing\Country\CountryRepository as ExternalCountryRepository;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Language\LanguageManagerInterface;
class CountryRepository extends ExternalCountryRepository {
protected $cache;
public function __construct(CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
parent::__construct();
$this->cache = $cache;
$language = $language_manager
->getConfigOverrideLanguage() ?: $language_manager
->getCurrentLanguage();
$this->defaultLocale = $language
->getId();
}
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];
}
}