Рефакторинг плагина site map

This commit is contained in:
Anton Vakhrushev 2019-01-06 14:55:14 +03:00
parent ac15fd6f0a
commit f131f53ea7

View File

@ -5,17 +5,21 @@ namespace Homepage\SiteMapBundle;
use Sculpin\Core\DataProvider\DataProviderInterface;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Sculpin;
use Sculpin\Core\Source\SourceInterface;
use Sculpin\Core\Source\SourceSet;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterface
{
protected $siteMap;
/**
* @var array|null
*/
private $siteMap;
/**
* @var SourceSet
*/
protected $sources;
private $sources;
/**
* {@inheritdoc}
@ -27,6 +31,18 @@ class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterfac
];
}
/**
* Provide data.
*
* @return array
*/
public function provideData(): array
{
$this->buildSiteMap();
return $this->siteMap;
}
/**
* Before run.
*
@ -37,26 +53,49 @@ class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterfac
$this->sources = $sourceSetEvent->sourceSet();
}
protected function buildSiteMap()
protected function buildSiteMap(): array
{
if (!empty($this->siteMap)) {
if ($this->siteMap !== null) {
return $this->siteMap;
}
$this->siteMap = $this->createSiteMap();
return $this->siteMap;
}
private function createSiteMap(): array
{
$siteMap = [];
/** @var \Sculpin\Core\Source\FileSource $source */
foreach ($this->sources->allSources() as $source) {
$url = $this->createSiteUrlFromSource($source);
if (!$url) {
continue;
}
$siteMap[$url['loc']] = $url;
}
return $siteMap;
}
private function createSiteUrlFromSource(SourceInterface $source): array
{
$data = $source->data()->export();
if (empty($data) || $source->useFileReference()) {
continue;
return [];
}
if ($data['draft']) {
return [];
}
$siteMapData = $data['sitemap'] ?? [];
if (isset($siteMapData['_exclude'])) {
continue;
if (array_key_exists('_exclude', $siteMapData)) {
return [];
}
$loc = $data['canonical'] ?? $data['url'];
@ -76,23 +115,6 @@ class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterfac
$url = array_merge($url, $data['sitemap']);
}
$siteMap[$url['loc']] = $url;
}
$this->siteMap = $siteMap;
return $this->siteMap;
}
/**
* Provide data.
*
* @return array
*/
public function provideData(): array
{
$this->buildSiteMap();
return $this->siteMap;
return $url;
}
}