SimplesitemapManager.php 10.4 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\simple_sitemap;

use Drupal\Core\Config\ConfigFactory;
6
use Drupal\Core\Database\Connection;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager;

/**
 * Class SimplesitemapManager
 * @package Drupal\simple_sitemap
 */
class SimplesitemapManager {

  const DEFAULT_SITEMAP_TYPE = 'default_hreflang';
  const DEFAULT_SITEMAP_GENERATOR = 'default';

  /**
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

28 29 30 31 32
  /**
   * @var \Drupal\Core\Database\Connection
   */
  protected $db;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager
   */
  protected $sitemapTypeManager;

  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
   */
  protected $urlGeneratorManager;

  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager
   */
  protected $sitemapGeneratorManager;

gbyte.co's avatar
gbyte.co committed
48 49 50 51 52
  /**
   * @var \Drupal\simple_sitemap\SimplesitemapSettings
   */
  protected $settings;

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  /**
   * @var SitemapTypeBase[] $sitemapTypes
   */
  protected $sitemapTypes = [];

  /**
   * @var UrlGeneratorBase[] $urlGenerators
   */
  protected $urlGenerators = [];

  /**
   * @var SitemapGeneratorBase[] $sitemapGenerators
   */
  protected $sitemapGenerators = [];

  /**
   * SimplesitemapManager constructor.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
71
   * @param \Drupal\Core\Database\Connection $database
72 73 74
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager $sitemap_type_manager
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager $sitemap_generator_manager
gbyte.co's avatar
gbyte.co committed
75
   * @param \Drupal\simple_sitemap\SimplesitemapSettings $settings
76 77 78
   */
  public function __construct(
    ConfigFactory $config_factory,
79
    Connection $database,
80 81
    SitemapTypeManager $sitemap_type_manager,
    UrlGeneratorManager $url_generator_manager,
gbyte.co's avatar
gbyte.co committed
82 83
    SitemapGeneratorManager $sitemap_generator_manager,
    SimplesitemapSettings $settings
84 85
  ) {
    $this->configFactory = $config_factory;
86
    $this->db = $database;
87 88 89
    $this->sitemapTypeManager = $sitemap_type_manager;
    $this->urlGeneratorManager = $url_generator_manager;
    $this->sitemapGeneratorManager = $sitemap_generator_manager;
gbyte.co's avatar
gbyte.co committed
90
    $this->settings = $settings;
91 92 93
  }

  /**
94
   * @param string $sitemap_generator_id
95 96 97 98 99 100 101 102 103 104 105 106 107
   * @return \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function getSitemapGenerator($sitemap_generator_id) {
    if (!isset($this->sitemapGenerators[$sitemap_generator_id])) {
      $this->sitemapGenerators[$sitemap_generator_id]
        = $this->sitemapGeneratorManager->createInstance($sitemap_generator_id);
    }

    return $this->sitemapGenerators[$sitemap_generator_id];
  }

  /**
108
   * @param string $url_generator_id
109 110 111 112 113 114 115 116 117 118 119 120 121
   * @return \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function getUrlGenerator($url_generator_id) {
    if (!isset($this->urlGenerators[$url_generator_id])) {
      $this->urlGenerators[$url_generator_id]
        = $this->urlGeneratorManager->createInstance($url_generator_id);
    }

    return $this->urlGenerators[$url_generator_id];
  }

  /**
122
   * @return \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeBase[]
123 124 125 126 127 128 129 130 131 132
   */
  public function getSitemapTypes() {
    if (empty($this->sitemapTypes)) {
      $this->sitemapTypes = $this->sitemapTypeManager->getDefinitions();
    }

    return $this->sitemapTypes;
  }

  /**
133 134
   * @param string|null $sitemap_type
   * @param bool $attach_type_info
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
   * @return array
   */
  public function getSitemapVariants($sitemap_type = NULL, $attach_type_info = TRUE) {
    if (NULL === $sitemap_type) {
      $variants = [];
      foreach ($this->configFactory->listAll('simple_sitemap.variants.') as $config_name) {
        $config_name_parts = explode('.', $config_name);
        $saved_variants = $this->configFactory->get($config_name)->get('variants');
        $saved_variants = $attach_type_info ? $this->attachSitemapTypeToVariants($saved_variants, $config_name_parts[2]) : $saved_variants;
        $variants = array_merge($variants, (is_array($saved_variants) ? $saved_variants : []));
      }
    }
    else {
      $variants = $this->configFactory->get("simple_sitemap.variants.$sitemap_type")->get('variants');
      $variants = is_array($variants) ? $variants : [];
      $variants = $attach_type_info ? $this->attachSitemapTypeToVariants($variants, $sitemap_type) : $variants;
    }
152 153
    array_multisort(array_column($variants, "weight"), SORT_ASC, $variants);
    return $variants;
154 155
  }

156 157 158 159 160
  /**
   * @param array $variants
   * @param string $type
   * @return array
   */
161 162 163 164 165
  protected function attachSitemapTypeToVariants(array $variants, $type) {
    return array_map(function($variant) use ($type) { return $variant + ['type' => $type]; }, $variants);
  }

