
| Current Path : /var/www/html/store/web/modules/contrib/address/src/Repository/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/store/web/modules/contrib/address/src/Repository/SubdivisionRepository.php |
<?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;
/**
* Provides subdivisions.
*
* Subdivisions are stored on disk in JSON and cached inside Drupal.
*/
class SubdivisionRepository extends ExternalSubdivisionRepository {
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* Creates a SubdivisionRepository instance.
*
* @param \CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface $address_format_repository
* The address format repository.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend.
*/
public function __construct(AddressFormatRepositoryInterface $address_format_repository, EventDispatcherInterface $event_dispatcher, CacheBackendInterface $cache) {
parent::__construct($address_format_repository);
$this->eventDispatcher = $event_dispatcher;
$this->cache = $cache;
}
/**
* {@inheritdoc}
*/
protected function loadDefinitions(array $parents): array {
$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($event, AddressEvents::SUBDIVISIONS);
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];
}
}