vendor/presta/sitemap-bundle/src/Sitemap/Urlset.php line 135

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the PrestaSitemapBundle package.
  4. *
  5. * (c) PrestaConcept <https://prestaconcept.net>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Presta\SitemapBundle\Sitemap;
  11. use DateTimeInterface;
  12. use Presta\SitemapBundle\Sitemap\Url\Url;
  13. /**
  14. * Url set containing urls.
  15. *
  16. * https://www.sitemaps.org/protocol.html
  17. * https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps
  18. */
  19. class Urlset extends XmlConstraint
  20. {
  21. public const TAG = 'sitemap';
  22. /**
  23. * @var string
  24. */
  25. protected $loc;
  26. /**
  27. * @var DateTimeInterface
  28. */
  29. protected $lastmod;
  30. /**
  31. * @var string
  32. */
  33. protected $urlsXml = '';
  34. /**
  35. * @var array<string, string>
  36. */
  37. protected $customNamespaces = [];
  38. /**
  39. * @param string $loc
  40. * @param DateTimeInterface|null $lastmod
  41. */
  42. public function __construct(string $loc, DateTimeInterface $lastmod = null)
  43. {
  44. $this->loc = $loc;
  45. $this->lastmod = $lastmod ?? new \DateTimeImmutable();
  46. }
  47. /**
  48. * @return string
  49. */
  50. public function getLoc(): string
  51. {
  52. return $this->loc;
  53. }
  54. /**
  55. * @return DateTimeInterface
  56. */
  57. public function getLastmod(): DateTimeInterface
  58. {
  59. return $this->lastmod;
  60. }
  61. /**
  62. * add url to pool and check limits
  63. *
  64. * @param Url $url
  65. *
  66. * @throws \RuntimeException
  67. */
  68. public function addUrl(Url $url): void
  69. {
  70. if ($this->isFull()) {
  71. throw new \RuntimeException('The urlset limit has been exceeded');
  72. }
  73. $urlXml = $url->toXml();
  74. $this->appendXML($urlXml);
  75. //add unknown custom namespaces
  76. $this->customNamespaces = array_merge($this->customNamespaces, $url->getCustomNamespaces());
  77. //---------------------
  78. //Check limits
  79. if ($this->countItems++ >= self::LIMIT_ITEMS) {
  80. $this->limitItemsReached = true;
  81. }
  82. $urlLength = strlen($urlXml);
  83. $this->countBytes += $urlLength;
  84. if ($this->countBytes + $urlLength + strlen($this->getStructureXml()) > self::LIMIT_BYTES) {
  85. //we suppose the next url is almost the same length and cannot be added
  86. //plus we keep 500kB (@see self::LIMIT_BYTES)
  87. //... beware of numerous images set in url
  88. $this->limitBytesReached = true;
  89. }
  90. //---------------------
  91. }
  92. /**
  93. * Appends URL's XML to internal string buffer
  94. *
  95. * @param string $urlXml
  96. */
  97. protected function appendXML(string $urlXml): void
  98. {
  99. $this->urlsXml .= $urlXml;
  100. }
  101. /**
  102. * get the xml structure of the current urlset
  103. *
  104. * @return string
  105. */
  106. protected function getStructureXml(): string
  107. {
  108. $struct = '<?xml version="1.0" encoding="UTF-8"?>';
  109. $struct .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" NAMESPACES>URLS</urlset>';
  110. $namespaces = '';
  111. foreach ($this->customNamespaces as $key => $location) {
  112. $namespaces .= ' xmlns:' . $key . '="' . $location . '"';
  113. }
  114. $struct = str_replace('NAMESPACES', $namespaces, $struct);
  115. return $struct;
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. public function toXml(): string
  121. {
  122. return str_replace('URLS', $this->urlsXml, $this->getStructureXml());
  123. }
  124. }