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

Commit e7c498c

Browse files
committed
Content Method Provider
1 parent 4c1953f commit e7c498c

File tree

10 files changed

+238
-17
lines changed

10 files changed

+238
-17
lines changed

AutoRoute/ServiceRegistry.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface;
6+
57
class ServiceRegistry
68
{
79
protected $tokenProviders = array();
@@ -33,7 +35,7 @@ public function getTokenProvider($name)
3335
*
3436
* @return ConflictResolverInterface
3537
*/
36-
public function getConflcitResolver($name)
38+
public function getConflictResolver($name)
3739
{
3840
if (!isset($this->conflictResolvers[$name])) {
3941
throw new \InvalidArgumentException(sprintf(
@@ -45,13 +47,13 @@ public function getConflcitResolver($name)
4547
return $this->conflictResolvers[$name];
4648
}
4749

48-
public function regsiterTokenProvider(TokenProviderInterface $provider)
50+
public function registerTokenProvider($name, TokenProviderInterface $provider)
4951
{
50-
$this->tokenProviders[$provider->getName()] = $provider;
52+
$this->tokenProviders[$name] = $provider;
5153
}
5254

53-
public function registerConflictResolver(ConflictResolverInterface $conflictResolver)
55+
public function registerConflictResolver($name, ConflictResolverInterface $conflictResolver)
5456
{
55-
$this->conflictResolver[$conflictResolver->getName()] = $conflictResolver;
57+
$this->conflictResolvers[$name] = $conflictResolver;
5658
}
5759
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProvider;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface;
6+
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
8+
9+
class ContentMethodProvider implements TokenProviderInterface
10+
{
11+
protected $slugifier;
12+
13+
public function __construct(SlugifierInterface $slugifier)
14+
{
15+
$this->slugifier = $slugifier;
16+
}
17+
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
public function provideValue($object, $options)
22+
{
23+
$method = $options['method'];
24+
25+
if (!method_exists($object, $method)) {
26+
throw new \InvalidArgumentException(sprintf(
27+
'Method "%s" does not exist on object "%s"',
28+
$method,
29+
get_class($object)
30+
));
31+
}
32+
33+
$value = $object->$method();
34+
35+
if ($options['slugify']) {
36+
$value = $this->slugifier->slugify($value);
37+
}
38+
39+
return $value;
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function configureOptions(OptionsResolverInterface $optionsResolver)
46+
{
47+
$optionsResolver->setRequired(array(
48+
'method',
49+
));
50+
51+
$optionsResolver->setDefaults(array(
52+
'slugify' => true,
53+
));
54+
55+
$optionsResolver->setAllowedTypes(array(
56+
'slugify' => 'bool',
57+
));
58+
}
59+
}

AutoRoute/TokenProviderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface TokenProviderInterface
1515
*
1616
* @return string
1717
*/
18-
public function getValue($document, $options);
18+
public function provideValue($document, $options);
1919

2020
/**
2121
* Configure the options for this token provider

AutoRoute/UrlGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function generateUrl($document)
4444

4545
$tokens = array();
4646
foreach ($tokenProviderConfigs as $name => $options) {
47-
$tokenProvider = $this->serviceRegistry->getTokenProvider($options['provider']);
48-
$tokens['{' . $name . '}'] = $tokenProvider->getValue($document, $options);
47+
$tokenProvider = $this->serviceRegistry->getTokenProvider($options['name']);
48+
$tokens['{' . $name . '}'] = $tokenProvider->provideValue($document, $options);
4949
}
5050

5151
$urlSchema = $metadata->getUrlSchema();

DependencyInjection/CmfRoutingAutoExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function load(array $configs, ContainerBuilder $container)
2828
$configuration = new Configuration();
2929
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3030
$loader->load('auto_route.xml');
31+
$loader->load('token_providers.xml');
3132

3233
$config = $processor->processConfiguration($configuration, $configs);
3334

DependencyInjection/Compiler/AutoRoutePass.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Reference;
1617

1718
/**
1819
* @author Daniel Leech <[email protected]>
@@ -22,23 +23,20 @@ class AutoRoutePass implements CompilerPassInterface
2223
public function process(ContainerBuilder $container)
2324
{
2425
if (!$container->hasDefinition(
25-
'cmf_routing_auto.factory'
26+
'cmf_routing_auto.service_registry'
2627
)) {
2728
return;
2829
}
2930

3031
$builderUnitChainFactory = $container->getDefinition(
31-
'cmf_routing_auto.factory'
32+
'cmf_routing_auto.service_registry'
3233
);
3334

3435
$types = array(
35-
'provider',
36-
'exists_action',
37-
'not_exists_action',
38-
'route_maker'
36+
'token_provider' => 'registerTokenProvider',
3937
);
4038

41-
foreach ($types as $type) {
39+
foreach ($types as $type => $registerMethod) {
4240
$ids = $container->findTaggedServiceIds('cmf_routing_auto.'.$type);
4341
foreach ($ids as $id => $attributes) {
4442
if (!isset($attributes[0]['alias'])) {
@@ -50,8 +48,8 @@ public function process(ContainerBuilder $container)
5048
}
5149

5250
$builderUnitChainFactory->addMethodCall(
53-
'registerAlias',
54-
array($type, $attributes[0]['alias'], $id));
51+
$registerMethod,
52+
array($attributes[0]['alias'], new Reference($id)));
5553
}
5654
}
5755
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog:
22
url_schema: /test/blog/{blog_title}
3+
token_providers:
4+
blog_title: [ content_method, [ method: getTitle ] ]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ServiceRegistry;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\OperationStack;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\BaseTestCase;
8+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface;
9+
10+
class ServiceRegistryTest extends \PHPUnit_Framework_TestCase
11+
{
12+
private $serviceRegistry;
13+
private $tokenProvider;
14+
private $conflictResolver;
15+
16+
public function setUp()
17+
{
18+
$this->serviceRegistry = new ServiceRegistry();
19+
$this->tokenProvider = $this->getMock('Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface');
20+
$this->conflictResolver = $this->getMock('Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolverInterface');
21+
}
22+
23+
public function testRegistration()
24+
{
25+
$tps = array('tp_1', 'tp_2');
26+
$crs = array('cr_1', 'cr_2');
27+
28+
foreach ($tps as $tp) {
29+
$this->serviceRegistry->registerTokenProvider($tp, $this->tokenProvider);
30+
}
31+
32+
foreach ($crs as $cr) {
33+
$this->serviceRegistry->registerConflictResolver($cr, $this->conflictResolver);
34+
}
35+
36+
foreach ($tps as $tp) {
37+
$res = $this->serviceRegistry->getTokenProvider($tp);
38+
$this->assertSame($this->tokenProvider, $res);
39+
}
40+
41+
foreach ($crs as $cr) {
42+
$res = $this->serviceRegistry->getConflictResolver($cr);
43+
$this->assertSame($this->conflictResolver, $res);
44+
}
45+
46+
}
47+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\AutoRoute\TokenProvider;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\BaseTestCase;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\PhpcrOdmAdapter;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProvider\ContentMethodProvider;
8+
9+
class ContentMethodTest extends BaseTestCase
10+
{
11+
protected $slugifier;
12+
protected $article;
13+
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->slugifier = $this->prophet->prophesize('Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface');
19+
$this->article = $this->prophet->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article');
20+
$this->provider = new ContentMethodProvider($this->slugifier->reveal());
21+
}
22+
23+
public function provideGetValue()
24+
{
25+
return array(
26+
array(
27+
array(
28+
'method' => 'getTitle',
29+
'slugify' => true,
30+
),
31+
true,
32+
),
33+
array(
34+
array(
35+
'method' => 'getTitle',
36+
'slugify' => false,
37+
),
38+
true,
39+
),
40+
array(
41+
array(
42+
'method' => 'getMethodNotExist',
43+
'slugify' => false,
44+
),
45+
false,
46+
),
47+
);
48+
}
49+
50+
/**
51+
* @dataProvider provideGetValue
52+
*/
53+
public function testGetValue($options, $methodExists = false)
54+
{
55+
$method = $options['method'];
56+
57+
if (!$methodExists) {
58+
$this->setExpectedException(
59+
'InvalidArgumentException', 'Method "' . $options['method'] . '" does not exist'
60+
);
61+
} else {
62+
$expectedResult = 'This is value';
63+
$this->article->$method()->willReturn($expectedResult);
64+
}
65+
66+
if ($options['slugify']) {
67+
$expectedResult = 'this-is-value';
68+
$this->slugifier->slugify('This is value')->willReturn($expectedResult);
69+
}
70+
71+
$res = $this->provider->provideValue($this->article->reveal(), $options);
72+
73+
$this->assertEquals($expectedResult, $res);
74+
}
75+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\DependencyInjection\Compiler;
4+
use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Compiler\AutoRoutePass;
5+
use Symfony\Component\DependencyInjection\Definition;
6+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Reference;
9+
10+
class AutoRoutePassTest extends AbstractCompilerPassTestCase
11+
{
12+
protected function registerCompilerPass(ContainerBuilder $container)
13+
{
14+
$container->addCompilerPass(new AutoRoutePass());
15+
}
16+
17+
public function testRegistration()
18+
{
19+
$serviceRegistryDefinition = new Definition();
20+
$this->setDefinition('cmf_routing_auto.service_registry', $serviceRegistryDefinition);
21+
22+
$tokenProviderDefinition = new Definition();
23+
$tokenProviderDefinition->addTag('cmf_routing_auto.token_provider', array('alias' => 'foobar'));
24+
$this->setDefinition('some_token_provider', $tokenProviderDefinition);
25+
$this->compile();
26+
27+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
28+
'cmf_routing_auto.service_registry',
29+
'registerTokenProvider',
30+
array(
31+
'foobar',
32+
new Reference('some_token_provider')
33+
)
34+
);
35+
36+
}
37+
}

0 commit comments

Comments
 (0)