Skip to content

Instantly share code, notes, and snippets.

@jeroendesloovere
Last active July 13, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroendesloovere/131dc592d100fc147bef52601aef41f0 to your computer and use it in GitHub Desktop.
Save jeroendesloovere/131dc592d100fc147bef52601aef41f0 to your computer and use it in GitHub Desktop.
Sitemap generator: new version
<?php
/**
* NOTE: the code below was a brainstorm session.
* You should check out https://github.com/jeroendesloovere/sitemap-bundle for the latest code
*/
/**
* CORE
*/
class SitemapProviders
{
/** @var SitemapProviderInterface[] */
private $sitemapProviders;
public function add(SitemapProviderInterface $sitemapProvider): void
{
$this->sitemapProviders[$sitemapProvider->getKey()] = $sitemapProvider;
}
public function getAll(): array
{
return $this->sitemapProviders;
}
}
class SitemapProvider
{
/** @var SitemapItems */
private $items;
/** @var string - bvb: NewsArticle, will create a "sitemap_NewsArticle.xml" file */
private $key;
public function __construct(string $key)
{
$this->key = $key;
$this->items = new SitemapItems();
}
public function getItems(): SitemapItems
{
return $this->items;
}
public function getKey(): string
{
return $this->key;
}
public function createItem(string $url, \DateTime $editedOn, ChangeFrequency $changeFrequency): void
{
$this->items->add(new SitemapItem($url, $editedOn, $changeFrequency));
}
}
interface SitemapProviderInterface
{
public function getKey(): string;
public function createItem(): void;
public function createItems(): void;
}
class SitemapItems
{
/** @var SitemapItem[] */
private $items;
public function add(SitemapItem $item): void
{
$this->items[] = $item;
}
public function getAll(): array
{
return $this->items;
}
}
class SitemapItem
{
/** @var \DateTime */
private $editedOn;
/** @var string */
private $url;
/** @var ChangeFrequency */
private $changeFrequency;
public function __construct(string $url, \DateTime $editedOn, ChangeFrequency $changeFrequency)
{
$this->url = $url;
$this->editedOn = $editedOn;
$this->changeFrequency = $changeFrequency;
}
public function getChangeFrequency(): ChangeFrequency
{
return $this->changeFrequency;
}
public function getEditedOn(): DateTime
{
return $this->editedOn;
}
public function getUrl(): string
{
return $this->url;
}
}
class SitemapGenerator
{
/** @var SitemapProviders */
private $sitemapProviders;
/** @var string */
private $toPath;
public function __construct(string $toPath, SitemapProviders $sitemapProviders)
{
$this->sitemapProviders = $sitemapProviders;
$this->toPath = realpath($toPath);
}
public function generate(): void
{
foreach ($this->sitemapProviders as $sitemapProvider) {
$this->generateSitemapForProvider($sitemapProvider);
}
$this->generateSitemapIndex(array_keys($this->sitemapProviders->getAll()));
}
private function generateSitemapForProvider(SitemapProvider $sitemapProvider): void
{
/** @var SitemapItem[] $items */
$items = $sitemapProvider->getItems();
}
private function generateSitemapIndex(array $fileNames): void
{
// @todo: generate sitemap overview index
}
}
class ChangeFrequency
{
}
/**
* CUSTOM INTEGRATION
*
``` yaml
services:
App\NewsArticleSitemapProvider:
tags:
- { name: sitemap.provider }
App\NewsCategorySitemapProvider:
tags:
- { name: sitemap.provider }
```
*/
class NewsArticleSitemapProvider extends SitemapProvider implements SitemapProviderInterface
{
/** @var NewsArticleRepository */
private $articleRepository;
public function __construct(NewsArticleRepository $articleRepository)
{
$this->articleRepository = $articleRepository;
parent::__construct('NewsArticle');
}
public function createItems(): void
{
/** @var Article[] $articles */
$articles = $this->articleRepository->findAll();
foreach ($articles as $article) {
$this->createItem('/nl/xxx/url-to-article', $article->getEditedOn(), ChangeFrequency::monthly());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment