Рефакторинг плагина site map
This commit is contained in:
parent
ac15fd6f0a
commit
f131f53ea7
@ -5,17 +5,21 @@ namespace Homepage\SiteMapBundle;
|
|||||||
use Sculpin\Core\DataProvider\DataProviderInterface;
|
use Sculpin\Core\DataProvider\DataProviderInterface;
|
||||||
use Sculpin\Core\Event\SourceSetEvent;
|
use Sculpin\Core\Event\SourceSetEvent;
|
||||||
use Sculpin\Core\Sculpin;
|
use Sculpin\Core\Sculpin;
|
||||||
|
use Sculpin\Core\Source\SourceInterface;
|
||||||
use Sculpin\Core\Source\SourceSet;
|
use Sculpin\Core\Source\SourceSet;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterface
|
class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterface
|
||||||
{
|
{
|
||||||
protected $siteMap;
|
/**
|
||||||
|
* @var array|null
|
||||||
|
*/
|
||||||
|
private $siteMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var SourceSet
|
* @var SourceSet
|
||||||
*/
|
*/
|
||||||
protected $sources;
|
private $sources;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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.
|
* Before run.
|
||||||
*
|
*
|
||||||
@ -37,26 +53,49 @@ class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterfac
|
|||||||
$this->sources = $sourceSetEvent->sourceSet();
|
$this->sources = $sourceSetEvent->sourceSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildSiteMap()
|
protected function buildSiteMap(): array
|
||||||
{
|
{
|
||||||
if (!empty($this->siteMap)) {
|
if ($this->siteMap !== null) {
|
||||||
return $this->siteMap;
|
return $this->siteMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->siteMap = $this->createSiteMap();
|
||||||
|
|
||||||
|
return $this->siteMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createSiteMap(): array
|
||||||
|
{
|
||||||
$siteMap = [];
|
$siteMap = [];
|
||||||
|
|
||||||
/** @var \Sculpin\Core\Source\FileSource $source */
|
/** @var \Sculpin\Core\Source\FileSource $source */
|
||||||
foreach ($this->sources->allSources() as $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();
|
$data = $source->data()->export();
|
||||||
|
|
||||||
if (empty($data) || $source->useFileReference()) {
|
if (empty($data) || $source->useFileReference()) {
|
||||||
continue;
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($data['draft']) {
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$siteMapData = $data['sitemap'] ?? [];
|
$siteMapData = $data['sitemap'] ?? [];
|
||||||
|
|
||||||
if (isset($siteMapData['_exclude'])) {
|
if (array_key_exists('_exclude', $siteMapData)) {
|
||||||
continue;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$loc = $data['canonical'] ?? $data['url'];
|
$loc = $data['canonical'] ?? $data['url'];
|
||||||
@ -76,23 +115,6 @@ class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterfac
|
|||||||
$url = array_merge($url, $data['sitemap']);
|
$url = array_merge($url, $data['sitemap']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$siteMap[$url['loc']] = $url;
|
return $url;
|
||||||
}
|
|
||||||
|
|
||||||
$this->siteMap = $siteMap;
|
|
||||||
|
|
||||||
return $this->siteMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provide data.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function provideData(): array
|
|
||||||
{
|
|
||||||
$this->buildSiteMap();
|
|
||||||
|
|
||||||
return $this->siteMap;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user