You are here

trait TreeLockTrait in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/TreeLockTrait.php \Drupal\entity_hierarchy\Storage\TreeLockTrait

Defines a trait for locking tree operations.

Hierarchy

2 files declare their use of TreeLockTrait
EntityReferenceHierarchy.php in src/Plugin/Field/FieldType/EntityReferenceHierarchy.php
EntityReferenceHierarchyFieldItemList.php in src/Plugin/Field/FieldType/EntityReferenceHierarchyFieldItemList.php

File

src/Storage/TreeLockTrait.php, line 10

Namespace

Drupal\entity_hierarchy\Storage
View source
trait TreeLockTrait {

  /**
   * Lock backend.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lockBackend;

  /**
   * Gets lock backend.
   *
   * @return \Drupal\Core\Lock\LockBackendInterface
   *   Lock backend.
   */
  protected function lockBackend() {
    if (!isset($this->lockBackend)) {
      $this->lockBackend = \Drupal::lock();
    }
    return $this->lockBackend;
  }

  /**
   * Sets lock backend.
   *
   * @param \Drupal\Core\Lock\LockBackendInterface $lockBackend
   *   Lock backend.
   *
   * @return $this
   */
  public function setLockBackend(LockBackendInterface $lockBackend) {
    $this->lockBackend = $lockBackend;
    return $this;
  }

  /**
   * Locks tree.
   *
   * @param string $fieldName
   *   Field name.
   * @param string $entityTypeId
   *   Entity Type ID.
   *
   * @throws \Exception
   *   When lock cannot be acquired after 30 seconds.
   */
  protected function lockTree($fieldName, $entityTypeId) {
    $count = 0;
    while (!$this
      ->lockBackend()
      ->acquire($this
      ->getLockName($fieldName, $entityTypeId))) {

      // Wait a while before trying again.
      sleep(1);
      $count++;
      if ($count === 30) {
        throw new \Exception('Unable to acquire lock to update tree.');
      }
    }
  }

  /**
   * Releases lock.
   *
   * @param string $fieldName
   *   Field name.
   * @param string $entityTypeId
   *   Entity Type ID.
   */
  protected function releaseLock($fieldName, $entityTypeId) {
    $this
      ->lockBackend()
      ->release($this
      ->getLockName($fieldName, $entityTypeId));
  }

  /**
   * Gets lock name.
   *
   * @param string $fieldName
   *   Field name.
   * @param string $entityTypeId
   *   Entity Type ID.
   *
   * @return string
   *   Lock name.
   */
  protected function getLockName($fieldName, $entityTypeId) {
    return sprintf('%s_%s', $fieldName, $entityTypeId);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TreeLockTrait::$lockBackend protected property Lock backend.
TreeLockTrait::getLockName protected function Gets lock name.
TreeLockTrait::lockBackend protected function Gets lock backend.
TreeLockTrait::lockTree protected function Locks tree.
TreeLockTrait::releaseLock protected function Releases lock.
TreeLockTrait::setLockBackend public function Sets lock backend.