Commit 4c10c835 authored by gbyte.co's avatar gbyte.co

Improve API and implement cache per variant WIP

parent c37d97dc
......@@ -3,7 +3,8 @@
namespace Drupal\simple_sitemap;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Cache\Cache;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase;
/**
* Class Batch
......@@ -76,8 +77,12 @@ class Batch {
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateSitemap(array $arguments, array $batch_meta, &$context) {
\Drupal::service('plugin.manager.simple_sitemap.url_generator')
->createInstance($arguments['url_generator'])
/** @var UrlGeneratorBase $url_generator*/
$url_generator = \Drupal::service('plugin.manager.simple_sitemap.url_generator')
->createInstance($arguments['url_generator']);
$url_generator
->setContext($context)
->setSettings($arguments['settings'])
->setBatchMeta($batch_meta)
......@@ -96,8 +101,12 @@ class Batch {
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateIndex(array $arguments, array $batch_meta, &$context) {
\Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator'])
/** @var SitemapGeneratorBase $sitemap_generator*/
$sitemap_generator = \Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator']);
$sitemap_generator
->setSettings($arguments['settings'])
->setSitemapVariant($arguments['variant'])
->generateIndex();
......@@ -113,10 +122,15 @@ class Batch {
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function removeSitemap(array $arguments, array $batch_meta, &$context) {
\Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator'])
/** @var SitemapGeneratorBase $sitemap_generator*/
$sitemap_generator = \Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator']);
$sitemap_generator
->setSitemapVariant($arguments['variant'])
->remove();
->remove()
->invalidateCache();
}
/**
......@@ -134,7 +148,6 @@ class Batch {
*/
public static function finishGeneration($success, $results, $operations) {
if ($success) {
Cache::invalidateTags(['simple_sitemap']);
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_MESSAGE)
// ['@url' => $this->sitemapGenerator->getCustomBaseUrl() . '/sitemap.xml']) //todo: Use actual base URL for message.
......
......@@ -8,7 +8,6 @@ use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
/**
......@@ -75,12 +74,11 @@ class SimplesitemapController extends ControllerBase {
$response = new CacheableResponse($output, Response::HTTP_OK, [
'content-type' => 'application/xml',
'X-Robots-Tag' => 'noindex', // Do not index the sitemap itself.
'X-Robots-Tag' => 'noindex', // Tell search engines not to index the sitemap itself.
]);
// Cache output.
$meta_data = $response->getCacheableMetadata();
$meta_data->addCacheTags(['simple_sitemap']);
$response->getCacheableMetadata()->addCacheTags(["simple_sitemap:$variant"]);
return $response;
}
......
......@@ -9,6 +9,7 @@ use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Component\Datetime\Time;
use Drupal\Core\Cache\Cache;
/**
* Class SitemapGeneratorBase
......@@ -175,6 +176,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
$this->db->delete('simple_sitemap')
->condition('type', $this->sitemapVariant)
->execute();
return $this;
}
......@@ -199,6 +201,8 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
];
$this->db->insert('simple_sitemap')->fields($values)->execute();
return $this;
}
/**
......@@ -224,6 +228,14 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
])
->execute();
}
return $this;
}
public function invalidateCache() {
Cache::invalidateTags(['simple_sitemap:' . $this->sitemapVariant]);
return $this;
}
/**
......
......@@ -220,12 +220,12 @@ abstract class UrlGeneratorBase extends SimplesitemapPluginBase implements UrlGe
$this->context['results']['generate'][$this->sitemapVariant]['queued_links'][] = $result;
}
protected function sliceFromBatchResultQueue($sitemap_type, $number_results) {
$this->context['results']['generate'][$sitemap_type]['queued_links'] = array_slice(
$this->context['results']['generate'][$sitemap_type]['queued_links'], $number_results
protected function sliceFromBatchResultQueue($sitemap_variant, $number_results) {
$this->context['results']['generate'][$sitemap_variant]['queued_links'] = array_slice(
$this->context['results']['generate'][$sitemap_variant]['queued_links'], $number_results
);
if (empty($this->context['results']['generate'][$sitemap_type]['queued_links'])) {
unset($this->context['results']['generate'][$sitemap_type]);
if (empty($this->context['results']['generate'][$sitemap_variant]['queued_links'])) {
unset($this->context['results']['generate'][$sitemap_variant]);
}
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -58,6 +58,8 @@ abstract class SimplesitemapTestBase extends BrowserTestBase {
*/
protected $node2;
protected $defaultSitemapUrl = 'sitemap.xml';
/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment