Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions modules/next/src/Entity/NextSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,15 @@ public function getLiveUrlForEntity(EntityInterface $entity): ?Url {
/**
* {@inheritdoc}
*/
public function getRevalidateUrlForPath(string $path): ?Url {
public function buildRevalidateUrl(array $query = []): ?Url {
$revalidate_url = $this->getRevalidateUrl();

if (!$revalidate_url) {
return NULL;
}

$query = [
'path' => $path,
];

if ($secret = $this->getRevalidateSecret()) {
$query['secret'] = $secret;
}

return Url::fromUri($this->getRevalidateUrl(), [
return Url::fromUri($revalidate_url, [
'query' => $query,
]);
}
Expand Down
6 changes: 3 additions & 3 deletions modules/next/src/Entity/NextSiteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ public function setRevalidateSecret(string $revalidate_secret): self;
/**
* Returns the revalidate url for given path.
*
* @param string $path
* The path.
* @param array $query
* The revalidate URL query parameters.
*
* @return \Drupal\Core\Url|null
* The revalidate url.
*/
public function getRevalidateUrlForPath(string $path): ?Url;
public function buildRevalidateUrl(array $query = []): ?Url;

}
107 changes: 107 additions & 0 deletions modules/next/src/Plugin/Next/Revalidator/CacheTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Drupal\next\Plugin\Next\Revalidator;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Error;
use Drupal\next\Event\EntityActionEvent;
use Drupal\next\Plugin\ConfigurableRevalidatorBase;
use Drupal\next\Plugin\RevalidatorInterface;
use Symfony\Component\HttpFoundation\Response;

/**
* Cache tag based on-demand revalidation plugin.
*
* @Revalidator(
* id = "cache_tag",
* label = "Cache Tag",
* description = "Cache tag based on-demand revalidation."
* )
*/
class CacheTag extends ConfigurableRevalidatorBase implements RevalidatorInterface {

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// No configuration form.
}

/**
* {@inheritdoc}
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function revalidate(EntityActionEvent $event): bool {
$revalidated = FALSE;
$entity = $event->getEntity();
$sites = $event->getSites();

if (!($entity instanceof FieldableEntityInterface)) {
return FALSE;
}

// Get all available cache tags (including list tags).
$list_tags = $entity->getEntityType()->getListCacheTags();
if ($entity->getEntityType()->hasKey('bundle')) {
$list_tags[] = $entity->getEntityTypeId() . '_list:' . $entity->bundle();
}
$combined_tags = array_merge($entity->getCacheTags(), $list_tags);
$cache_tags = implode(',', $combined_tags);
Comment on lines +60 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. It is much more concise than our original version.


/** @var \Drupal\next\Entity\NextSite $site */
foreach ($sites as $site) {
try {
$revalidate_url = $site->buildRevalidateUrl(['tags' => $cache_tags]);
if (!$revalidate_url) {
throw new \Exception('No revalidate url set.');
}

if ($this->nextSettingsManager->isDebug()) {
$this->logger->notice('(@action): Revalidating tags %list for the site %site. URL: %url', [
'@action' => $event->getAction(),
'%list' => $cache_tags,
'%site' => $site->label(),
'%url' => $revalidate_url->toString(),
]);
}

$response = $this->httpClient->request('GET', $revalidate_url->toString());
if ($response && $response->getStatusCode() === Response::HTTP_OK) {
if ($this->nextSettingsManager->isDebug()) {
$this->logger->notice('(@action): Successfully revalidated tags %list for the site %site. URL: %url', [
'@action' => $event->getAction(),
'%list' => $cache_tags,
'%site' => $site->label(),
'%url' => $revalidate_url->toString(),
]);
}

$revalidated = TRUE;
}
}
catch (\Exception $exception) {
Error::logException($this->logger, $exception);
$revalidated = FALSE;
}
}

return $revalidated;
}

}
3 changes: 2 additions & 1 deletion modules/next/src/Plugin/Next/Revalidator/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ public function revalidate(EntityActionEvent $event): bool {
}

foreach ($paths as $path) {
/** @var \Drupal\next\Entity\NextSite $site */
foreach ($sites as $site) {
try {
$revalidate_url = $site->getRevalidateUrlForPath($path);
$revalidate_url = $site->buildRevalidateUrl(['path' => $path]);

if (!$revalidate_url) {
throw new \Exception('No revalidate url set.');
Expand Down
8 changes: 4 additions & 4 deletions modules/next/tests/src/Kernel/Entity/NextSiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function test() {
}

/**
* @covers ::getRevalidateUrlForPath
* @covers ::buildRevalidateUrl
*/
public function testGetRevalidateUrlForPath() {
$marketing = NextSite::create([
Expand All @@ -129,13 +129,13 @@ public function testGetRevalidateUrlForPath() {
]);
$marketing->save();

$this->assertNull($marketing->getRevalidateUrlForPath('/foo'));
$this->assertNull($marketing->buildRevalidateUrl(['path' => '/foo']));

$marketing->setRevalidateUrl('http://example.com/api/revalidate');
$this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->getRevalidateUrlForPath('/foo')->toString());
$this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->buildRevalidateUrl(['path' => '/foo'])->toString());

$marketing->setRevalidateSecret('12345');
$this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->getRevalidateUrlForPath('/foo')->toString());
$this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->buildRevalidateUrl(['path' => '/foo'])->toString());
}

}
Loading