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

Commit 70ae5f2

Browse files
committed
Merge pull request #150 from symfony-cmf/adapter_registration
Sulu changes: Added possiblity to register custom adapters etc.
2 parents 71ca30c + 16152a4 commit 70ae5f2

File tree

13 files changed

+223
-45
lines changed

13 files changed

+223
-45
lines changed

CmfRoutingAutoBundle.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414

1515
use Symfony\Component\HttpKernel\Bundle\Bundle;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17-
use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Compiler\AutoRoutePass;
1817
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
18+
use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Compiler\ServicePass;
19+
use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Compiler\AdapterPass;
1920

2021
class CmfRoutingAutoBundle extends Bundle
2122
{
2223
public function build(ContainerBuilder $container)
2324
{
2425
parent::build($container);
25-
$container->addCompilerPass(new AutoRoutePass());
26+
$container->addCompilerPass(new ServicePass());
27+
$container->addCompilerPass(new AdapterPass());
2628
$this->buildPhpcrCompilerPass($container);
2729
}
2830

@@ -40,16 +42,20 @@ private function buildPhpcrCompilerPass(ContainerBuilder $container)
4042
return;
4143
}
4244

43-
$container->addCompilerPass(
44-
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
45-
array(
46-
realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model',
47-
),
48-
array('cmf_routing_auto.persistence.phpcr.manager_name'),
49-
false,
50-
array('CmfRoutingAutoBundle' => 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model')
51-
)
52-
);
45+
$bundles = $container->getParameter('kernel.bundles');
46+
47+
if (isset($bundles['CmfRoutingBundle'])) {
48+
$container->addCompilerPass(
49+
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
50+
array(
51+
realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model',
52+
),
53+
array('cmf_routing_auto.persistence.phpcr.manager_name'),
54+
false,
55+
array('CmfRoutingAutoBundle' => 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model')
56+
)
57+
);
58+
}
5359
}
5460

5561
}

DependencyInjection/CmfRoutingAutoExtension.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,29 @@ public function load(array $configs, ContainerBuilder $container)
5353
$container->setParameter('cmf_routing_auto.metadata.loader.resources', $resources);
5454

5555
$hasProvider = false;
56+
57+
$adapterName = null;
58+
if (isset($config['adapter'])) {
59+
$adapterName = $config['adapter'];
60+
}
61+
5662
if ($this->isConfigEnabled($container, $config['persistence']['phpcr'])) {
5763
$hasProvider = true;
5864
$loader->load('phpcr-odm.xml');
65+
if (null === $adapterName) {
66+
$adapterName = 'doctrine_phpcr_odm';
67+
}
5968
$container->setParameter('cmf_routing_auto.persistence.phpcr.route_basepath', $config['persistence']['phpcr']['route_basepath']);
6069
}
6170

62-
if (!$hasProvider) {
63-
throw new InvalidConfigurationException(
64-
'The RoutingAutoBundle requires that you enable one of the persistence layers in your application configuration. ' .
65-
'See the documentation for more information'
66-
);
71+
if (false === $hasProvider && null === $adapterName) {
72+
throw new InvalidConfigurationException(sprintf(
73+
'No adapter has been configured, you either need to enable a persistence layer or '.
74+
'explicitly specify an adapter using the "adapter" configuration key.'
75+
));
6776
}
77+
78+
$container->setParameter('cmf_routing_auto.adapter_name', $adapterName);
6879
}
6980

7081
protected function findMappingFiles($bundles)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Compiler;
14+
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* @author Daniel Leech <[email protected]>
21+
*/
22+
class AdapterPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container)
25+
{
26+
if (!$container->hasDefinition(
27+
'cmf_routing_auto.auto_route_manager'
28+
)) {
29+
return;
30+
}
31+
32+
$adapter = $container->getParameter('cmf_routing_auto.adapter_name');
33+
$adapterId = null;
34+
$adapterNames = array();
35+
$ids = $container->findTaggedServiceIds('cmf_routing_auto.adapter');
36+
37+
foreach ($ids as $id => $attributes) {
38+
if (!isset($attributes[0]['alias'])) {
39+
throw new \InvalidArgumentException(sprintf(
40+
'No "name" specified for auto route adapter "%s"',
41+
$id
42+
));
43+
}
44+
45+
$alias = $attributes[0]['alias'];
46+
$adapterNames[] = $alias;
47+
if ($adapter === $alias) {
48+
$adapterId = $id;
49+
break;
50+
}
51+
}
52+
53+
if (null === $adapterId) {
54+
throw new \RuntimeException(sprintf(
55+
'Could not find configured adapter "%s", available adapters: "%s"',
56+
$adapter,
57+
implode('", "', $adapterNames)
58+
));
59+
}
60+
61+
$managerDef = $container->getDefinition('cmf_routing_auto.auto_route_manager');
62+
$container->setAlias('cmf_routing_auto.adapter', $adapterId);
63+
}
64+
}

