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

Commit bdc0511

Browse files
committed
Resource aliases
1 parent 02a2c89 commit bdc0511

File tree

14 files changed

+336
-2
lines changed

14 files changed

+336
-2
lines changed

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1717
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1818
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19+
use Symfony\Component\Config\Definition\Processor;
1920

2021
class CmfResourceRestExtension extends Extension implements PrependExtensionInterface
2122
{
@@ -46,8 +47,25 @@ public function prepend(ContainerBuilder $container)
4647
*/
4748
public function load(array $configs, ContainerBuilder $container)
4849
{
50+
$processor = new Processor();
4951
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
52+
$configuration = new Configuration();
53+
$config = $processor->processConfiguration($configuration, $configs);
54+
5055
$loader->load('serializer.xml');
5156
$loader->load('resource-rest.xml');
57+
58+
$this->configurePayloadAliasRegistry($container, $config['payload_alias_map']);
59+
}
60+
61+
private function configurePayloadAliasRegistry(ContainerBuilder $container, $aliasMap)
62+
{
63+
$registry = $container->getDefinition('cmf_resource_rest.payload_alias_registry');
64+
$registry->replaceArgument(0, $aliasMap);
65+
}
66+
67+
public function getNamespace()
68+
{
69+
return 'http://cmf.symfony.com/schema/dic/' . $this->getAlias();
5270
}
5371
}

DependencyInjection/Configuration.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection;
13+
14+
use Symfony\Component\Config\Definition\ConfigurationInterface;
15+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16+
17+
class Configuration implements ConfigurationInterface
18+
{
19+
/**
20+
* Returns the config tree builder.
21+
*
22+
* @return TreeBuilder
23+
*/
24+
public function getConfigTreeBuilder()
25+
{
26+
$treeBuilder = new TreeBuilder();
27+
$treeBuilder->root('cmf_resource_rest')
28+
->children()
29+
->arrayNode('payload_alias_map')
30+
->useAttributeAsKey('alias')
31+
->prototype('array')
32+
->children()
33+
->scalarNode('repository')->end()
34+
->scalarNode('type')->end()
35+
->end()
36+
->end()
37+
->end()
38+
->end();
39+
40+
return $treeBuilder;
41+
}
42+
}
43+

Resources/config/resource-rest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<parameters>
88
<parameter key="cmf_resource_rest.controller.resource.class">Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController</parameter>
99
<parameter key="cmf_resource_rest.path_helper.class">Symfony\Cmf\Bundle\ResourceRestBundle\Helper\PathHelper</parameter>
10+
<parameter key="cmf_resource_rest.payload_alias_registry.class">Symfony\Cmf\Bundle\ResourceRestBundle\ResourceRest\PayloadAliasRegistry</parameter>
1011
</parameters>
1112

1213
<services>
@@ -19,5 +20,9 @@
1920

2021
<service id="cmf_resource_rest.path_helper" class="%cmf_resource_rest.path_helper.class%" />
2122

23+
<service id="cmf_resource_rest.payload_alias_registry" class="%cmf_resource_rest.payload_alias_registry.class%">
24+
<argument type="collection" />
25+
</service>
26+
2227
</services>
2328
</container>

Resources/config/serializer.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<parameter key="cmf_resource_rest.serializer.handler.phpcr_node.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Handler\PhpcrNodeHandler</parameter>
1010
<parameter key="cmf_resource_rest.serializer.subscriber.phpcr_node.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\PhpcrNodeSubscriber</parameter>
1111
<parameter key="cmf_resource_rest.serializer.subscriber.resource_collection.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\ResourceCollectionSubscriber</parameter>
12+
<parameter key="cmf_resource_rest.serializer.subscriber.resource.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\ResourceSubscriber</parameter>
1213
</parameters>
1314

1415
<services>
@@ -25,6 +26,11 @@
2526
<tag name="jms_serializer.event_subscriber" />
2627
</service>
2728

29+
<service id="cmf_resource_rest.serializer.subscriber.resource" class="%cmf_resource_rest.serializer.subscriber.resource.class%">
30+
<tag name="jms_serializer.event_subscriber" />
31+
<argument type="service" id="cmf_resource.registry" />
32+
</service>
33+
2834
<service id="cmf_resource_rest.serializer.subscriber.resource_collection" class="%cmf_resource_rest.serializer.subscriber.resource_collection.class%">
2935
<tag name="jms_serializer.event_subscriber" />
3036
</service>

