-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sitemap support #152
Merged
Merged
Sitemap support #152
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't
|
||
$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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I upgraded it to
^2.0|^3.1.0
last week, which went fairly easy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely we would need to upgrade to PHPUnit ^6.0 at least for the whole plugin then? Feels like that belongs to a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True :)