diff --git a/src/Provider/StaticUrlProvider.php b/src/Provider/StaticUrlProvider.php index 0eee14e6..8bd00b4f 100644 --- a/src/Provider/StaticUrlProvider.php +++ b/src/Provider/StaticUrlProvider.php @@ -4,7 +4,8 @@ use SitemapPlugin\Factory\SitemapUrlFactoryInterface; use SitemapPlugin\Model\ChangeFrequency; -use Sylius\Component\Locale\Context\LocaleContextInterface; +use Sylius\Component\Channel\Context\ChannelContextInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Symfony\Component\Routing\RouterInterface; /** @@ -22,11 +23,6 @@ final class StaticUrlProvider implements UrlProviderInterface */ private $sitemapUrlFactory; - /** - * @var LocaleContextInterface - */ - private $localeContext; - /** * @var array */ @@ -37,22 +33,27 @@ final class StaticUrlProvider implements UrlProviderInterface */ private $routes; + /** + * @var ChannelContextInterface + */ + private $channelContext; + /** * StaticUrlProvider constructor. * @param RouterInterface $router * @param SitemapUrlFactoryInterface $sitemapUrlFactory - * @param LocaleContextInterface $localeContext + * @param ChannelContextInterface $channelContext * @param array $routes */ public function __construct( RouterInterface $router, SitemapUrlFactoryInterface $sitemapUrlFactory, - LocaleContextInterface $localeContext, + ChannelContextInterface $channelContext, array $routes ) { $this->router = $router; $this->sitemapUrlFactory = $sitemapUrlFactory; - $this->localeContext = $localeContext; + $this->channelContext = $channelContext; $this->routes = $routes; } @@ -73,33 +74,55 @@ public function generate(): iterable return $this->urls; } + /** @var ChannelInterface $channel */ + $channel = $this->channelContext->getChannel(); + foreach ($this->routes as $route) { $staticUrl = $this->sitemapUrlFactory->createNew(); $staticUrl->setChangeFrequency(ChangeFrequency::weekly()); $staticUrl->setPriority(0.3); + // Populate locales array by other enabled locales for current channel if no locales are specified if (!isset($route['locales']) || empty($route['locales'])) { - $route['locales'] = [$this->localeContext->getLocaleCode()]; + $route['locales'] = $this->getAlternativeLocales($channel); + } + + if (!array_key_exists('_locale', $route['parameters'])) { + $route['parameters']['_locale'] = $channel->getDefaultLocale()->getCode(); + } + $location = $this->router->generate($route['route'], $route['parameters']); + $staticUrl->setLocalization($location); + + foreach ($route['locales'] as $alternativeLocaleCode) { + $route['parameters']['_locale'] = $alternativeLocaleCode; + $alternativeLocation = $this->router->generate($route['route'], $route['parameters']); + $staticUrl->addAlternative($alternativeLocation, $alternativeLocaleCode); } - foreach ($route['locales'] as $localeCode) { - // Add localeCode to parameters if not set - if (!array_key_exists('_locale', $route['parameters'])) { - $route['parameters']['_locale'] = $localeCode; - } + $this->urls[] = $staticUrl; + } - $location = $this->router->generate($route['route'], $route['parameters']); + return $this->urls; + } - if ($localeCode === $this->localeContext->getLocaleCode()) { - $staticUrl->setLocalization($location); - } else { - $staticUrl->addAlternative($location, $localeCode); - } + /** + * @param ChannelInterface $channel + * + * @return string[] + */ + private function getAlternativeLocales(ChannelInterface $channel): array + { + $locales = []; - $this->urls[] = $staticUrl; + foreach ($channel->getLocales() as $locale) { + if ($locale === $channel->getDefaultLocale()) { + continue; } + + $locales[] = $locale->getCode(); } - return $this->urls; + return $locales; } + } diff --git a/src/Resources/config/services/providers/static.xml b/src/Resources/config/services/providers/static.xml index 18c04d1a..390ea9d6 100644 --- a/src/Resources/config/services/providers/static.xml +++ b/src/Resources/config/services/providers/static.xml @@ -4,7 +4,7 @@ - + %sylius.sitemap_static% diff --git a/tests/Controller/AbstractTestController.php b/tests/Controller/AbstractTestController.php index b7f09768..10947804 100644 --- a/tests/Controller/AbstractTestController.php +++ b/tests/Controller/AbstractTestController.php @@ -60,6 +60,7 @@ public function setupDatabase() $this->channel->setTaxCalculationStrategy('order_items_based'); $this->channel->addLocale($this->locale); + $this->channel->addLocale($locale); $this->getEntityManager()->persist($this->channel); $this->getEntityManager()->flush(); diff --git a/tests/Controller/SitemapStaticControllerApiTest.php b/tests/Controller/SitemapStaticControllerApiTest.php index 17446bc3..87449ea9 100644 --- a/tests/Controller/SitemapStaticControllerApiTest.php +++ b/tests/Controller/SitemapStaticControllerApiTest.php @@ -2,8 +2,6 @@ namespace Tests\SitemapPlugin\Controller; -use Sylius\Component\Core\Model\Taxon; - /** * @author Stefan Doorn */ diff --git a/tests/Responses/Expected/show_sitemap_all.xml b/tests/Responses/Expected/show_sitemap_all.xml index 0dcd0670..1ed07c91 100644 --- a/tests/Responses/Expected/show_sitemap_all.xml +++ b/tests/Responses/Expected/show_sitemap_all.xml @@ -1,5 +1,5 @@ - + http://localhost/en_US/products/test @string@.isDateTime() @@ -13,11 +13,29 @@ http://localhost/en_US/ + + + weekly + 0.3 + + + http://localhost/nl_NL/ + + weekly 0.3 http://localhost/en_US/contact/ + + + weekly + 0.3 + + + http://localhost/nl_NL/contact/ + + weekly 0.3 diff --git a/tests/Responses/Expected/show_sitemap_all_relative.xml b/tests/Responses/Expected/show_sitemap_all_relative.xml index c17cde8b..c6d2232a 100644 --- a/tests/Responses/Expected/show_sitemap_all_relative.xml +++ b/tests/Responses/Expected/show_sitemap_all_relative.xml @@ -1,5 +1,5 @@ - + /en_US/products/test @string@.isDateTime() @@ -13,11 +13,29 @@ /en_US/ + + + weekly + 0.3 + + + /nl_NL/ + + weekly 0.3 /en_US/contact/ + + + weekly + 0.3 + + + /nl_NL/contact/ + + weekly 0.3 diff --git a/tests/Responses/Expected/show_sitemap_static.xml b/tests/Responses/Expected/show_sitemap_static.xml index 5f4df4b5..a76e2ea7 100644 --- a/tests/Responses/Expected/show_sitemap_static.xml +++ b/tests/Responses/Expected/show_sitemap_static.xml @@ -2,11 +2,29 @@ http://localhost/en_US/ + + + weekly + 0.3 + + + http://localhost/nl_NL/ + + weekly 0.3 http://localhost/en_US/contact/ + + + weekly + 0.3 + + + http://localhost/nl_NL/contact/ + + weekly 0.3