Возвращен sitemap

This commit is contained in:
Anton Vakhrushev 2019-01-02 17:00:35 +03:00
parent 28215491ca
commit ca22c7afd8
7 changed files with 150 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<?php
use Homepage\HtmlPrettierBundle\HtmlPrettierBundle;
//use Nickpeirson\Sculpin\Bundle\SitemapBundle\SculpinSitemapBundle;
use Homepage\SiteMapBundle\SiteMapBundle;
use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel;
class SculpinKernel extends AbstractKernel
@ -9,7 +9,7 @@ class SculpinKernel extends AbstractKernel
protected function getAdditionalSculpinBundles(): array
{
return [
// SculpinSitemapBundle::class,
SiteMapBundle::class,
HtmlPrettierBundle::class,
];
}

View File

@ -41,7 +41,13 @@ class HtmlPrettier implements EventSubscriberInterface
/** @var \Sculpin\Core\Source\SourceInterface $source */
foreach ($sourceSet->allSources() as $source) {
$filename = $source->filename();
if ($this->endsWith($filename, '.md') || $this->endsWith($filename, '.html.twig')) {
$isSuitable = $filename === 'sitemap.xml'
|| $this->endsWith($filename, '.md')
|| $this->endsWith($filename, '.html.twig')
;
if ($isSuitable) {
yield $source;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Homepage\SiteMapBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class SiteMapExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sculpin_sitemap.event.sitemap_generator" class="Homepage\SiteMapBundle\SiteMapGenerator" public="true">
<tag name="kernel.event_subscriber" />
<tag name="sculpin.data_provider" alias="sitemap" />
</service>
</services>
</container>

View File

@ -0,0 +1,9 @@
<?php
namespace Homepage\SiteMapBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class SiteMapBundle extends Bundle
{
}

View File

@ -0,0 +1,98 @@
<?php
namespace Homepage\SiteMapBundle;
use Sculpin\Core\DataProvider\DataProviderInterface;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Sculpin;
use Sculpin\Core\Source\SourceSet;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SiteMapGenerator implements DataProviderInterface, EventSubscriberInterface
{
protected $siteMap;
/**
* @var SourceSet
*/
protected $sources;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
Sculpin::EVENT_BEFORE_RUN => 'saveSourceSet',
];
}
/**
* Before run.
*
* @param SourceSetEvent $sourceSetEvent Source Set Event
*/
public function saveSourceSet(SourceSetEvent $sourceSetEvent)
{
$this->sources = $sourceSetEvent->sourceSet();
}
protected function buildSiteMap()
{
if (!empty($this->siteMap)) {
return $this->siteMap;
}
$siteMap = [];
/** @var \Sculpin\Core\Source\FileSource $source */
foreach ($this->sources->allSources() as $source) {
$data = $source->data()->export();
if (empty($data) || $source->useFileReference()) {
continue;
}
$siteMapData = $data['sitemap'] ?? [];
if (isset($siteMapData['_exclude'])) {
continue;
}
$loc = $data['canonical'] ?? $data['url'];
if (is_callable([$source, 'file'])) {
$lastmod = date(DATE_W3C, $source->file()->getMTime());
} else {
$lastmod = date(DATE_W3C);
}
$url = [
'loc' => $loc,
'lastmod' => $lastmod,
];
if (isset($data['sitemap'])) {
$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;
}
}

View File

@ -11,7 +11,7 @@ sitemap:
<url>
{# Last slash for pretty url #}
<loc>{{ site.url }}{{ url.loc != '/.' ? (url.loc ~ '/') : '' }}</loc>
<loc>{{ site.url }}{{ url.loc != '/.' ? (url.loc|trim('/', side='right') ~ '/') : '' }}</loc>
<lastmod>{{ url.lastmod }}</lastmod>