You are here

class SubdivisionRepository in Address 8

Provides subdivisions.

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

Hierarchy

  • class \Drupal\address\Repository\SubdivisionRepository extends \CommerceGuys\Addressing\Subdivision\SubdivisionRepository

Expanded class hierarchy of SubdivisionRepository

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

File

src/Repository/SubdivisionRepository.php, line 17

Namespace

Drupal\address\Repository
View source
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) {
    $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];
  }

}

Members

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