-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from stefandoorn/1.4-feature/section-sitemap
[1.4] Add sitemap provider for sections
- Loading branch information
Showing
7 changed files
with
288 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* another great project. | ||
* You can find more information about us on https://bitbag.shop and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusCmsPlugin\SitemapProvider; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\SectionInterface; | ||
use BitBag\SyliusCmsPlugin\Entity\SectionTranslationInterface; | ||
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface; | ||
use Doctrine\Common\Collections\Collection; | ||
use SitemapPlugin\Factory\SitemapUrlFactoryInterface; | ||
use SitemapPlugin\Model\ChangeFrequency; | ||
use SitemapPlugin\Model\SitemapUrlInterface; | ||
use SitemapPlugin\Provider\UrlProviderInterface; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Sylius\Component\Locale\Model\LocaleInterface; | ||
use Sylius\Component\Resource\Model\TranslationInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
final class SectionUrlProvider implements UrlProviderInterface | ||
{ | ||
/** @var SectionRepositoryInterface */ | ||
private $sectionRepository; | ||
|
||
/** @var RouterInterface */ | ||
private $router; | ||
|
||
/** @var SitemapUrlFactoryInterface */ | ||
private $sitemapUrlFactory; | ||
|
||
/** @var LocaleContextInterface */ | ||
private $localeContext; | ||
|
||
/** @var ChannelContextInterface */ | ||
private $channelContext; | ||
|
||
public function __construct( | ||
SectionRepositoryInterface $sectionRepository, | ||
RouterInterface $router, | ||
SitemapUrlFactoryInterface $sitemapUrlFactory, | ||
LocaleContextInterface $localeContext, | ||
ChannelContextInterface $channelContext | ||
) { | ||
$this->sectionRepository = $sectionRepository; | ||
$this->router = $router; | ||
$this->sitemapUrlFactory = $sitemapUrlFactory; | ||
$this->localeContext = $localeContext; | ||
$this->channelContext = $channelContext; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'cms_sections'; | ||
} | ||
|
||
public function generate(): iterable | ||
{ | ||
$urls = []; | ||
|
||
foreach ($this->getSections() as $section) { | ||
$urls[] = $this->createSectionUrl($section); | ||
} | ||
|
||
return $urls; | ||
} | ||
|
||
private function getTranslations(SectionInterface $section): Collection | ||
{ | ||
return $section->getTranslations()->filter(function (TranslationInterface $translation) { | ||
return $this->localeInLocaleCodes($translation); | ||
}); | ||
} | ||
|
||
private function localeInLocaleCodes(TranslationInterface $translation): bool | ||
{ | ||
return in_array($translation->getLocale(), $this->getLocaleCodes()); | ||
} | ||
|
||
private function getSections(): iterable | ||
{ | ||
return $this->sectionRepository->findAll(); | ||
} | ||
|
||
private function getLocaleCodes(): array | ||
{ | ||
/** @var ChannelInterface $channel */ | ||
$channel = $this->channelContext->getChannel(); | ||
|
||
return $channel->getLocales()->map(function (LocaleInterface $locale) { | ||
return $locale->getCode(); | ||
})->toArray(); | ||
} | ||
|
||
private function createSectionUrl(SectionInterface $section): SitemapUrlInterface | ||
{ | ||
$url = $this->sitemapUrlFactory->createNew(); | ||
|
||
$url->setChangeFrequency(ChangeFrequency::daily()); | ||
$url->setPriority(0.7); | ||
|
||
/** @var SectionTranslationInterface $translation */ | ||
foreach ($this->getTranslations($section) as $translation) { | ||
if (!$translation->getLocale() || !$this->localeInLocaleCodes($translation)) { | ||
continue; | ||
} | ||
|
||
$location = $this->router->generate('bitbag_sylius_cms_plugin_shop_section_show', [ | ||
'code' => $section->getCode(), | ||
'_locale' => $translation->getLocale(), | ||
]); | ||
|
||
if ($translation->getLocale() === $this->localeContext->getLocaleCode()) { | ||
$url->setLocalization($location); | ||
|
||
continue; | ||
} | ||
|
||
$url->addAlternative($location, $translation->getLocale()); | ||
} | ||
|
||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/Api/Sitemap/Provider/SitemapSectionControllerApiLocalesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* another great project. | ||
* You can find more information about us on https://bitbag.shop and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\BitBag\SyliusCmsPlugin\Api\Sitemap\Provider; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\Section; | ||
|
||
class SitemapSectionControllerApiLocalesTest extends AbstractTestController | ||
{ | ||
/** | ||
* @before | ||
*/ | ||
public function setUpDatabase(): void | ||
{ | ||
parent::setUpDatabase(); | ||
|
||
$section = new Section(); | ||
$section->setCurrentLocale('en_US'); | ||
$section->setName('Test'); | ||
$section->setCode('test'); | ||
$section->setCurrentLocale('nl_NL'); | ||
$section->setName('Test'); | ||
$this->getEntityManager()->persist($section); | ||
|
||
$section = new Section(); | ||
$section->setCurrentLocale('en_US'); | ||
$section->setName('Mock'); | ||
$section->setCode('mock'); | ||
$section->setCurrentLocale('nl_NL'); | ||
$section->setName('Mock'); | ||
$this->getEntityManager()->persist($section); | ||
|
||
$this->getEntityManager()->flush(); | ||
} | ||
|
||
public function testShowActionResponse(): void | ||
{ | ||
$this->client->request('GET', '/sitemap/cms_sections.xml'); | ||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'show_sitemap_sections_locale'); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/Api/Sitemap/Provider/SitemapSectionControllerApiTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* another great project. | ||
* You can find more information about us on https://bitbag.shop and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\BitBag\SyliusCmsPlugin\Api\Sitemap\Provider; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\Section; | ||
|
||
class SitemapSectionControllerApiTest extends AbstractTestController | ||
{ | ||
/** | ||
* @before | ||
*/ | ||
public function setUpDatabase(): void | ||
{ | ||
parent::setUpDatabase(); | ||
|
||
$section = new Section(); | ||
$section->setCurrentLocale('en_US'); | ||
$section->setName('Test'); | ||
$section->setCode('test'); | ||
$this->getEntityManager()->persist($section); | ||
|
||
$section = new Section(); | ||
$section->setCurrentLocale('en_US'); | ||
$section->setName('Mock'); | ||
$section->setCode('mock'); | ||
$this->getEntityManager()->persist($section); | ||
|
||
$this->getEntityManager()->flush(); | ||
} | ||
|
||
public function testShowActionResponse(): void | ||
{ | ||
$this->client->request('GET', '/sitemap/cms_sections.xml'); | ||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'show_sitemap_sections'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>http://localhost/en_US/section/test</loc> | ||
<changefreq>daily</changefreq> | ||
<priority>0.7</priority> | ||
</url> | ||
<url> | ||
<loc>http://localhost/en_US/section/mock</loc> | ||
<changefreq>daily</changefreq> | ||
<priority>0.7</priority> | ||
</url> | ||
</urlset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> | ||
<url> | ||
<loc>http://localhost/en_US/section/test</loc> | ||
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/section/test"/> | ||
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/section/test"/> | ||
<changefreq>daily</changefreq> | ||
<priority>0.7</priority> | ||
</url> | ||
<url> | ||
<loc>http://localhost/nl_NL/section/test</loc> | ||
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/section/test"/> | ||
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/section/test"/> | ||
<changefreq>daily</changefreq> | ||
<priority>0.7</priority> | ||
</url> | ||
<url> | ||
<loc>http://localhost/en_US/section/mock</loc> | ||
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/section/mock"/> | ||
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/section/mock"/> | ||
<changefreq>daily</changefreq> | ||
<priority>0.7</priority> | ||
</url> | ||
<url> | ||
<loc>http://localhost/nl_NL/section/mock</loc> | ||
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/section/mock"/> | ||
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/section/mock"/> | ||
<changefreq>daily</changefreq> | ||
<priority>0.7</priority> | ||
</url> | ||
</urlset> |