You are here

public function TrafficRegistry::add in URLs queuer 8

Register a new URL or path with its associated cache tags at the registry.

@warning Implementation specific contstraints - such as database field length - might dismiss the URL being added. Although implementations should prevent this from happening at all cost, it could happen.

Parameters

string $url_or_path: The URL or path string to register (may already exist).

string[] $tags: Unassociative array with cache tags associated with the URL or path.

Throws

\LogicException Thrown when $tags is empty.

Overrides TrafficRegistryInterface::add

File

src/TrafficRegistry.php, line 34

Class

TrafficRegistry
Provides a database-driven traffic registry with URLs and tags.

Namespace

Drupal\purge_queuer_url

Code

public function add($url_or_path, array $tags) {
  if (!$this->connection
    ->schema()
    ->tableExists('purge_queuer_url')) {
    return;
  }
  if (empty($tags)) {
    throw new \LogicException('$tags cannot be empty!');
  }

  // Sometimes Drupal generates ridiculously long URLs that pass well over the
  // VARCHAR max length of 255, for example with the ?redirect parameters. We
  // dismiss these URLs here, as using bigger datatypes ain't worth the
  // trade-off and would make this module even more expensive to use.
  if (strlen($url_or_path) > 255) {
    return;
  }

  // Build a list of tag IDs by adding and or selecting them from the db.
  $tag_ids = ';' . implode(';', array_keys($this
    ->getTagIds($tags)));

  // Insert or update the URL with the shortened list of tag ids.
  $fields = [
    'url' => $url_or_path,
    'tag_ids' => $tag_ids,
  ];
  $this->connection
    ->merge('purge_queuer_url')
    ->insertFields($fields)
    ->updateFields($fields)
    ->key([
    'url' => $url_or_path,
  ])
    ->execute();
}