Skip to content

Commit 5aa2ff1

Browse files
einorlersaimaz
authored andcommitted
Router bug fix (#91)
* introduced disable_alias parameter in config * updated the documentation * updated the tests * fixed seo_routes configuration merging issue * made supports return false in stead of an error * implemented config of the ongr router priority * added documentation about the router priority
1 parent 5227bff commit 5aa2ff1

File tree

9 files changed

+39
-6
lines changed

9 files changed

+39
-6
lines changed

DependencyInjection/Compiler/SetRouterPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ class SetRouterPass implements CompilerPassInterface
1919
{
2020
public function process(ContainerBuilder $container)
2121
{
22-
$container->setAlias('router', 'ongr_router.chain_router');
22+
if (!$container->getParameter('ongr_router.disable_alias')) {
23+
$container->setAlias('router', 'ongr_router.chain_router');
24+
}
25+
26+
$container
27+
->getDefinition('ongr_router.dynamic_router')
28+
->addTag('router', ['priority' => $container->getParameter('ongr_router.router_priority')]);
2329
$container
2430
->getDefinition('ongr_router.elasticsearch_route_provider')
2531
->addMethodCall(

DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,22 @@ public function getConfigTreeBuilder()
3232

3333
$rootNode
3434
->children()
35+
->booleanNode('disable_alias')
36+
->info('disables aliasing of Symfony router in case it is done somewhere else')
37+
->defaultFalse()
38+
->end()
39+
->integerNode('router_priority')
40+
->defaultValue(-100)
41+
->info('The priority with which the ONGR router is set to the chain router')
42+
->end()
3543
->scalarNode('manager')
3644
->defaultValue('es.manager.default')
3745
->info('Elasticsearch manager to use in the ONGR default router')
3846
->example('es.manager.default')
3947
->end()
4048
->arrayNode('seo_routes')
4149
->defaultValue([])
50+
->useAttributeAsKey('name')
4251
->prototype('scalar')->end()
4352
->end()
4453
->end();

DependencyInjection/ONGRRouterExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function load(array $configs, ContainerBuilder $container)
3636
$loader->load('services.yml');
3737

3838
$container->setParameter('ongr_router.manager', $config['manager']);
39+
$container->setParameter('ongr_router.disable_alias', $config['disable_alias']);
3940
$container->setParameter('ongr_router.seo_routes', $config['seo_routes']);
41+
$container->setParameter('ongr_router.router_priority', $config['router_priority']);
4042
}
4143
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ ongr_elasticsearch:
8282
- AppBundle
8383

8484
ongr_router:
85+
#disable_alias: true # defaults to false
86+
#router_priority: 1000 # defaults to -100
8587
manager: es.manager.default
8688
seo_routes:
8789
'AppBundle:Product': AppBundle:Product:document
@@ -98,6 +100,13 @@ At `_controller` you define controller and action for every document type.
98100

99101
`urlAnalyzer` at `ongr_elasticsearch` configuration defines how all url fields are analyzed by Elasticsearch.
100102

103+
If you are using another third party bundle that also has an aliased Symfony router, you may set `disable_alias` to `true`. This
104+
prevents possible conflicts.
105+
106+
`router_priority` parameter defines the priority with which the `ONGR` dynamic router
107+
is set to the chain router. It defaults to -100 in order to be called after the standard Symfony router
108+
but this value can be changed depending on your projects' need.
109+
101110
Check [Elasticsearch bundle mappings docs](https://github.com/ongr-io/ElasticsearchBundle/blob/master/Resources/doc/mapping.md) for more information about the configuration.
102111

103112

Resources/config/services.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,3 @@ services:
3636
ongr_router.dynamic_router:
3737
class: Symfony\Cmf\Component\Routing\DynamicRouter
3838
arguments: ["@router.request_context", "@ongr_router.nested_matcher", "@ongr_router.url_generator", '', "@event_dispatcher", "@ongr_router.elasticsearch_route_provider"]
39-
tags:
40-
- { name: router, priority: -100 }

Routing/DocumentUrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function supports($name)
113113
if ($name instanceof SeoAwareInterface) {
114114
return true;
115115
} else {
116-
throw new RouteNotFoundException('$name must be an instance of SeoAwareInterface');
116+
return false;
117117
}
118118
}
119119
/**

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function testConfiguration()
3838
$expectedConfiguration = [
3939
'manager' => 'es.manager.default',
4040
'seo_routes' => $configs['ongr_router']['seo_routes'],
41+
'disable_alias' => false,
42+
'router_priority' => -100,
4143
];
4244
$this->assertEquals($expectedConfiguration, $processorConfig);
4345
}

Tests/Unit/DependencyInjection/RoutePassTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,20 @@ public function getContainer()
4949
['routes to be changed']
5050
]
5151
);
52+
$dynamicRouter = new Definition(
53+
'Symfony\Cmf\Component\Routing\DynamicRouter'
54+
);
5255
$container->setDefinition(
5356
'ongr_router.elasticsearch_route_provider',
5457
$provider
5558
);
59+
$container->setDefinition(
60+
'ongr_router.dynamic_router',
61+
$dynamicRouter
62+
);
5663
$container->setParameter('ongr_router.seo_routes', $routes);
64+
$container->setParameter('ongr_router.disable_alias', false);
65+
$container->setParameter('ongr_router.router_priority', -100);
5766
$container->setParameter('ongr_router.manager', 'es.manager.default');
5867

5968
return $container;

Tests/Unit/Routing/ElasticsearchRouteProviderTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public function testNoRouteMapDefined()
116116
'GET'
117117
);
118118
$provider->getRouteCollectionForRequest($request);
119-
120119
}
121120

122121
/**
@@ -135,6 +134,5 @@ public function testNoManagerDefined()
135134
'GET'
136135
);
137136
$provider->getRouteCollectionForRequest($request);
138-
139137
}
140138
}

0 commit comments

Comments
 (0)