Skip to content

Commit

Permalink
Merge pull request #24 from sweoggy/patch-1
Browse files Browse the repository at this point in the history
Make StaticUrlProvider also use proper alternative URLs
  • Loading branch information
stefandoorn authored Jan 28, 2018
2 parents e59a084 + e868b6e commit 4ea4b6b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 28 deletions.
69 changes: 46 additions & 23 deletions src/Provider/StaticUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -22,11 +23,6 @@ final class StaticUrlProvider implements UrlProviderInterface
*/
private $sitemapUrlFactory;

/**
* @var LocaleContextInterface
*/
private $localeContext;

/**
* @var array
*/
Expand All @@ -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;
}

Expand All @@ -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;
}

}
2 changes: 1 addition & 1 deletion src/Resources/config/services/providers/static.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<service id="sylius.sitemap_provider.static" class="SitemapPlugin\Provider\StaticUrlProvider">
<argument type="service" id="router" />
<argument type="service" id="sylius.sitemap_url_factory" />
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.context.channel" />
<argument>%sylius.sitemap_static%</argument>
<tag name="sylius.sitemap_provider" />
</service>
Expand Down
1 change: 1 addition & 0 deletions tests/Controller/AbstractTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions tests/Controller/SitemapStaticControllerApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Tests\SitemapPlugin\Controller;

use Sylius\Component\Core\Model\Taxon;

/**
* @author Stefan Doorn <[email protected]>
*/
Expand Down
20 changes: 19 additions & 1 deletion tests/Responses/Expected/show_sitemap_all.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://localhost/en_US/products/test</loc>
<lastmod>@[email protected]()</lastmod>
Expand All @@ -13,11 +13,29 @@
</url>
<url>
<loc>http://localhost/en_US/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/nl_NL/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/en_US/contact/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/contact/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/contact/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/nl_NL/contact/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/contact/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/contact/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
Expand Down
20 changes: 19 additions & 1 deletion tests/Responses/Expected/show_sitemap_all_relative.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>/en_US/products/test</loc>
<lastmod>@[email protected]()</lastmod>
Expand All @@ -13,11 +13,29 @@
</url>
<url>
<loc>/en_US/</loc>
<xhtml:link rel="alternate" hreflang="en" href="/en_US/"/>
<xhtml:link rel="alternate" hreflang="nl" href="/nl_NL/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>/nl_NL/</loc>
<xhtml:link rel="alternate" hreflang="en" href="/en_US/"/>
<xhtml:link rel="alternate" hreflang="nl" href="/nl_NL/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>/en_US/contact/</loc>
<xhtml:link rel="alternate" hreflang="en" href="/en_US/contact/"/>
<xhtml:link rel="alternate" hreflang="nl" href="/nl_NL/contact/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>/nl_NL/contact/</loc>
<xhtml:link rel="alternate" hreflang="en" href="/en_US/contact/"/>
<xhtml:link rel="alternate" hreflang="nl" href="/nl_NL/contact/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
Expand Down
18 changes: 18 additions & 0 deletions tests/Responses/Expected/show_sitemap_static.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://localhost/en_US/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/nl_NL/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/en_US/contact/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/contact/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/contact/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/nl_NL/contact/</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/contact/"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/contact/"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
Expand Down

0 comments on commit 4ea4b6b

Please sign in to comment.