DependencyInjection/Compiler/AutoRoutePass.php renamed to DependencyInjection/Compiler/ServicePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @author Daniel Leech <[email protected]>
2121
*/
22-
class AutoRoutePass implements CompilerPassInterface
22+
class ServicePass implements CompilerPassInterface
2323
{
2424
public function process(ContainerBuilder $container)
2525
{

DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public function getConfigTreeBuilder()
2626
{
2727
$treeBuilder = new TreeBuilder();
2828
$treeBuilder->root('cmf_routing_auto')
29+
->addDefaultsIfNotSet()
2930
->children()
31+
->scalarNode('adapter')->info('Use a specific adapter, overrides any implicit selection')->end()
3032
->booleanNode('auto_mapping')->defaultTrue()->end()
3133
->arrayNode('mapping')
3234
->fixXmlConfig('resource')

Resources/config/auto_route.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<parameter key="cmf_routing_auto.phpcrodm_auto_route_listener.class">Symfony\Cmf\Bundle\RoutingAutoBundle\Doctrine\Phpcr\AutoRouteListener</parameter>
99
<parameter key="cmf_routing_auto.auto_route_manager.class">Symfony\Cmf\Component\RoutingAuto\AutoRouteManager</parameter>
1010
<parameter key="cmf_routing_auto.service_registry.class">Symfony\Cmf\Component\RoutingAuto\ServiceRegistry</parameter>
11-
<parameter key="cmf_routing_auto.adapter.phpcr_odm.class">Symfony\Cmf\Bundle\RoutingAutoBundle\Adapter\PhpcrOdmAdapter</parameter>
1211
<parameter key="cmf_routing_auto.uri_generator.class">Symfony\Cmf\Component\RoutingAuto\UriGenerator</parameter>
1312

1413
<parameter key="cmf_routing_auto.metadata.loader.yaml.class">Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\YmlFileLoader</parameter>
@@ -35,15 +34,15 @@
3534
id="cmf_routing_auto.auto_route_manager"
3635
class="Symfony\Cmf\Component\RoutingAuto\AutoRouteManager"
3736
>
38-
<argument type="service" id="cmf_routing_auto.adapter.phpcr_odm"/>
37+
<argument type="service" id="cmf_routing_auto.adapter" />
3938
<argument type="service" id="cmf_routing_auto.uri_generator" />
4039
<argument type="service" id="cmf_routing_auto.defunct_route_handler.delegating" />
4140
</service>
4241

4342
<!-- URL Generator -->
4443
<service id="cmf_routing_auto.uri_generator" class="%cmf_routing_auto.uri_generator.class%">
4544
<argument type="service" id="cmf_routing_auto.metadata.factory" />
46-
<argument type="service" id="cmf_routing_auto.adapter.phpcr_odm" />
45+
<argument type="service" id="cmf_routing_auto.adapter" />
4746
<argument type="service" id="cmf_routing_auto.service_registry" />
4847
</service>
4948

Resources/config/conflict_resolvers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<services>
1414

1515
<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" />
16+
<argument type="service" id="cmf_routing_auto.adapter" />
1717
<tag name="cmf_routing_auto.conflict_resolver" alias="auto_increment" />
1818
</service>
1919

Resources/config/defunct_route_handlers.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
<services>
1515

1616
<service id="cmf_routing_auto.defunct_route_handler.remove" class="%cmf_routing_auto.defunct_route_handler.remove.class%">
17-
<argument type="service" id="cmf_routing_auto.adapter.phpcr_odm" />
17+
<argument type="service" id="cmf_routing_auto.adapter" />
1818
<tag name="cmf_routing_auto.defunct_route_handler" alias="remove" />
1919
</service>
2020

2121
<service id="cmf_routing_auto.defunct_route_handler.leave_redirect" class="%cmf_routing_auto.defunct_route_handler.leave_redirect.class%">
22-
<argument type="service" id="cmf_routing_auto.adapter.phpcr_odm" />
22+
<argument type="service" id="cmf_routing_auto.adapter" />
2323
<tag name="cmf_routing_auto.defunct_route_handler" alias="leave_redirect" />
2424
</service>
2525

2626
<service id="cmf_routing_auto.defunct_route_handler.delegating" class="%cmf_routing_auto.defunct_route_handler.delegating.class%">
2727
<argument type="service" id="cmf_routing_auto.metadata.factory" />
28-
<argument type="service" id="cmf_routing_auto.adapter.phpcr_odm" />
28+
<argument type="service" id="cmf_routing_auto.adapter" />
2929
<argument type="service" id="cmf_routing_auto.service_registry" />
3030
</service>
3131

Resources/config/phpcr-odm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<service id="cmf_routing_auto.adapter.phpcr_odm" class="%cmf_routing_auto.adapter.phpcr_odm.class%">
1616
<argument type="service" id="doctrine_phpcr.odm.default_document_manager"/>
1717
<argument>%cmf_routing_auto.persistence.phpcr.route_basepath%</argument>
18+
<tag name="cmf_routing_auto.adapter" alias="doctrine_phpcr_odm" />
1819
</service>
1920

2021
<service id="cmf_routing_auto.phpcrodm_auto_route_listener" class="%cmf_routing_auto.phpcrodm_auto_route_listener.class%">

Tests/Unit/Adapter/PhpcrOdmAdapterTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public function testCreateAutoRoute($path, $expectedParentPath, $expectedName, $
116116
}
117117

118118
$this->uriContext->getUri()->willReturn($path);
119-
120119
$res = $this->adapter->createAutoRoute($this->uriContext->reveal(), $this->contentDocument, 'fr');
121120
$this->assertNotNull($res);
122121
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $res);

0 commit comments

Comments
 (0)