Resources/config/serializer/GenericResource.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<serializer>
33
<class name="Puli\Repository\Resource\GenericResource" xmlns:h="https://github.com/willdurand/Hateoas">
44
<property name="repo" exclude="true" />
5+
<property name="repo" exclude="true" />
56
</class>
67
</serializer>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber;
13+
14+
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
15+
use PHPCR\NodeInterface;
16+
use JMS\Serializer\EventDispatcher\Events;
17+
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
18+
use Puli\Repository\Api\ResourceCollection;
19+
use JMS\Serializer\EventDispatcher\ObjectEvent;
20+
use Puli\Repository\Api\Resource\Resource;
21+
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
22+
23+
/**
24+
* Force instaces of ResourceCollection to type "ResourceCollection"
25+
*
26+
* @author Daniel Leech <[email protected]>
27+
*/
28+
class ResourceSubscriber implements EventSubscriberInterface
29+
{
30+
private $registry;
31+
32+
public function __construct(RepositoryRegistryInterface $registry)
33+
{
34+
$this->registry = $registry;
35+
}
36+
37+
public static function getSubscribedEvents()
38+
{
39+
return array(
40+
array(
41+
'event' => Events::POST_SERIALIZE,
42+
'method' => 'onPostSerialize',
43+
),
44+
);
45+
}
46+
47+
/**
48+
* @param PreSerializeEvent $event
49+
*/
50+
public function onPostSerialize(ObjectEvent $event)
51+
{
52+
$object = $event->getObject();
53+
54+
if ($object instanceof Resource) {
55+
$visitor = $event->getVisitor();
56+
$visitor->addData('repository', $this->registry->getName($object->getRepository()));
57+
$visitor->addData('type', get_class($object));
58+
}
59+
}
60+
}

Tests/Features/resource_api_phpcr.feature

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Feature: Request Resources from the REST API
1010
repository:
1111
doctrine_phpcr:
1212
phpcr_repo:
13-
basepath: /tests/cmf/articles
13+
basepath: /tests/cmf/articles
14+
15+
16+
1417
"""
1518

1619

Tests/Features/resource_api_phpcr_odm.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Feature: Request Resources from the REST API
1111
doctrine_phpcr_odm:
1212
phpcrodm_repo:
1313
basepath: /tests/cmf/articles
14+
15+
cmf_resource_rest:
16+
payload_alias:
17+
article: { repository: doctrine_phpcr_odm, type: "Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\Document\Article" }
1418
"""
1519

1620

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
namespace Symfony\Cmf\Bundle\ResourceBundle\Tests\Unit\DependencyInjection;
13+
14+
15+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
16+
use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\CmfResourceRestExtension;
17+
18+
class CmfResourceRestExtensionTest extends AbstractExtensionTestCase
19+
{
20+
protected function getContainerExtensions()
21+
{
22+
return array(new CmfResourceRestExtension());
23+
}
24+
25+
public function provideExtension()
26+
{
27+
return array(
28+
array(
29+
array(
30+
'payload_alias_map' => array(
31+
'article' => array(
32+
'repository' => 'doctrine_phpcr_odm',
33+
'type' => 'Article',
34+
),
35+
),
36+
),
37+
array(),
38+
),
39+
);
40+
}
41+
42+
/**
43+
* @dataProvider provideExtension
44+
*/
45+
public function testExtension($config, $expectedServiceIds)
46+
{
47+
$this->load($config);
48+
49+
foreach ($expectedServiceIds as $expectedServiceId) {
50+
$this->assertContainerBuilderHasService($expectedServiceId);
51+
}
52+
}
53+
}
54+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Unit\DependencyInjection;
13+
14+
use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\CmfResourceRestExtension;
15+
use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Configuration;
16+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase;
17+
18+
class ConfigurationTest extends AbstractExtensionConfigurationTestCase
19+
{
20+
protected function getContainerExtension()
21+
{
22+
return new CmfResourceRestExtension();
23+
}
24+
25+
protected function getConfiguration()
26+
{
27+
return new Configuration();
28+
}
29+
30+
public function provideConfig()
31+
{
32+
return array(
33+
array(__DIR__ . '/fixtures/config.xml'),
34+
array(__DIR__ . '/fixtures/config.yml'),
35+
);
36+
}
37+
38+
/**
39+
* @dataProvider provideConfig
40+
*/
41+
public function testConfig($source)
42+
{
43+
$this->assertProcessedConfigurationEquals(array(
44+
'payload_alias_map' => array(
45+
'article' => array(
46+
'repository' => 'doctrine_phpcr_odm',
47+
'type' => 'Namespace\Article'
48+
),
49+
),
50+
), array($source));
51+
}
52+
}

0 commit comments

Comments
 (0)