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

Commit 2e719d2

Browse files
authored
Merge pull request #34 from symfony-cmf/phpcr_odm_enhancer
Added doctrine phpcr odm description enhancer
2 parents 76f8b19 + 6e96ba1 commit 2e719d2

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

DependencyInjection/Compiler/DescriptionEnhancerPass.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,26 @@ public function process(ContainerBuilder $container)
2626

2727
$taggedIds = $container->findTaggedServiceIds('cmf_resource.description.enhancer');
2828
$enabledEnhancers = $container->getParameter('cmf_resource.description.enabled_enhancers');
29+
$validAttributes = ['name', 'alias', 'priority'];
2930

3031
foreach ($taggedIds as $serviceId => $attributes) {
31-
if (!isset($attributes[0]['alias'])) {
32+
$attributes = $attributes[0];
33+
34+
if ($diff = array_diff(array_keys($attributes), $validAttributes)) {
35+
throw new InvalidArgumentException(sprintf(
36+
'Unknown tag attributes "%s" for service "%s", valid attributes: "%s"',
37+
implode('", "', $diff), $serviceId, implode('", "', $validAttributes)
38+
));
39+
}
40+
41+
if (!isset($attributes['alias'])) {
3242
throw new InvalidArgumentException(sprintf(
3343
'Resource enhancer "%s" has no "alias" attribute in its tag',
3444
$serviceId
3545
));
3646
}
3747

38-
$name = $attributes[0]['alias'];
48+
$name = $attributes['alias'];
3949

4050
if (isset($enhancers[$name])) {
4151
throw new InvalidArgumentException(sprintf(
@@ -45,7 +55,8 @@ public function process(ContainerBuilder $container)
4555
));
4656
}
4757

48-
$enhancers[$name] = new Reference($serviceId);
58+
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
59+
$enhancers[$name] = [$priority, new Reference($serviceId)];
4960
}
5061

5162
$enhancerNames = array_keys($enhancers);
@@ -61,10 +72,18 @@ public function process(ContainerBuilder $container)
6172

6273
$inactiveEnhancers = array_diff($enhancerNames, $enabledEnhancers);
6374
foreach ($inactiveEnhancers as $inactiveEnhancer) {
64-
$container->removeDefinition((string) $enhancers[$inactiveEnhancer]);
6575
unset($enhancers[$inactiveEnhancer]);
6676
}
6777

78+
// sort enhancers, higher = more priority
79+
usort($enhancers, function ($a, $b) {
80+
return -strcmp($a[0], $b[0]);
81+
});
82+
83+
$enhancers = array_map(function ($enhancer) {
84+
return $enhancer[1];
85+
}, $enhancers);
86+
6887
$registryDef = $container->getDefinition('cmf_resource.description.factory');
6988
$registryDef->replaceArgument(0, array_values($enhancers));
7089
}

Resources/config/description.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
<tag name="cmf_resource.description.enhancer" alias="sonata_admin" />
1616
</service>
1717

18+
<service id="cmf_resource.description.enhancer.doctrine.phpcr_odm" class="Symfony\Cmf\Component\Resource\Description\Enhancer\Doctrine\PhpcrOdm\PhpcrOdmEnhancer" public="false">
19+
<argument type="service" id="cmf_resource.description.enhancer.doctrine.phpcr_odm.metadata_factory" />
20+
<tag name="cmf_resource.description.enhancer" alias="doctrine_phpcr_odm" />
21+
</service>
22+
23+
<service id="cmf_resource.description.enhancer.doctrine.phpcr_odm.metadata_factory" class="Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory" public="false">
24+
<factory service="doctrine_phpcr.odm.document_manager" method="getMetadataFactory" />
25+
</service>
26+
1827
<service id="cmf_resource.description.enhancer.sylius_resource" class="Symfony\Cmf\Component\Resource\Description\Enhancer\Sylius\ResourceEnhancer" public="false">
1928
<argument type="service" id="sylius.resource_registry" />
2029
<argument type="service" id="request_stack" />

Tests/Unit/DependencyInjection/Compiler/DescriptionEnhancerPassTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,55 @@ public function testUnknownEnhancer()
118118
$this->pass->process($this->container->reveal());
119119
}
120120

121+
/**
122+
* It should throw an exception if an invalid tag attributes is used.
123+
*
124+
* @expectedException Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
125+
* @expectedExceptionMessage Unknown tag attributes "foobar" for service "service_1", valid attributes: "name", "alias", "priority
126+
*/
127+
public function testInvalidAttribute()
128+
{
129+
$this->container->has('cmf_resource.description.factory')->willReturn(true);
130+
$this->container->getParameter('cmf_resource.description.enabled_enhancers')->willReturn([
131+
'service_1',
132+
]);
133+
$this->container->findTaggedServiceIds('cmf_resource.description.enhancer')->willReturn([
134+
'service_1' => [['alias' => 'one', 'foobar' => 'barfoo']],
135+
]);
136+
137+
$this->pass->process($this->container->reveal());
138+
}
139+
140+
/**
141+
* It should sort description enhancers.
142+
*/
143+
public function testSortDescriptionEnhancers()
144+
{
145+
$this->container->has('cmf_resource.description.factory')->willReturn(true);
146+
$this->container->getParameter('cmf_resource.description.enabled_enhancers')->willReturn([
147+
'enhancer_1',
148+
'enhancer_2',
149+
'enhancer_3',
150+
'enhancer_4',
151+
]);
152+
$this->container->findTaggedServiceIds('cmf_resource.description.enhancer')->willReturn([
153+
'service_1' => [['alias' => 'enhancer_1']],
154+
'service_2' => [['alias' => 'enhancer_2', 'priority' => 255]],
155+
'service_3' => [['alias' => 'enhancer_3', 'priority' => -250]],
156+
'service_4' => [['alias' => 'enhancer_4', 'priority' => -255]],
157+
]);
158+
159+
$this->container->getDefinition('cmf_resource.description.factory')->willReturn($this->factoryDefinition->reveal());
160+
$this->factoryDefinition->replaceArgument(0, [
161+
new Reference('service_2'),
162+
new Reference('service_1'),
163+
new Reference('service_4'),
164+
new Reference('service_3'),
165+
])->shouldBeCalled();
166+
167+
$this->pass->process($this->container->reveal());
168+
}
169+
121170
/**
122171
* It should remove inactive enhancers.
123172
*/
@@ -133,7 +182,6 @@ public function testRemoveInactive()
133182
]);
134183
$this->container->getDefinition('cmf_resource.description.factory')->willReturn($this->factoryDefinition->reveal());
135184

136-
$this->container->removeDefinition('service_2')->shouldBeCalled();
137185
$this->factoryDefinition->replaceArgument(0, [new Reference('service_1')])->shouldBeCalled();
138186

139187
$this->pass->process($this->container->reveal());

Tests/Unit/Registry/RepositoryRegistryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public function testNames()
102102
]
103103
);
104104

105-
$this->assertEquals([ 'test', 'tset' ], $registry->names());
105+
$this->assertEquals(['test', 'tset'], $registry->names());
106106
}
107107

108108
/**
109-
* It should return all registered repositories
109+
* It should return all registered repositories.
110110
*/
111111
public function testAll()
112112
{

0 commit comments

Comments
 (0)