Skip to content

Commit

Permalink
Allow to set the default section for static routes (prestaconcept#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
yann-eugone authored Apr 13, 2019
1 parent b2271f0 commit bcd69da
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function getConfigTreeBuilder()
->scalarNode('lastmod')->defaultValue('now')->end()
->end()
->end()
->scalarNode('default_section')
->defaultValue('default')
->info('The default section in which static routes are registered.')
->end()
->end()
;

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/PrestaSitemapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter($this->getAlias() . '.sitemap_file_prefix', $config['sitemap_file_prefix']);
$container->setParameter($this->getAlias() . '.items_by_set', $config['items_by_set']);
$container->setParameter($this->getAlias() . '.defaults', $config['defaults']);
$container->setParameter($this->getAlias() . '.default_section', $config['default_section']);

if (true === $config['route_annotation_listener']) {
$loader->load('route_annotation_listener.xml');
Expand Down
13 changes: 10 additions & 3 deletions EventListener/RouteAnnotationEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ class RouteAnnotationEventListener implements EventSubscriberInterface
*/
protected $router;

/**
* @var string
*/
private $defaultSection;

/**
* @param RouterInterface $router
* @param string $defaultSection
*/
public function __construct(RouterInterface $router)
public function __construct(RouterInterface $router, $defaultSection)
{
$this->router = $router;
$this->defaultSection = $defaultSection;
}

/**
Expand All @@ -67,7 +74,7 @@ public function registerRouteAnnotation(SitemapPopulateEvent $event)
{
$section = $event->getSection();

if (is_null($section) || $section == 'default') {
if (is_null($section) || $section === $this->defaultSection) {
$this->addUrlsFromRoutes($event);
}
}
Expand All @@ -89,7 +96,7 @@ private function addUrlsFromRoutes(SitemapPopulateEvent $event)
continue;
}

$section = $event->getSection() ?: 'default';
$section = $event->getSection() ?: $this->defaultSection;
if (isset($options['section'])) {
$section = $options['section'];
}
Expand Down
1 change: 1 addition & 0 deletions Resources/config/route_annotation_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<services>
<service id="presta_sitemap.eventlistener.route_annotation" class="%presta_sitemap.eventlistener.route_annotation.class%">
<argument type="service" id="router"/>
<argument>%presta_sitemap.default_section%</argument>
<tag name="kernel.event_subscriber"/>
</service>
</services>
Expand Down
8 changes: 8 additions & 0 deletions Resources/doc/2-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ presta_sitemap:
lastmod: now
```
Or choose the default sections for static routes:
```yaml
# config/packages/presta_sitemap.yaml
presta_sitemap:
default_section: default
```
## Time to live
You may want to change the default `3600` seconds max-age set when rendering the
Expand Down
3 changes: 3 additions & 0 deletions Resources/doc/3-static-routes-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ The supported sitemap parameters are:
one of `"always"`, `"hourly"`, `"daily"`, `"weekly"`, `"monthly"`, `"yearly"`, `"never"` (default: `"daily"`)
* `"priority"`: a number between `0` and `1` (default: `1`)

> **Note** you can change defaults in the bundle configuration.
> Jump to [dedicated documentation](2-configuration.md) for more information.

## Annotation

Expand Down
10 changes: 2 additions & 8 deletions Tests/EventListener/RouteAnnotationEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,9 @@ private function getRouter()
*/
private function getListener()
{
$listener = new RouteAnnotationEventListener(
return new RouteAnnotationEventListener(
$this->getRouter(),
array(
'priority' => 1,
'changefreq' => UrlConcrete::CHANGEFREQ_DAILY,
'lastmod' => 'now',
)
'default'
);

return $listener;
}
}

0 comments on commit bcd69da

Please sign in to comment.