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

Some clean up refactoring

parent 21a48146
......@@ -50,7 +50,6 @@ services:
- '@simple_sitemap.sitemap_generator'
- '@language_manager'
- '@entity_type.manager'
- '@path.validator'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
......@@ -62,9 +61,9 @@ services:
- '@simple_sitemap.sitemap_generator'
- '@language_manager'
- '@entity_type.manager'
- '@path.validator'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
- '@path.validator'
simple_sitemap.arbitrary_url_generator:
class: Drupal\simple_sitemap\Batch\Generator\ArbitraryUrlGenerator
......@@ -74,7 +73,6 @@ services:
- '@simple_sitemap.sitemap_generator'
- '@language_manager'
- '@entity_type.manager'
- '@path.validator'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
......
......@@ -2,7 +2,18 @@
namespace Drupal\simple_sitemap\Batch\Generator;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\Entity;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
/**
* Class CustomUrlGenerator
......@@ -12,11 +23,47 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE = "The custom path @path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users. You can review custom paths <a href='@custom_paths_url'>here</a>.";
/**
* @var \Drupal\Core\Path\PathValidator
*/
protected $pathValidator;
/**
* @var bool
*/
protected $includeImages;
/**
* CustomUrlGenerator constructor.
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\simple_sitemap\Logger $logger
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
* @param \Drupal\Core\Path\PathValidator $path_validator
*/
public function __construct(
Simplesitemap $generator,
SitemapGenerator $sitemap_generator,
LanguageManagerInterface $language_manager,
EntityTypeManagerInterface $entity_type_manager,
Logger $logger,
EntityHelper $entityHelper,
PathValidator $path_validator
) {
$this->pathValidator = $path_validator;
parent::__construct(
$generator,
$sitemap_generator,
$language_manager,
$entity_type_manager,
$logger,
$entityHelper
);
}
/**
* Batch function which generates urls to custom paths.
*
......
......@@ -13,7 +13,6 @@ use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
/**
* Class UrlGeneratorBase
......@@ -56,13 +55,6 @@ class UrlGeneratorBase {
*/
protected $entityTypeManager;
/**
* @var \Drupal\Core\Path\PathValidator
*
* @todo Move to CustomUrlGenerator
*/
protected $pathValidator;
/**
* @var \Drupal\simple_sitemap\Logger
*/
......@@ -94,26 +86,23 @@ class UrlGeneratorBase {
* @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\Core\Path\PathValidator $path_validator
* @param \Drupal\simple_sitemap\Logger $logger
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
*/
public function __construct(
Simplesitemap $generator,
SitemapGenerator $sitemap_generator, //todo: use $this->generator->sitemapGenerator instead?
SitemapGenerator $sitemap_generator,
LanguageManagerInterface $language_manager,
EntityTypeManagerInterface $entity_type_manager,
PathValidator $path_validator,
Logger $logger,
EntityHelper $entityHelper
) {
$this->generator = $generator;
$this->sitemapGenerator = $sitemap_generator; // todo: using only one method, maybe make method static instead?
$this->sitemapGenerator = $sitemap_generator;
$this->languageManager = $language_manager;
$this->languages = $language_manager->getLanguages();
$this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
$this->entityTypeManager = $entity_type_manager;
$this->pathValidator = $path_validator;
$this->logger = $logger;
$this->entityHelper = $entityHelper;
$this->anonUser = $this->entityTypeManager->getStorage('user')
......
......@@ -58,12 +58,15 @@ class Simplesitemap {
/**
* @var array
*/
protected static $allowed_link_settings = [
protected static $allowedLinkSettings = [
'entity' => ['index', 'priority', 'changefreq', 'include_images'],
'custom' => ['priority', 'changefreq'],
];
protected static $link_setting_defaults = [
/**
* @var array
*/
protected static $linkSettingDefaults = [
'index' => 1,
'priority' => 0.5,
'changefreq' => '',
......@@ -208,8 +211,17 @@ class Simplesitemap {
*/
public function generateSitemap($from = 'form') {
$this->sitemapGenerator
->setGenerator($this)
->setGenerateFrom($from)
->setBundleSettings($this->getBundleSettings())
->setCustomLinks($this->getCustomLinks())
->setSettings([
'base_url' => $this->getSetting('base_url', ''),
'batch_process_limit' => $this->getSetting('batch_process_limit', NULL),
'max_links' => $this->getSetting('max_links', 2000),
'skip_untranslated' => $this->getSetting('skip_untranslated', FALSE),
'remove_duplicates' => $this->getSetting('remove_duplicates', TRUE),
'excluded_languages' => $this->getSetting('excluded_languages', []),
'from' => $from,
])
->startGeneration();
}
......@@ -224,7 +236,7 @@ class Simplesitemap {
*/
protected function getSitemapIndex($chunk_info) {
return $this->sitemapGenerator
->setGenerator($this)
->setSettings(['base_url' => $this->getSetting('base_url', '')])
->generateSitemapIndex($chunk_info);
}
......@@ -415,10 +427,10 @@ class Simplesitemap {
* @return array
*/
protected function supplementDefaultSettings($type, $settings) {
foreach(self::$allowed_link_settings[$type] as $allowed_link_setting) {
foreach(self::$allowedLinkSettings[$type] as $allowed_link_setting) {
if (!isset($settings[$allowed_link_setting])
&& isset(self::$link_setting_defaults[$allowed_link_setting])) {
$settings[$allowed_link_setting] = self::$link_setting_defaults[$allowed_link_setting];
&& isset(self::$linkSettingDefaults[$allowed_link_setting])) {
$settings[$allowed_link_setting] = self::$linkSettingDefaults[$allowed_link_setting];
}
}
return $settings;
......
......@@ -49,24 +49,29 @@ class SitemapGenerator {
protected $moduleHandler;
/**
* @var string
* @var bool
*/
protected $generateFrom = 'form';
protected $isHreflangSitemap;
/**
* @var bool
* @var \Drupal\Component\Datetime\Time
*/
protected $isHreflangSitemap;
protected $time;
/**
* @var \Drupal\simple_sitemap\Simplesitemap
* @var array
*/
protected $generator;
protected $bundleSettings;
/**
* @var \Drupal\Component\Datetime\Time
* @var array
*/
protected $time;
protected $customLinks;
/**
* @var array
*/
protected $settings;
/**
* @var array
......@@ -122,20 +127,29 @@ class SitemapGenerator {
}
/**
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param array $bundle_settings
* @return $this
*/
public function setBundleSettings(array $bundle_settings) {
$this->bundleSettings = $bundle_settings;
return $this;
}
/**
* @param array $custom_links
* @return $this
*/
public function setGenerator(Simplesitemap $generator) {
$this->generator = $generator;
public function setCustomLinks(array $custom_links) {
$this->customLinks = $custom_links;
return $this;
}
/**
* @param string $from
* @param array $settings
* @return $this
*/
public function setGenerateFrom($from) {
$this->generateFrom = $from;
public function setSettings(array $settings) {
$this->settings = $settings;
return $this;
}
......@@ -143,17 +157,7 @@ class SitemapGenerator {
* Adds all operations to the batch and starts it.
*/
public function startGeneration() {
$this->batch->setBatchInfo([
'from' => $this->generateFrom,
'batch_process_limit' => !empty($this->generator->getSetting('batch_process_limit'))
? $this->generator->getSetting('batch_process_limit') : NULL,
'max_links' => $this->generator->getSetting('max_links', 2000),
'skip_untranslated' => $this->generator->getSetting('skip_untranslated', FALSE),
'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
'entity_types' => $this->generator->getBundleSettings(),
'base_url' => $this->generator->getSetting('base_url', ''),
'excluded_languages' => $this->generator->getSetting('excluded_languages', []),
]);
$this->batch->setBatchInfo($this->settings + ['entity_types' => $this->bundleSettings]);
// Add custom link generating operation.
$this->batch->addOperation('simple_sitemap.custom_url_generator', $this->getCustomUrlsData());
......@@ -181,7 +185,7 @@ class SitemapGenerator {
*/
protected function getCustomUrlsData() {
$paths = [];
foreach ($this->generator->getCustomLinks() as $i => $custom_path) {
foreach ($this->customLinks as $i => $custom_path) {
$paths[$i]['path'] = $custom_path['path'];
$paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$paths[$i]['changefreq'] = isset($custom_path['changefreq']) ? $custom_path['changefreq'] : NULL;
......@@ -198,8 +202,7 @@ class SitemapGenerator {
protected function getEntityTypeData() {
$data_sets = [];
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
$entity_types = $this->generator->getBundleSettings();
foreach ($entity_types as $entity_type_name => $bundles) {
foreach ($this->bundleSettings as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) {
$keys = $sitemap_entity_types[$entity_type_name]->getKeys();
......@@ -283,7 +286,7 @@ class SitemapGenerator {
* @return string
*/
public function getCustomBaseUrl() {
$customBaseUrl = $this->generator->getSetting('base_url', '');
$customBaseUrl = $this->settings['base_url'];
return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
}
......
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