  /**
166
   * @param string $name
167
   * @param array $definition
168
   * @return $this
169
   * @throws \Drupal\Component\Plugin\Exception\PluginException
170 171
   */
  public function addSitemapVariant($name, $definition = []) {
172 173 174 175 176 177 178 179 180 181
    $all_variants = $this->getSitemapVariants();
    if (isset($all_variants[$name])) {
      $old_variant = $all_variants[$name];
      if (!empty($definition['type']) && $old_variant['type'] !== $definition['type']) {
        $this->removeSitemapVariants($name);
        unset($old_variant);
      }
      else {
        unset($old_variant['type']);
      }
182 183
    }

184 185
    if (!isset($old_variant) && empty($definition['label'])) {
      $definition['label'] = (string) $name;
186 187
    }

188 189 190
    if (!isset($old_variant) && empty($definition['type'])) {
      $definition['type'] = self::DEFAULT_SITEMAP_TYPE;
    }
191

192 193
    if (isset($definition['weight'])) {
      $definition['weight'] = (int) $definition['weight'];
194
    }
195 196 197 198 199 200
    elseif (!isset($old_variant)) {
      $definition['weight'] = 0;
    }

    if (isset($old_variant)) {
      $definition = $definition + $old_variant;
201 202
    }

203 204 205 206 207
    $variants = array_merge($this->getSitemapVariants($definition['type'], FALSE), [$name => ['label' => $definition['label'], 'weight' => $definition['weight']]]);
    $this->configFactory->getEditable('simple_sitemap.variants.' . $definition['type'])
      ->set('variants', $variants)
      ->save();

208 209 210
    return $this;
  }

211 212
  /**
   * @param null|array|string $variant_names
213
   *  Limit removal by specific variants.
214 215
   *
   * @return $this
216
   *
217 218
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
219
  public function removeSitemap($variant_names = NULL) {
220 221 222 223 224 225 226 227 228 229 230 231 232
    if (NULL === $variant_names || !empty((array) $variant_names)) {
      $saved_variants = $this->getSitemapVariants();
      $remove_variants = NULL === $variant_names
        ? $saved_variants
        : array_intersect_key($saved_variants, array_flip((array) $variant_names));

      if (!empty($remove_variants)) {
        $type_definitions = $this->getSitemapTypes();
        foreach ($remove_variants as $variant_name => $variant_definition) {
          $this->getSitemapGenerator($type_definitions[$variant_definition['type']]['sitemapGenerator'])
            ->setSitemapVariant($variant_name)
            ->remove();
        }
233 234 235 236 237 238
      }
    }

    return $this;
  }

239 240
  /**
   * @param null|array|string $variant_names
241
   *  Limit removal by specific variants.
242 243
   *
   * @return $this
244
   *
245 246
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
247
  public function removeSitemapVariants($variant_names = NULL) {
248
    if (NULL === $variant_names || !empty((array) $variant_names)) {
249 250 251

      // Remove sitemap instances.
      $this->removeSitemap($variant_names);
252

253 254
      if (NULL === $variant_names) {
        // Remove all variants and their bundle settings.
255
        foreach(['variants', 'bundle_settings', 'custom_links'] as $config_name_part) {
256 257 258 259
          foreach ($this->configFactory->listAll("simple_sitemap.$config_name_part.") as $config_name) {
            $this->configFactory->getEditable($config_name)->delete();
          }
        }
260
      }
261 262 263 264 265 266 267 268
      else {
        // Remove bundle settings for specific variants.
        foreach ((array) $variant_names as $variant_name) {
          foreach ($this->configFactory->listAll("simple_sitemap.bundle_settings.$variant_name.") as $config_name) {
            $this->configFactory->getEditable($config_name)->delete();
          }
        }

269 270 271 272 273 274 275
        // Remove custom links for specific variants.
        foreach ((array) $variant_names as $variant_name) {
          foreach ($this->configFactory->listAll("simple_sitemap.custom_links.$variant_name") as $config_name) {
            $this->configFactory->getEditable($config_name)->delete();
          }
        }

276 277 278 279 280 281 282 283 284 285 286 287
        // Remove specific variants from configuration.
        $remove_variants = [];
        $variants = $this->getSitemapVariants();
        foreach ((array) $variant_names as $variant_name) {
          if (isset($variants[$variant_name])) {
            $remove_variants[$variants[$variant_name]['type']][$variant_name] = $variant_name;
          }
        }
        foreach ($remove_variants as $type => $variants_per_type) {
          $this->configFactory->getEditable("simple_sitemap.variants.$type")
            ->set('variants', array_diff_key($this->getSitemapVariants($type, FALSE), $variants_per_type))
            ->save();
288 289 290
        }
      }

291 292 293 294
      // Remove bundle setting overrides for entities.
      $query = $this->db->delete('simple_sitemap_entity_overrides');
      if (NULL !== $variant_names) {
        $query->condition('type', (array) $variant_names, 'IN');
295
      }
296 297 298 299 300 301
      $query->execute();


      // Remove default variant setting.
      if (NULL === $variant_names
        || in_array($this->settings->getSetting('default_variant', ''), (array) $variant_names)) {
gbyte.co's avatar
gbyte.co committed
302 303
        $this->settings->saveSetting('default_variant', '');
      }
304 305 306 307 308
    }

    return $this;
  }
}