From 1f2cc52969c3151da82bfb2618ee10ba3e30e725 Mon Sep 17 00:00:00 2001 From: Stefan Doorn Date: Wed, 18 Oct 2017 22:25:40 +0200 Subject: [PATCH 1/2] Enable/disable default providers --- README.md | 5 + src/DependencyInjection/Configuration.php | 8 ++ src/DependencyInjection/SitemapExtension.php | 9 ++ .../config/services/providers/products.xml | 13 +++ .../config/services/providers/static.xml | 12 ++ .../config/services/providers/taxons.xml | 13 +++ src/Resources/config/services/sitemap.xml | 26 ----- .../Compiler/SitemapParameterTest.php | 110 ++++++++++++++++++ .../Compiler/SitemapProviderPassTest.php | 12 ++ 9 files changed, 182 insertions(+), 26 deletions(-) create mode 100644 src/Resources/config/services/providers/products.xml create mode 100644 src/Resources/config/services/providers/static.xml create mode 100644 src/Resources/config/services/providers/taxons.xml create mode 100644 tests/DependencyInjection/Compiler/SitemapParameterTest.php diff --git a/README.md b/README.md index a375fd7d..28e8a147 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8abd9004..17522ede 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() diff --git a/src/DependencyInjection/SitemapExtension.php b/src/DependencyInjection/SitemapExtension.php index c3312c02..9a05fb14 100644 --- a/src/DependencyInjection/SitemapExtension.php +++ b/src/DependencyInjection/SitemapExtension.php @@ -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)); + } + } } } diff --git a/src/Resources/config/services/providers/products.xml b/src/Resources/config/services/providers/products.xml new file mode 100644 index 00000000..14cb8926 --- /dev/null +++ b/src/Resources/config/services/providers/products.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/src/Resources/config/services/providers/static.xml b/src/Resources/config/services/providers/static.xml new file mode 100644 index 00000000..18c04d1a --- /dev/null +++ b/src/Resources/config/services/providers/static.xml @@ -0,0 +1,12 @@ + + + + + + + + %sylius.sitemap_static% + + + + diff --git a/src/Resources/config/services/providers/taxons.xml b/src/Resources/config/services/providers/taxons.xml new file mode 100644 index 00000000..34d42c37 --- /dev/null +++ b/src/Resources/config/services/providers/taxons.xml @@ -0,0 +1,13 @@ + + + + + + + + + %sylius.sitemap_exclude_taxon_root% + + + + diff --git a/src/Resources/config/services/sitemap.xml b/src/Resources/config/services/sitemap.xml index 22bb08b5..6fb41007 100644 --- a/src/Resources/config/services/sitemap.xml +++ b/src/Resources/config/services/sitemap.xml @@ -48,32 +48,6 @@ - - - - - - - - - - - - - - - %sylius.sitemap_exclude_taxon_root% - - - - - - - - %sylius.sitemap_static% - - - diff --git a/tests/DependencyInjection/Compiler/SitemapParameterTest.php b/tests/DependencyInjection/Compiler/SitemapParameterTest.php new file mode 100644 index 00000000..3cec7ccf --- /dev/null +++ b/tests/DependencyInjection/Compiler/SitemapParameterTest.php @@ -0,0 +1,110 @@ + + */ +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() + ); + } +} diff --git a/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php b/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php index c104ae09..19111e21 100644 --- a/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php +++ b/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php @@ -102,4 +102,16 @@ private function assertContainerBuilderDoesNotHaveServiceDefinitionWithMethodCal new \PHPUnit_Framework_Constraint_Not(new DefinitionHasMethodCallConstraint($method)) ); } + + /** + * @return array + */ + public function providers(): array + { + return [ + ['products'], + ['taxons'], + ['static'], + ]; + } } From 160c04b73f3ec45b038d502482808f2837e22a63 Mon Sep 17 00:00:00 2001 From: Stefan Doorn Date: Thu, 19 Oct 2017 14:43:14 +0200 Subject: [PATCH 2/2] Remove unused dataProvider method --- .../Compiler/SitemapProviderPassTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php b/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php index 19111e21..c104ae09 100644 --- a/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php +++ b/tests/DependencyInjection/Compiler/SitemapProviderPassTest.php @@ -102,16 +102,4 @@ private function assertContainerBuilderDoesNotHaveServiceDefinitionWithMethodCal new \PHPUnit_Framework_Constraint_Not(new DefinitionHasMethodCallConstraint($method)) ); } - - /** - * @return array - */ - public function providers(): array - { - return [ - ['products'], - ['taxons'], - ['static'], - ]; - } }