You are here

public function UrlAndPathQueuer::invalidateTags in URLs queuer 8

Queues invalidated cache tags as tag purgables.

Overrides CacheTagsInvalidatorInterface::invalidateTags

File

src/Plugin/Purge/Queuer/UrlAndPathQueuer.php, line 79

Class

UrlAndPathQueuer
Queues URLs or paths when Drupal invalidates cache tags.

Namespace

Drupal\purge_queuer_url\Plugin\Purge\Queuer

Code

public function invalidateTags(array $tags) {
  if (!$this
    ->initialize()) {
    return;
  }

  // Remove tags to lookup that have already been invalidated during runtime.
  foreach ($tags as $i => $tag) {
    if (in_array($tag, $this->invalidatedTags)) {
      unset($tags[$i]);
    }
  }

  // When there are still tags left, attempt to lookup URLs and queue them.
  if (count($tags)) {
    if ($urls_and_paths = $this->registry
      ->getUrls($tags)) {
      $invalidations = [];

      // Iterate the matches and add URL/Path invalidations correspondingly.
      foreach ($urls_and_paths as $url_or_path) {
        $invalidation_type = strpos($url_or_path, '://') ? 'url' : 'path';
        try {
          $invalidations[] = $this->purgeInvalidationFactory
            ->get($invalidation_type, $url_or_path);
        } catch (TypeUnsupportedException $e) {

          // When there's no purger enabled for it, don't bother queuing URLs.
          return;
        } catch (PluginNotFoundException $e) {

          // When Drupal uninstalls Purge, rebuilds plugin caches it might
          // run into the condition where the tag plugin isn't available. In
          // these scenarios we want the queuer to silently fail.
          return;
        }
      }

      // Queue the invalidations and mark the tags to prevent duplicates.
      if (count($invalidations)) {
        foreach ($tags as $tag) {
          $this->invalidatedTags[] = $tag;
        }

        // The invalidations now go in purge's queue buffer, we're done!
        $this->purgeQueue
          ->add($this->queuer, $invalidations);
      }
    }
  }
}