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

Commit 4f893d2

Browse files
committed
Merge pull request #88 from symfony-cmf/conflict_resolvers
Conflict resolvers
2 parents 1184bb8 + 44bebff commit 4f893d2

File tree

15 files changed

+289
-29
lines changed

15 files changed

+289
-29
lines changed

AutoRoute/AutoRouteManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function buildUrlContextCollection(UrlContextCollection $urlContextCollec
6161
$autoRoute = $existingRoute;
6262
} else {
6363
$url = $urlContext->getUrl();
64-
$url = $this->urlGenerator->resolveConflict($url);
64+
$url = $this->urlGenerator->resolveConflict($urlContext);
6565
$urlContext->setUrl($url);
6666
}
6767
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolverInterface;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\AdapterInterface;
8+
9+
/**
10+
* This conflict resolver will generate candidate URLs by appending
11+
* a number to the URL. It will keep incrementing this number until
12+
* the URL does not exist.
13+
*
14+
* @author Daniel Leech <[email protected]>
15+
*/
16+
class AutoIncrementConflictResolver implements ConflictResolverInterface
17+
{
18+
protected $adapter;
19+
protected $inc;
20+
21+
/**
22+
* @param AdapterInterface $adapter
23+
*/
24+
public function __construct(AdapterInterface $adapter)
25+
{
26+
$this->adapter = $adapter;
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function resolveConflict(UrlContext $urlContext)
33+
{
34+
$this->inc = 0;
35+
36+
$url = $urlContext->getUrl();
37+
$candidateUrl = $this->incrementUrl($url);
38+
39+
while ($route = $this->adapter->findRouteForUrl($candidateUrl)) {
40+
$candidateUrl = $this->incrementUrl($url);
41+
}
42+
43+
return $candidateUrl;
44+
}
45+
46+
protected function incrementUrl($url)
47+
{
48+
return sprintf('%s-%s', $url, ++$this->inc);
49+
}
50+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver\Exception;
4+
5+
/**
6+
* Exception thrown when there is an existing URL and
7+
* the "ThrowException" conflict resolver is used.
8+
*
9+
* @author Daniel Leech <[email protected]>
10+
*/
11+
class ExistingUrlException extends \Exception
12+
{
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolverInterface;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\AdapterInterface;
8+
9+
/**
10+
* This conflcit resolver "resolves" conflicts by throwing exceptions.
11+
*
12+
* @author Daniel Leech <[email protected]>
13+
*/
14+
class ThrowExceptionConflictResolver implements ConflictResolverInterface
15+
{
16+
/**
17+
* {@inheritDoc}
18+
*/
19+
public function resolveConflict(UrlContext $urlContext)
20+
{
21+
$url = $urlContext->getUrl();
22+
23+
throw new Exception\ExistingUrlException(sprintf(
24+
'There already exists an auto route for URL "%s" and the system is configured ' .
25+
'to throw this exception in this case. Alternatively you can choose to use a ' .
26+
'different strategy, for example, auto incrementation. Please refer to the ' .
27+
'documentation for more information.',
28+
$url
29+
));
30+
}
31+
}
32+

AutoRoute/Mapping/ClassMetadata.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ClassMetadata extends MergeableClassMetadata
3232
protected $tokenProviders = array();
3333

3434
/**
35-
* @var null|array
35+
* @var array
3636
*/
37-
protected $conflictResolver;
37+
protected $conflictResolver = array('name' => 'throw_exception', 'options' => array());
3838

3939
/**
4040
* Defunct route handler, default to remove
@@ -122,17 +122,6 @@ public function getConflictResolver()
122122
return $this->conflictResolver;
123123
}
124124

125-
/**
126-
* Return true if a conflict resolver configuration
127-
* has been registered.
128-
*
129-
* @return boolean
130-
*/
131-
public function hasConflictResolver()
132-
{
133-
return null !== $this->conflictResolver;
134-
}
135-
136125
/**
137126
* Set the defunct route handler configuration.
138127
*
@@ -155,16 +144,6 @@ public function getDefunctRouteHandler()
155144
return $this->defunctRouteHandler;
156145
}
157146

158-
/**
159-
* Return true if a defunct route handler has been set.
160-
*
161-
* @return boolean
162-
*/
163-
public function hasDefunctRouteHandler()
164-
{
165-
return null !== $this->defunctRouteHandler;
166-
}
167-
168147
/**
169148
* Extend the metadata of the mapped class with given $name
170149
*

AutoRoute/UrlGenerator.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ public function generateUrl(UrlContext $urlContext)
6666
*/
6767
public function resolveConflict(UrlContext $urlContext)
6868
{
69-
$realClassName = $this->driver->getRealClassName($urlContext->getSubjectObject());
70-
$metadata = $this->factory->getMetadataForClass($realClassName);
69+
$realClassName = $this->driver->getRealClassName(get_class($urlContext->getSubjectObject()));
70+
$metadata = $this->metadataFactory->getMetadataForClass($realClassName);
7171

72-
list ($name, $config) = $metadata->getConflictResolverConfig();
73-
$conflictResolver = $this->serviceRegistry->getConflictResolver($name, $config);
74-
$url = $conflictResolver->resolveConflict($url);
72+
$conflictResolverConfig = $metadata->getConflictResolver();
73+
$conflictResolver = $this->serviceRegistry->getConflictResolver(
74+
$conflictResolverConfig['name'],
75+
$conflictResolverConfig['options']
76+
);
77+
$url = $conflictResolver->resolveConflict($urlContext);
7578

7679
return $url;
7780
}

DependencyInjection/CmfRoutingAutoExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
3030
$loader->load('auto_route.xml');
3131
$loader->load('token_providers.xml');
3232
$loader->load('defunct_route_handlers.xml');
33+
$loader->load('conflict_resolvers.xml');
3334

3435
$config = $processor->processConfiguration($configuration, $configs);
3536

DependencyInjection/Compiler/AutoRoutePass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function process(ContainerBuilder $container)
3535
$types = array(
3636
'token_provider' => 'registerTokenProvider',
3737
'defunct_route_handler' => 'registerDefunctRouteHandler',
38+
'conflict_resolver' => 'registerConflictResolver',
3839
);
3940

4041
foreach ($types as $type => $registerMethod) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<parameters>
7+
8+
<parameter key="cmf_routing_auto.conflict_resolver.auto_increment.class">Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver\AutoIncrementConflictResolver</parameter>
9+
<parameter key="cmf_routing_auto.conflict_resolver.throw_exception.class">Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver\ThrowExceptionConflictResolver</parameter>
10+
11+
</parameters>
12+
13+
<services>
14+
15+
<service id="cmf_routing_auto.conflict_resolver.auto_increment" class="%cmf_routing_auto.conflict_resolver.auto_increment.class%">
16+
<argument type="service" id="cmf_routing_auto.adapter.phpcr_odm" />
17+
<tag name="cmf_routing_auto.conflict_resolver" alias="auto_increment" />
18+
</service>
19+
20+
<service id="cmf_routing_auto.conflict_resolver.throw_exception" class="%cmf_routing_auto.conflict_resolver.throw_exception.class%">
21+
<tag name="cmf_routing_auto.conflict_resolver" alias="throw_exception" />
22+
</service>
23+
24+
</services>
25+
</container>
26+

Tests/Functional/Command/RefreshCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected function createBlog($withPosts = false)
3232

3333
if ($withPosts) {
3434
$post = new Post;
35+
$post->name = 'This is a post title';
3536
$post->title = 'This is a post title';
3637
$post->body = 'Test Body';
3738
$post->blog = $blog;

0 commit comments

Comments
 (0)