From 612894bdf6f352bd881da846471d01527b5451c2 Mon Sep 17 00:00:00 2001 From: Stefan Doorn Date: Mon, 26 Feb 2018 22:40:58 +0100 Subject: [PATCH] Code & performance improvements to Static Content provider --- src/Provider/StaticUrlProvider.php | 77 +++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/src/Provider/StaticUrlProvider.php b/src/Provider/StaticUrlProvider.php index 4239bedb..439a6f54 100644 --- a/src/Provider/StaticUrlProvider.php +++ b/src/Provider/StaticUrlProvider.php @@ -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); @@ -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 @@ -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) {