Skip to content

Commit

Permalink
Merge pull request #20 from stefandoorn/providers-enable-disable
Browse files Browse the repository at this point in the history
Enable/disable default providers (fixes #19)
  • Loading branch information
stefandoorn authored Oct 19, 2017
2 parents b754a95 + 160c04b commit 5cc0048
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 26 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Get a full list of configuration: `bin/console config:dump-reference sitemap`

```yaml
sitemap:
providers:
products: true
taxons: true
static: true
template: '@SitemapPlugin/show.xml.twig'
index_template: '@SitemapPlugin/index.xml.twig'
exclude_taxon_root: true
Expand All @@ -60,6 +64,7 @@ sitemap:
### Feature switches
* `providers`: Enable/disable certain providers to be included in the sitemap. Defaults are true.
* `exclude_taxon_root`: Often you don't want to include the root of your taxon tree as it has a generic name as 'products'.
* `absolute_url`: Whether to generate absolute URL's (true) or relative (false). Defaults to true.
* `hreflang`: Whether to generate alternative URL versions for each locale. Defaults to true. Background: https://support.google.com/webmasters/answer/189077?hl=en.
Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ private function addSitemapSection(ArrayNodeDefinition $node): void
{
$node
->children()
->arrayNode('providers')
->addDefaultsIfNotSet()
->children()
->booleanNode('products')->defaultTrue()->end()
->booleanNode('taxons')->defaultTrue()->end()
->booleanNode('static')->defaultTrue()->end()
->end()
->end()
->scalarNode('template')
->defaultValue('@SitemapPlugin/show.xml.twig')
->end()
Expand Down
9 changes: 9 additions & 0 deletions src/DependencyInjection/SitemapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ public function load(array $config, ContainerBuilder $container)
$container->setParameter('sylius.sitemap_absolute_url', $config['absolute_url']);
$container->setParameter('sylius.sitemap_hreflang', $config['hreflang']);
$container->setParameter('sylius.sitemap_static', $config['static_routes']);

foreach ($config['providers'] as $provider => $setting) {
$parameter = sprintf('sylius.provider.%s', $provider);
$container->setParameter($parameter, $setting);

if ($setting === true) {
$loader->load(sprintf('services/providers/%s.xml', $provider));
}
}
}
}
13 changes: 13 additions & 0 deletions src/Resources/config/services/providers/products.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.sitemap_provider.product" class="SitemapPlugin\Provider\ProductUrlProvider">
<argument type="service" id="sylius.repository.product" />
<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" />
<tag name="sylius.sitemap_provider" />
</service>
</services>
</container>
12 changes: 12 additions & 0 deletions src/Resources/config/services/providers/static.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<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>%sylius.sitemap_static%</argument>
<tag name="sylius.sitemap_provider" />
</service>
</services>
</container>
13 changes: 13 additions & 0 deletions src/Resources/config/services/providers/taxons.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.sitemap_provider.taxon" class="SitemapPlugin\Provider\TaxonUrlProvider">
<argument type="service" id="sylius.repository.taxon" />
<argument type="service" id="router" />
<argument type="service" id="sylius.sitemap_url_factory" />
<argument type="service" id="sylius.context.locale" />
<argument>%sylius.sitemap_exclude_taxon_root%</argument>
<tag name="sylius.sitemap_provider" />
</service>
</services>
</container>
26 changes: 0 additions & 26 deletions src/Resources/config/services/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,6 @@
<tag name="routing.loader" />
</service>

<service id="sylius.sitemap_provider.product" class="SitemapPlugin\Provider\ProductUrlProvider">
<argument type="service" id="sylius.repository.product" />
<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" />
<tag name="sylius.sitemap_provider" />
</service>

<service id="sylius.sitemap_provider.taxon" class="SitemapPlugin\Provider\TaxonUrlProvider">
<argument type="service" id="sylius.repository.taxon" />
<argument type="service" id="router" />
<argument type="service" id="sylius.sitemap_url_factory" />
<argument type="service" id="sylius.context.locale" />
<argument>%sylius.sitemap_exclude_taxon_root%</argument>
<tag name="sylius.sitemap_provider" />
</service>

<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>%sylius.sitemap_static%</argument>
<tag name="sylius.sitemap_provider" />
</service>

<service id="sylius.sitemap_index_provider.index" class="SitemapPlugin\Provider\IndexUrlProvider">
<argument type="service" id="router" />
<argument type="service" id="sylius.sitemap_index_url_factory" />
Expand Down
110 changes: 110 additions & 0 deletions tests/DependencyInjection/Compiler/SitemapParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Tests\SitemapPlugin\DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use SitemapPlugin\DependencyInjection\SitemapExtension;

/**
* @author Stefan Doorn <[email protected]>
*/
class SitemapParameterTest extends AbstractExtensionTestCase
{
/**
* @test
* @dataProvider providers
*/
public function it_has_providers_enabled_by_default_with_parameter(
array $config,
bool $products,
bool $taxons,
bool $static
) {
$this->load($config);

$this->assertContainerBuilderHasParameter(sprintf('sylius.provider.%s', 'products'), $products);
$this->assertContainerBuilderHasParameter(sprintf('sylius.provider.%s', 'taxons'), $taxons);
$this->assertContainerBuilderHasParameter(sprintf('sylius.provider.%s', 'static'), $static);

if ($products) {
$this->assertContainerBuilderHasService('sylius.sitemap_provider.product', \SitemapPlugin\Provider\ProductUrlProvider::class);
$this->assertContainerBuilderHasServiceDefinitionWithTag('sylius.sitemap_provider.product', 'sylius.sitemap_provider');
}
else {
$this->assertContainerBuilderNotHasService('sylius.sitemap_provider.product');
}

if ($taxons) {
$this->assertContainerBuilderHasService('sylius.sitemap_provider.taxon', \SitemapPlugin\Provider\TaxonUrlProvider::class);
$this->assertContainerBuilderHasServiceDefinitionWithTag('sylius.sitemap_provider.taxon', 'sylius.sitemap_provider');

}
else {
$this->assertContainerBuilderNotHasService('sylius.sitemap_provider.taxon');
}

if ($static) {
$this->assertContainerBuilderHasService('sylius.sitemap_provider.static', \SitemapPlugin\Provider\StaticUrlProvider::class);
$this->assertContainerBuilderHasServiceDefinitionWithTag('sylius.sitemap_provider.static', 'sylius.sitemap_provider');

}
else {
$this->assertContainerBuilderNotHasService('sylius.sitemap_provider.static');
}
}

/**
* @return array
*/
public function providers(): array
{
return [
[
['providers' => [
'products' => true,
'taxons' => true,
'static' => true,
]],
true,
true,
true
],
[
['providers' => []],
true,
true,
true,
],
[
['providers' => [
'products' => false,
'taxons' => false,
'static' => false,
]],
false,
false,
false
],
[
['providers' => [
'products' => true,
'taxons' => false,
'static' => true,
]],
true,
false,
true,
],
];
}

/**
* @return array
*/
protected function getContainerExtensions()
{
return array(
new SitemapExtension()
);
}
}

0 comments on commit 5cc0048

Please sign in to comment.