SubdivisionRepository.php in Address 8
File
src/Repository/SubdivisionRepository.php
View source
<?php
namespace Drupal\address\Repository;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepository as ExternalSubdivisionRepository;
use Drupal\address\Event\AddressEvents;
use Drupal\address\Event\SubdivisionsEvent;
use Drupal\Core\Cache\CacheBackendInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SubdivisionRepository extends ExternalSubdivisionRepository {
protected $eventDispatcher;
protected $cache;
public function __construct(AddressFormatRepositoryInterface $address_format_repository, EventDispatcherInterface $event_dispatcher, CacheBackendInterface $cache) {
parent::__construct($address_format_repository);
$this->eventDispatcher = $event_dispatcher;
$this->cache = $cache;
}
protected function loadDefinitions(array $parents) {
$group = $this
->buildGroup($parents);
if (isset($this->definitions[$group])) {
return $this->definitions[$group];
}
$this->definitions[$group] = [];
if ($this
->hasData($parents)) {
$cache_key = 'address.subdivisions.' . $group;
$filename = $this->definitionPath . $group . '.json';
$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];
}
}