UrlGeneratorBase.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
  3. use Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Drupal\simple_sitemap\Logger;
  6. use Drupal\simple_sitemap\Simplesitemap;
  7. /**
  8. * Class UrlGeneratorBase
  9. * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
  10. */
  11. abstract class UrlGeneratorBase extends SimplesitemapPluginBase implements UrlGeneratorInterface {
  12. /**
  13. * @var \Drupal\simple_sitemap\Simplesitemap
  14. */
  15. protected $generator;
  16. /**
  17. * @var \Drupal\simple_sitemap\Logger
  18. */
  19. protected $logger;
  20. /**
  21. * @var array
  22. */
  23. protected $settings;
  24. /**
  25. * @var string
  26. */
  27. protected $sitemapVariant;
  28. /**
  29. * UrlGeneratorBase constructor.
  30. * @param array $configuration
  31. * @param $plugin_id
  32. * @param $plugin_definition
  33. * @param \Drupal\simple_sitemap\Simplesitemap $generator
  34. * @param \Drupal\simple_sitemap\Logger $logger
  35. */
  36. public function __construct(
  37. array $configuration,
  38. $plugin_id,
  39. $plugin_definition,
  40. Simplesitemap $generator,
  41. Logger $logger
  42. ) {
  43. parent::__construct($configuration, $plugin_id, $plugin_definition);
  44. $this->generator = $generator;
  45. $this->logger = $logger;
  46. }
  47. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  48. return new static(
  49. $configuration,
  50. $plugin_id,
  51. $plugin_definition,
  52. $container->get('simple_sitemap.generator'),
  53. $container->get('simple_sitemap.logger')
  54. );
  55. }
  56. /**
  57. * @param array $settings
  58. * @return $this
  59. */
  60. public function setSettings(array $settings) {
  61. $this->settings = $settings;
  62. return $this;
  63. }
  64. /**
  65. * @param string $sitemap_variant
  66. * @return $this
  67. */
  68. public function setSitemapVariant($sitemap_variant) {
  69. $this->sitemapVariant = $sitemap_variant;
  70. return $this;
  71. }
  72. /**
  73. * @param string $url
  74. * @return string
  75. */
  76. protected function replaceBaseUrlWithCustom($url) {
  77. return !empty($this->settings['base_url'])
  78. ? str_replace($GLOBALS['base_url'], $this->settings['base_url'], $url)
  79. : $url;
  80. }
  81. /**
  82. * @return mixed
  83. */
  84. abstract public function getDataSets();
  85. /**
  86. * @param $data_set
  87. * @return mixed
  88. */
  89. abstract protected function processDataSet($data_set);
  90. /**
  91. * @param $data_set
  92. * @return array
  93. */
  94. public function generate($data_set) {
  95. $path_data = $this->processDataSet($data_set);
  96. return FALSE !== $path_data ? [$path_data] : [];
  97. }
  98. }