Skip to content

Commit

Permalink
Merge pull request #41 from stefandoorn/code-improvements-static-sitemap
Browse files Browse the repository at this point in the history
Code improvements Static Sitemap provider
  • Loading branch information
stefandoorn authored Feb 27, 2018
2 parents d59d50c + 612894b commit aaab930
Showing 1 changed file with 55 additions and 22 deletions.
77 changes: 55 additions & 22 deletions src/Provider/StaticUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,11 @@ public function generate(): iterable
return $this->urls;
}

/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

foreach ($this->routes as $route) {
foreach ($this->transformAndYieldRoutes() as $route) {
$staticUrl = $this->sitemapUrlFactory->createNew();
$staticUrl->setChangeFrequency(ChangeFrequency::weekly());
$staticUrl->setPriority(0.3);

// Add default locale to route if not set
if (!array_key_exists('_locale', $route['parameters'])) {
if ($channel->getDefaultLocale()) {
$route['parameters']['_locale'] = $channel->getDefaultLocale()->getCode();
}
}

// 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->getAlternativeLocales($channel);
}

// Remove the locale that is on the main route from the alternatives to prevent duplicates
$route = $this->excludeMainRouteLocaleFromAlternativeLocales($route);

$location = $this->router->generate($route['route'], $route['parameters']);
$staticUrl->setLocalization($location);

Expand All @@ -112,6 +94,56 @@ public function generate(): iterable
return $this->urls;
}

/**
* @return \Generator
*/
private function transformAndYieldRoutes(): \Generator
{
foreach($this->routes as $route) {
yield $this->transformRoute($route);
}
}

/**
* @param array $route
* @return array
*/
private function transformRoute(array $route): array
{
// Add default locale to route if not set
$route = $this->addDefaultRoute($route);

// 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->getAlternativeLocales();
}

// Remove the locale that is on the main route from the alternatives to prevent duplicates
$route = $this->excludeMainRouteLocaleFromAlternativeLocales($route);

return $route;
}

/**
* @param array $route
* @return array
*/
private function addDefaultRoute(array $route): array
{
if (isset($route['parameters']['_locale'])) {
return $route;
}

/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

if ($channel->getDefaultLocale()) {
$route['parameters']['_locale'] = $channel->getDefaultLocale()->getCode();
}

return $route;
}

/**
* @param array $route
* @return array
Expand All @@ -131,12 +163,13 @@ private function excludeMainRouteLocaleFromAlternativeLocales(array $route): arr
}

/**
* @param ChannelInterface $channel
*
* @return string[]
*/
private function getAlternativeLocales(ChannelInterface $channel): array
private function getAlternativeLocales(): array
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

$locales = [];

foreach ($channel->getLocales() as $locale) {
Expand Down

0 comments on commit aaab930

Please sign in to comment.