ProcessSitemap.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Webkul\Sitemap\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\File;
  9. use Illuminate\Support\Facades\Storage;
  10. use Spatie\Sitemap\Sitemap;
  11. use Spatie\Sitemap\SitemapIndex;
  12. use Spatie\Sitemap\Tags\Url;
  13. use Webkul\Sitemap\Contracts\Sitemap as SitemapContract;
  14. use Webkul\Sitemap\Models\Category;
  15. use Webkul\Sitemap\Models\Page;
  16. use Webkul\Sitemap\Models\Product;
  17. class ProcessSitemap implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. /**
  21. * Batch processed.
  22. */
  23. protected int $batchProcessed = 0;
  24. /**
  25. * Items to be processed.
  26. */
  27. protected array $itemsToBeProcessed = [];
  28. /**
  29. * Generated sitemaps.
  30. */
  31. protected array $generatedSitemaps = [];
  32. /**
  33. * Create a new job instance.
  34. */
  35. public function __construct(
  36. public SitemapContract $sitemap
  37. ) {}
  38. /**
  39. * Execute the job.
  40. */
  41. public function handle(): void
  42. {
  43. /**
  44. * If sitemap is disabled then return.
  45. */
  46. if (! core()->getConfigData('general.sitemap.settings.enabled')) {
  47. return;
  48. }
  49. /**
  50. * If the sitemap is already generated then delete the existing sitemap.
  51. */
  52. $this->sitemap->deleteFromStorage();
  53. /**
  54. * Process the store URL.
  55. */
  56. $this->processItems([Url::create('/')]);
  57. /**
  58. * Process the categories.
  59. */
  60. Category::query()->chunk(100, fn ($items) => $this->processItems($items));
  61. /**
  62. * Process the products.
  63. */
  64. Product::query()->chunk(100, fn ($items) => $this->processItems($items));
  65. /**
  66. * Process the CMS pages.
  67. */
  68. Page::query()->chunk(100, fn ($items) => $this->processItems($items));
  69. /**
  70. * If there are any items left to be processed then generate the sitemap.
  71. */
  72. if (! empty($this->itemsToBeProcessed)) {
  73. $this->generateSitemap();
  74. }
  75. /**
  76. * After generating all the sitemaps, we will generate the index.
  77. */
  78. $this->generateSitemapIndex();
  79. /**
  80. * Update the sitemap with the generated sitemap index and sitemaps.
  81. */
  82. $this->sitemap->update([
  83. 'generated_at' => now(),
  84. 'additional' => array_merge($this->sitemap->additional ?? [], [
  85. 'index' => $this->sitemap->index_file_name,
  86. 'sitemaps' => $this->generatedSitemaps,
  87. ]),
  88. ]);
  89. }
  90. /**
  91. * Process items.
  92. *
  93. * @param mixed $items
  94. */
  95. protected function processItems($items): void
  96. {
  97. foreach ($items as $item) {
  98. $this->itemsToBeProcessed[] = $item;
  99. if (count($this->itemsToBeProcessed) === (int) core()->getConfigData('general.sitemap.file_limits.max_url_per_file')) {
  100. $this->generateSitemap();
  101. }
  102. }
  103. }
  104. /**
  105. * Generate sitemap.
  106. */
  107. protected function generateSitemap(): void
  108. {
  109. $this->batchProcessed++;
  110. $sitemap = Sitemap::create();
  111. foreach ($this->itemsToBeProcessed as $item) {
  112. $sitemap->add($item);
  113. }
  114. $sitemapFilePath = clean_path($this->sitemap->path.'/'.File::name($this->sitemap->file_name).'-'.$this->sitemap->id.'-'.$this->batchProcessed.'.'.File::extension($this->sitemap->file_name));
  115. $sitemap->writeToDisk('public', $sitemapFilePath);
  116. $this->generatedSitemaps[] = $sitemapFilePath;
  117. $this->itemsToBeProcessed = [];
  118. }
  119. /**
  120. * Generate sitemap index.
  121. */
  122. protected function generateSitemapIndex(): void
  123. {
  124. $sitemap = SitemapIndex::create();
  125. foreach ($this->generatedSitemaps as $generatedSitemap) {
  126. $sitemap->add(Storage::disk('public')->url($generatedSitemap));
  127. }
  128. $sitemap->writeToDisk('public', $this->sitemap->index_file_name);
  129. }
  130. }