Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #248 from symfony-cmf/sitemap-helper-disable-inherit
Browse files Browse the repository at this point in the history
allow to configure sitemap helpers globally and per sitemap
  • Loading branch information
ElectricMaxxx committed Jul 31, 2015
2 parents 6f621be + a6d5777 commit 434fb20
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 60 deletions.
96 changes: 54 additions & 42 deletions DependencyInjection/CmfSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class CmfSeoExtension extends Extension
*/
private $contentListenerEnabled = false;

private $sitemapHelperMap = array(
'loaders' => 'cmf_seo.sitemap.loader',
'guessers' => 'cmf_seo.sitemap.guesser',
'voters' => 'cmf_seo.sitemap.voter'
);

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -252,29 +258,51 @@ private function loadSitemapHandling($config, XmlFileLoader $loader, ContainerBu
$loader->load('sitemap.xml');

$configurations = $config['configurations'];
// if there are no explicit configurations, enable the default sitemap
if (!count($configurations)) {
$configurations['sitemap'] = array();

$helperStatus = array();
foreach ($this->sitemapHelperMap as $helper => $tag) {
$helperStatus[$helper] = array();
$serviceDefinitionIds = $container->findTaggedServiceIds($tag);
foreach ($serviceDefinitionIds as $id => $attributes) {
if (0 === strncmp($this->getAlias(), $id, strlen($this->getAlias()))) {
// avoid interfering with services that are not part of this bundle
$helperStatus[$helper][$id] = array();
}
}
}

foreach ($configurations as $key => $configuration) {
foreach ($configurations as $sitemapName => $configuration) {
if (isset($configuration['default_change_frequency'])) {
$definition = new Definition('%cmf_seo.sitemap.guesser.default_change_frequency.class%', array(
$configuration['default_change_frequency']
));
$definition->addTag('cmf_seo.sitemap.guesser', array(
'sitemap' => $key,
'sitemap' => $sitemapName,
'priority' => -1,
));
$container->setDefinition($this->getAlias().'.sitemap.guesser.'.$key.'.default_change_frequency', $definition);
$container->setDefinition($this->getAlias().'.sitemap.guesser.'.$sitemapName.'.default_change_frequency', $definition);
}
unset($configurations[$key]['default_change_frequency']);
unset($configurations[$sitemapName]['default_change_frequency']);

// copy default configuration into this sitemap configuration to keep controller simple
foreach ($config['defaults']['templates'] as $format => $name) {
if (!isset($configurations[$key]['templates'][$format])) {
$configurations[$key]['templates'][$format] = $name;
if (!isset($configurations[$sitemapName]['templates'][$format])) {
$configurations[$sitemapName]['templates'][$format] = $name;
}
}
foreach ($helperStatus as $helper => $map) {
$status = count($configuration[$helper]) ? $configuration[$helper] : $config['defaults'][$helper];

foreach ($status as $s) {
if ('_all' === $s) {
foreach ($helperStatus[$helper] as $id => $sitemaps) {
$helperStatus[$helper][$id][] = $sitemapName;
}
} elseif ('_none' !== $s) {
$helperStatus[$helper][$s][] = $sitemapName;
}
}
unset($configurations[$sitemapName][$helper]);
}
}

Expand All @@ -285,21 +313,7 @@ private function loadSitemapHandling($config, XmlFileLoader $loader, ContainerBu
$config['defaults']['default_change_frequency']
);

// disabling/enabling loaders, guesser and voter
$helperValues = array(
'loaders' => 'cmf_seo.sitemap.loader',
'guessers' => 'cmf_seo.sitemap.guesser',
'voters' => 'cmf_seo.sitemap.voter'
);
foreach ($helperValues as $type => $tag) {
if (!isset($config['defaults'][$type])) {
throw new InvalidConfigurationException(
sprintf('The values for %s should be set.', implode(', ', array_keys($helperValues)))
);
}

$this->handleSitemapHelper($tag, $config['defaults'][$type], $container);
}
$this->handleSitemapHelper($helperStatus, $container);

if (!$alternateLocale) {
$container->removeDefinition($this->getAlias().'.sitemap.guesser.alternate_locales');
Expand All @@ -309,26 +323,24 @@ private function loadSitemapHandling($config, XmlFileLoader $loader, ContainerBu
/**
* Each helper type out of the guessers, loaders and voters hav its on configuration to enable/disable them
*
* @param string $tag The tag the services are tagged with.
* @param string $configurationValue One of none|all|<comma-separated-list-of-services-ids>
* @param array $helperStatus Map of type => id => list of sitemaps
* @param ContainerBuilder $container
*/
private function handleSitemapHelper($tag, $configurationValue, ContainerBuilder $container)
private function handleSitemapHelper($helperStatus, ContainerBuilder $container)
{
// all tagged services are active by default
if ('all' === $configurationValue) {
return;
}

/** @var Definition[] $serviceDefinitionIds */
$serviceDefinitionIds = $container->findTaggedServiceIds($tag);

if ('none' !== $configurationValue) {
$definitionsToRemoveIds = array_flip(explode(',', $configurationValue));
$serviceDefinitionIds = array_diff_key($serviceDefinitionIds, $definitionsToRemoveIds);
}

foreach ($serviceDefinitionIds as $serviceDefinitionId => $attributes) {
$container->removeDefinition($serviceDefinitionId);
foreach ($helperStatus as $type => $status) {
foreach ($status as $id => $sitemaps) {
if (count($sitemaps)) {
$definition = $container->getDefinition($id);
$tags = $definition->getTag($this->sitemapHelperMap[$type]);
$tag = reset($tags);
$tag['sitemap'] = implode(',', $sitemaps);
$definition->clearTag($this->sitemapHelperMap[$type]);
$definition->addTag($this->sitemapHelperMap[$type], $tag);
} else {
$container->removeDefinition($id);
}
}
}
}
}
57 changes: 54 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
use Symfony\Cmf\Bundle\SeoBundle\SeoPresentation;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand All @@ -33,6 +34,30 @@ public function getConfigTreeBuilder()

$nodeBuilder = $treeBuilder->root('cmf_seo')
->addDefaultsIfNotSet()
->beforeNormalization()
->ifTrue(function ($config) {
return isset($config['sitemap'])
&& (!isset($config['sitemap']['configurations'])
|| 0 == count($config['sitemap']['configurations'])
)
&& !isset($config['sitemap']['configuration']) // xml configuration
;
})
->then(function ($config) {
if (true === $config['sitemap']) {
$config['sitemap'] = array(
'enabled' => true,
'configurations' => array(
'sitemap' => array()
),
);
} elseif (is_array($config['sitemap'])) {
$config['sitemap']['configurations'] = array('sitemap' => array());
}

return $config;
})
->end()
->beforeNormalization()
->ifTrue(function ($config) {
return isset($config['content_key']) && !isset($config['content_listener']['content_key']);
Expand Down Expand Up @@ -187,22 +212,28 @@ private function addSitemapSection(NodeBuilder $nodeBuilder)
))
->prototype('scalar')->end()
->end()
->append($this->getSitemapHelperNode('loaders', array('_all')))
->append($this->getSitemapHelperNode('guessers', array('_all')))
->append($this->getSitemapHelperNode('voters', array('_all')))
->end()
->end()
->arrayNode('configurations')
->useAttributeAsKey('name')
->prototype('array')
->fixXmlConfig('template')
->fixXmlConfig('loader')
->fixXmlConfig('guesser')
->fixXmlConfig('voter')
->children()
->scalarNode('default_change_frequency')->defaultNull()->end()
->arrayNode('templates')
->useAttributeAsKey('format')
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->scalarNode('loaders')->defaultValue('all')->end()
->scalarNode('guessers')->defaultValue('all')->end()
->scalarNode('voters')->defaultValue('all')->end()
->append($this->getSitemapHelperNode('loaders', array()))
->append($this->getSitemapHelperNode('guessers', array()))
->append($this->getSitemapHelperNode('voters', array()))
->end()
->end()
->end()
Expand All @@ -211,6 +242,26 @@ private function addSitemapSection(NodeBuilder $nodeBuilder)
;
}

private function getSitemapHelperNode($type, $default)
{
$node = new ArrayNodeDefinition($type);
$node
->beforeNormalization()
->ifTrue(function ($config) {
return is_string($config);
})
->then(function ($config) {
return array($config);
})
->end()
->defaultValue($default)
->prototype('scalar')->end()
->end()
;

return $node;
}

/**
* Attach the content listener node to the tree.
*
Expand Down
6 changes: 3 additions & 3 deletions Resources/config/schema/seo-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
<xsd:complexType name="sitemap_configuration">
<xsd:sequence>
<xsd:element name="template" type="sitemap_template" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="loader" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="voter" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="guesser" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="default-change-frequency" type="change_frequency" default="always" />

<xsd:attribute name="guessers" type="xsd:string" />
<xsd:attribute name="loaders" type="xsd:string" />
<xsd:attribute name="voters" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="named_sitemap_configuration">
Expand Down
5 changes: 4 additions & 1 deletion Tests/Resources/Fixtures/config/config2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
<error enable-parent-provider="true" enable-sibling-provider="true" />

<sitemap enabled="true">
<configuration default-change-frequency="never" name="default" guessers="none" voters="none" loaders="none">
<configuration default-change-frequency="never" name="default">
<template format="xml">test.xml</template>
<template format="html">test.html</template>
<loader>_none</loader>
<voter>_none</voter>
<guesser>_none</guesser>
</configuration>
</sitemap>

Expand Down
11 changes: 6 additions & 5 deletions Tests/Unit/DependencyInjection/CmfSeoExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ public function testSitemapConfiguration()
'cmf_seo.sitemap.phpcr_loader',
'Symfony\Cmf\Bundle\SeoBundle\Doctrine\Phpcr\SitemapDocumentProvider'
);

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'cmf_seo.sitemap.phpcr_loader',
'cmf_seo.sitemap.loader',
array('priority' => -2)
array('priority' => -2, 'sitemap' => 'default,some_other')
);
$this->assertContainerBuilderHasService(
'cmf_seo.sitemap.voter_chain',
Expand All @@ -301,7 +302,7 @@ public function testSitemapConfiguration()
$this->assertContainerBuilderHasServiceDefinitionWithTag(
'cmf_seo.sitemap.publish_workflow_voter',
'cmf_seo.sitemap.voter',
array('priority' => -2)
array('priority' => -2, 'sitemap' => 'default,some_other')
);
$this->assertContainerBuilderHasService(
'cmf_seo.sitemap.provider',
Expand All @@ -318,7 +319,7 @@ public function testSitemapConfiguration()
$this->assertContainerBuilderHasServiceDefinitionWithTag(
$guesser,
'cmf_seo.sitemap.guesser',
array('priority' => -2)
array('priority' => -2, 'sitemap' => 'default,some_other')
);
}
$this->assertContainerBuilderHasServiceDefinitionWithArgument(
Expand Down Expand Up @@ -393,9 +394,9 @@ public function testDisablingSitemapHelpers()
'sitemap' => array(
'defaults' => array(
'default_change_frequency' => 'global-frequency',
'loaders' => 'all',
'loaders' => '_all',
'guessers' => 'cmf_seo.sitemap.guesser.default_change_frequency',
'voters' => 'none',
'voters' => '_none',
),
)
));
Expand Down
15 changes: 9 additions & 6 deletions Tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public function testDefaultsForAllConfigFormats()
'html' => 'CmfSeoBundle:Sitemap:index.html.twig',
'xml' => 'CmfSeoBundle:Sitemap:index.xml.twig',
),
'loaders' => 'all',
'guessers' => 'all',
'voters' => 'all',
'loaders' => array('_all'),
'guessers' => array('_all'),
'voters' => array('_all'),
),
),
'content_listener' => array(
Expand Down Expand Up @@ -103,6 +103,9 @@ public function testAdvancedXmlConfigurations()
'xml' => 'test.xml',
'html' => 'test.html',
),
'loaders' => array(),
'guessers' => array(),
'voters' => array(),
),
),
'defaults' => array(
Expand All @@ -111,9 +114,9 @@ public function testAdvancedXmlConfigurations()
'html' => 'foo.html.twig',
'xml' => 'foo.xml.twig',
),
'loaders' => 'all',
'guessers' => 'all',
'voters' => 'all',
'loaders' => array('_all'),
'guessers' => array('_all'),
'voters' => array('_all'),
),
),
'persistence' => array(
Expand Down

0 comments on commit 434fb20

Please sign in to comment.