-
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 #152 from sweoggy/sitemap-support
Sitemap support
- Loading branch information
Showing
20 changed files
with
508 additions
and
6,202 deletions.
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
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
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
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,11 @@ | ||
services: | ||
bitbag_sylius_cms_plugin.sitemap_provider.page: | ||
class: BitBag\SyliusCmsPlugin\Sitemap\Provider\PageUrlProvider | ||
arguments: | ||
- "@bitbag_sylius_cms_plugin.repository.page" | ||
- "@router" | ||
- "@sylius.sitemap_url_factory" | ||
- "@sylius.context.locale" | ||
- "@sylius.context.channel" | ||
tags: | ||
- "sylius.sitemap_provider" |
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,184 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusCmsPlugin\Sitemap\Provider; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\PageInterface; | ||
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface; | ||
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface; | ||
use Doctrine\Common\Collections\Collection; | ||
use SitemapPlugin\Factory\SitemapUrlFactoryInterface; | ||
use SitemapPlugin\Model\ChangeFrequency; | ||
use SitemapPlugin\Model\SitemapUrlInterface; | ||
use SitemapPlugin\Provider\UrlProviderInterface; | ||
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; | ||
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; | ||
|
||
class PageUrlProvider implements UrlProviderInterface | ||
{ | ||
/** | ||
* @var PageRepositoryInterface|EntityRepository | ||
*/ | ||
private $pageRepository; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* @var SitemapUrlFactoryInterface | ||
*/ | ||
private $sitemapUrlFactory; | ||
|
||
/** | ||
* @var LocaleContextInterface | ||
*/ | ||
private $localeContext; | ||
|
||
/** | ||
* @var ChannelContextInterface | ||
*/ | ||
private $channelContext; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $urls = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $channelLocaleCodes; | ||
|
||
/** | ||
* @param PageRepositoryInterface $pageRepository | ||
* @param RouterInterface $router | ||
* @param SitemapUrlFactoryInterface $sitemapUrlFactory | ||
* @param LocaleContextInterface $localeContext | ||
* @param ChannelContextInterface $channelContext | ||
*/ | ||
public function __construct( | ||
PageRepositoryInterface $pageRepository, | ||
RouterInterface $router, | ||
SitemapUrlFactoryInterface $sitemapUrlFactory, | ||
LocaleContextInterface $localeContext, | ||
ChannelContextInterface $channelContext | ||
) { | ||
$this->pageRepository = $pageRepository; | ||
$this->router = $router; | ||
$this->sitemapUrlFactory = $sitemapUrlFactory; | ||
$this->localeContext = $localeContext; | ||
$this->channelContext = $channelContext; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'cms_pages'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate(): iterable | ||
{ | ||
foreach ($this->getPages() as $product) { | ||
$this->urls[] = $this->createPageUrl($product); | ||
} | ||
|
||
return $this->urls; | ||
} | ||
|
||
/** | ||
* @param PageInterface $page | ||
* | ||
* @return Collection|PageTranslationInterface[] | ||
*/ | ||
private function getTranslations(PageInterface $page): Collection | ||
{ | ||
return $page->getTranslations()->filter(function (TranslationInterface $translation) { | ||
return $this->localeInLocaleCodes($translation); | ||
}); | ||
} | ||
|
||
/** | ||
* @param TranslationInterface $translation | ||
* | ||
* @return bool | ||
*/ | ||
private function localeInLocaleCodes(TranslationInterface $translation): bool | ||
{ | ||
return in_array($translation->getLocale(), $this->getLocaleCodes()); | ||
} | ||
|
||
/** | ||
* @return array|PageInterface[] | ||
*/ | ||
private function getPages(): iterable | ||
{ | ||
return $this->pageRepository->findByEnabled(true); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function getLocaleCodes(): array | ||
{ | ||
if (null === $this->channelLocaleCodes) { | ||
/** @var ChannelInterface $channel */ | ||
$channel = $this->channelContext->getChannel(); | ||
$this->channelLocaleCodes = $channel->getLocales()->map(function (LocaleInterface $locale) { | ||
return $locale->getCode(); | ||
})->toArray(); | ||
} | ||
|
||
return $this->channelLocaleCodes; | ||
} | ||
|
||
/** | ||
* @param PageInterface $page | ||
* | ||
* @return SitemapUrlInterface | ||
*/ | ||
private function createPageUrl(PageInterface $page): SitemapUrlInterface | ||
{ | ||
$pageUrl = $this->sitemapUrlFactory->createNew(); | ||
$pageUrl->setChangeFrequency(ChangeFrequency::daily()); | ||
$pageUrl->setPriority(0.7); | ||
if ($page->getUpdatedAt()) { | ||
$pageUrl->setLastModification($page->getUpdatedAt()); | ||
} elseif ($page->getCreatedAt()) { | ||
$pageUrl->setLastModification($page->getCreatedAt()); | ||
} | ||
/** @var PageTranslationInterface $translation */ | ||
foreach ($this->getTranslations($page) as $translation) { | ||
if (!$translation->getLocale()) { | ||
continue; | ||
} | ||
if (!$this->localeInLocaleCodes($translation)) { | ||
continue; | ||
} | ||
$location = $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', [ | ||
'slug' => $translation->getSlug(), | ||
'_locale' => $translation->getLocale(), | ||
]); | ||
if ($translation->getLocale() === $this->localeContext->getLocaleCode()) { | ||
$pageUrl->setLocalization($location); | ||
|
||
continue; | ||
} | ||
$pageUrl->addAlternative($location, $translation->getLocale()); | ||
} | ||
|
||
return $pageUrl; | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
|
||
namespace Tests\BitBag\SyliusCmsPlugin\Api\Sitemap\Provider; | ||
|
||
use Lakion\ApiTestCase\XmlApiTestCase; | ||
use Sylius\Component\Core\Model\Channel; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Currency\Model\Currency; | ||
use Sylius\Component\Currency\Model\CurrencyInterface; | ||
use Sylius\Component\Locale\Model\Locale; | ||
use Sylius\Component\Locale\Model\LocaleInterface; | ||
|
||
abstract class AbstractTestController extends XmlApiTestCase | ||
{ | ||
|
||
/** | ||
* @var ChannelInterface | ||
*/ | ||
protected $channel; | ||
|
||
/** | ||
* @var LocaleInterface | ||
*/ | ||
protected $locale; | ||
|
||
/** | ||
* @var LocaleInterface | ||
*/ | ||
protected $secondLocale; | ||
|
||
/** | ||
* @var CurrencyInterface | ||
*/ | ||
protected $currency; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function setupDatabase() | ||
{ | ||
parent::setUpDatabase(); | ||
|
||
$this->locale = new Locale(); | ||
$this->locale->setCode('en_US'); | ||
|
||
$this->getEntityManager()->persist($this->locale); | ||
|
||
$this->secondLocale = new Locale(); | ||
$this->secondLocale->setCode('nl_NL'); | ||
|
||
$this->getEntityManager()->persist($this->secondLocale); | ||
|
||
$this->currency = new Currency(); | ||
$this->currency->setCode('USD'); | ||
|
||
$this->getEntityManager()->persist($this->currency); | ||
|
||
$this->channel = new Channel(); | ||
$this->channel->setCode('US_WEB'); | ||
$this->channel->setName('US Web Store'); | ||
$this->channel->setDefaultLocale($this->locale); | ||
$this->channel->setBaseCurrency($this->currency); | ||
$this->channel->setTaxCalculationStrategy('order_items_based'); | ||
|
||
$this->channel->addLocale($this->locale); | ||
$this->channel->addLocale($this->secondLocale); | ||
|
||
$this->getEntityManager()->persist($this->channel); | ||
$this->getEntityManager()->flush(); | ||
} | ||
|
||
} |
Oops, something went wrong.