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

Commit 6e19d02

Browse files
committed
Support type
1 parent 9f02933 commit 6e19d02

File tree

10 files changed

+57
-131
lines changed

10 files changed

+57
-131
lines changed

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function load(array $configs, ContainerBuilder $container)
6161
private function configurePayloadAliasRegistry(ContainerBuilder $container, $aliasMap)
6262
{
6363
$registry = $container->getDefinition('cmf_resource_rest.payload_alias_registry');
64-
$registry->replaceArgument(0, $aliasMap);
64+
$registry->replaceArgument(1, $aliasMap);
6565
}
6666

6767
public function getNamespace()

Resources/config/resource-rest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<service id="cmf_resource_rest.path_helper" class="%cmf_resource_rest.path_helper.class%" />
2222

2323
<service id="cmf_resource_rest.payload_alias_registry" class="%cmf_resource_rest.payload_alias_registry.class%">
24+
<argument type="service" id="cmf_resource.registry" />
2425
<argument type="collection" />
2526
</service>
2627

Resources/config/serializer.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<service id="cmf_resource_rest.serializer.subscriber.resource" class="%cmf_resource_rest.serializer.subscriber.resource.class%">
3030
<tag name="jms_serializer.event_subscriber" />
3131
<argument type="service" id="cmf_resource.registry" />
32+
<argument type="service" id="cmf_resource_rest.payload_alias_registry" />
3233
</service>
3334

3435
<service id="cmf_resource_rest.serializer.subscriber.resource_collection" class="%cmf_resource_rest.serializer.subscriber.resource_collection.class%">

Resources/config/serializer/PhpcrOdmResource.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<class name="Symfony\Cmf\Component\Resource\Repository\Resource\PhpcrOdmResource" xmlns:h="https://github.com/willdurand/Hateoas">
44
<h:relation rel="self">
55
<h:href route="_cmf_resource">
6-
<h:parameter name="repositoryName" value="expr(service('cmf_resource.registry').getName(object.getRepository()))" />
6+
<h:parameter name="repositoryName" value="expr(service('cmf_resource.registry').getRepositoryAlias(object.getRepository()))" />
77
<h:parameter name="path" value="expr(service('cmf_resource_rest.path_helper').relativize(object.getPath()))" />
88
</h:href>
99
</h:relation>

Resources/config/serializer/PhpcrResource.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<class name="Symfony\Cmf\Component\Resource\Repository\Resource\PhpcrResource" xmlns:h="https://github.com/willdurand/Hateoas">
44
<h:relation rel="self">
55
<h:href route="_cmf_resource">
6-
<h:parameter name="repositoryName" value="expr(service('cmf_resource.registry').getName(object.getRepository()))" />
6+
<h:parameter name="repositoryName" value="expr(service('cmf_resource.registry').getRepositoryAlias(object.getRepository()))" />
77
<h:parameter name="path" value="expr(service('cmf_resource_rest.path_helper').relativize(object.getPath()))" />
88
</h:href>
99
</h:relation>

Serializer/EventSubscriber/ResourceSubscriber.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use JMS\Serializer\EventDispatcher\ObjectEvent;
2020
use Puli\Repository\Api\Resource\Resource;
2121
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
22+
use Symfony\Cmf\Bundle\ResourceRestBundle\ResourceRest\PayloadAliasRegistry;
2223

2324
/**
2425
* Force instaces of ResourceCollection to type "ResourceCollection"
@@ -28,10 +29,15 @@
2829
class ResourceSubscriber implements EventSubscriberInterface
2930
{
3031
private $registry;
32+
private $payloadAliasRegistry;
3133

32-
public function __construct(RepositoryRegistryInterface $registry)
34+
public function __construct(
35+
RepositoryRegistryInterface $registry,
36+
PayloadAliasRegistry $payloadAliasRegistry
37+
)
3338
{
3439
$this->registry = $registry;
40+
$this->payloadAliasRegistry = $payloadAliasRegistry;
3541
}
3642

3743
public static function getSubscribedEvents()
@@ -53,8 +59,9 @@ public function onPostSerialize(ObjectEvent $event)
5359

5460
if ($object instanceof Resource) {
5561
$visitor = $event->getVisitor();
56-
$visitor->addData('repository', $this->registry->getName($object->getRepository()));
57-
$visitor->addData('type', get_class($object));
62+
$visitor->addData('repository_alias', $this->registry->getRepositoryAlias($object->getRepository()));
63+
$visitor->addData('repository_type', $this->registry->getRepositoryType($object->getRepository()));
64+
$visitor->addData('payload_alias', $this->payloadAliasRegistry->getPayloadAlias($object));
5865
}
5966
}
6067
}

Tests/Features/resource_api_phpcr.feature

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,83 +13,42 @@ Feature: Request Resources from the REST API
1313
basepath: /tests/cmf/articles
1414
1515
16-
16+
cmf_resource_rest:
17+
payload_alias_map:
18+
article:
19+
repository: doctrine_phpcr
20+
type: "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article"
1721
"""
1822

1923

2024
Scenario: Retrieve PHPCR resource with children
2125
Given there exists a "Article" document at "/cmf/articles/foo":
2226
| title | Article 1 |
2327
| body | This is my article |
24-
And there exists a "Article" document at "/cmf/articles/foo/bar":
25-
| title | Article 2 |
26-
| body | This is my second article |
27-
And there exists a "Article" document at "/cmf/articles/foo/boo":
28-
| title | Article 2 |
29-
| body | This is my third article |
3028
Then I send a GET request to "/api/phpcr_repo/foo"
31-
And print response
3229
And the response should contain json:
3330
"""
3431
{
35-
"_links": {
36-
"self": {
37-
"href": "/api/phpcr_repo/foo"
38-
}
39-
},
40-
"children": {
41-
"bar": {
42-
"_links": {
43-
"self": {
44-
"href": "/api/phpcr_repo/foo/bar"
45-
}
46-
},
47-
"children": [],
48-
"node": {
49-
"body": "This is my second article",
50-
"jcr:mixinTypes": [
51-
"phpcr:managed"
52-
],
53-
"jcr:primaryType": "nt:unstructured",
54-
"phpcr:class": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
55-
"phpcr:classparents": [],
56-
"title": "Article 2"
57-
},
58-
"path": "/foo/bar",
59-
"repo_path": "/foo/bar"
60-
},
61-
"boo": {
62-
"_links": {
63-
"self": {
64-
"href": "/api/phpcr_repo/foo/boo"
65-
}
66-
},
67-
"children": [],
68-
"node": {
69-
"body": "This is my third article",
70-
"jcr:mixinTypes": [
71-
"phpcr:managed"
72-
],
73-
"jcr:primaryType": "nt:unstructured",
74-
"phpcr:class": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
75-
"phpcr:classparents": [],
76-
"title": "Article 2"
77-
},
78-
"path": "/foo/boo",
79-
"repo_path": "/foo/boo"
80-
}
81-
},
32+
"path": "\/foo",
33+
"repo_path": "\/foo",
34+
"children": [],
8235
"node": {
83-
"body": "This is my article",
36+
"jcr:primaryType": "nt:unstructured",
8437
"jcr:mixinTypes": [
8538
"phpcr:managed"
8639
],
87-
"jcr:primaryType": "nt:unstructured",
8840
"phpcr:class": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
8941
"phpcr:classparents": [],
90-
"title": "Article 1"
42+
"title": "Article 1",
43+
"body": "This is my article"
9144
},
92-
"path": "/foo",
93-
"repo_path": "/foo"
45+
"repository_alias": "phpcr_repo",
46+
"repository_type": "doctrine_phpcr",
47+
"payload_alias": "nt:unstructured",
48+
"_links": {
49+
"self": {
50+
"href": "\/api\/phpcr_repo\/foo"
51+
}
52+
}
9453
}
9554
"""

Tests/Features/resource_api_phpcr_odm.feature

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,84 +14,41 @@ Feature: Request Resources from the REST API
1414
1515
cmf_resource_rest:
1616
payload_alias_map:
17-
article: { repository: doctrine_phpcr_odm, type: "Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\Document\Article" }
17+
article:
18+
repository: doctrine_phpcr_odm
19+
type: "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article"
1820
"""
1921

2022

2123
Scenario: Retrieve a PHPCR-ODM resource with children
2224
Given there exists a "Article" document at "/cmf/articles/foo":
2325
| title | Article 1 |
2426
| body | This is my article |
25-
And there exists a "Article" document at "/cmf/articles/foo/bar":
26-
| title | Article 2 |
27-
| body | This is my second article |
28-
And there exists a "Article" document at "/cmf/articles/foo/boo":
29-
| title | Article 2 |
30-
| body | This is my third article |
3127
Then I send a GET request to "/api/phpcrodm_repo/foo"
32-
And print response
3328
And the response code should be 200
3429
And the response should contain json:
3530
"""
3631
{
37-
"_links": {
38-
"self": {
39-
"href": "/api/phpcrodm_repo/foo"
40-
}
41-
},
42-
"children": {
43-
"bar": {
44-
"_links": {
45-
"self": {
46-
"href": "/api/phpcrodm_repo/foo/bar"
47-
}
48-
},
49-
"children": [],
50-
"document": {
51-
"_links": {
52-
"self": {
53-
"href": "/path/to/this"
54-
}
55-
},
56-
"body": "This is my second article",
57-
"id": "/tests/cmf/articles/foo/bar",
58-
"title": "Article 2"
59-
},
60-
"path": "/foo/bar",
61-
"repo_path": "/foo/bar"
62-
},
63-
"boo": {
64-
"_links": {
65-
"self": {
66-
"href": "/api/phpcrodm_repo/foo/boo"
67-
}
68-
},
69-
"children": [],
70-
"document": {
71-
"_links": {
72-
"self": {
73-
"href": "/path/to/this"
74-
}
75-
},
76-
"body": "This is my third article",
77-
"id": "/tests/cmf/articles/foo/boo",
78-
"title": "Article 2"
79-
},
80-
"path": "/foo/boo",
81-
"repo_path": "/foo/boo"
82-
}
83-
},
32+
"path": "\/foo",
33+
"repo_path": "\/foo",
34+
"children": [],
8435
"document": {
36+
"id": "\/tests\/cmf\/articles\/foo",
37+
"title": "Article 1",
38+
"body": "This is my article",
8539
"_links": {
8640
"self": {
87-
"href": "/path/to/this"
41+
"href": "\/path\/to\/this"
8842
}
89-
},
90-
"body": "This is my article",
91-
"id": "/tests/cmf/articles/foo",
92-
"title": "Article 1"
43+
}
9344
},
94-
"path": "/foo",
95-
"repo_path": "/foo"
45+
"repository_alias": "phpcrodm_repo",
46+
"repository_type": "doctrine_phpcr_odm",
47+
"payload_alias": "article",
48+
"_links": {
49+
"self": {
50+
"href": "\/api\/phpcrodm_repo\/foo"
51+
}
52+
}
9653
}
9754
"""

Tests/Resources/app/AppKernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function registerContainerConfiguration(LoaderInterface $loader)
3434
{
3535
$loader->load(__DIR__.'/config/config.php');
3636

37-
if ($this->getEnvironment() !== 'behat') {
37+
if ($this->getEnvironment() !== 'behat' && file_exists(ResourceContext::getConfigurationFile())) {
38+
3839
$loader->import(ResourceContext::getConfigurationFile());
3940
}
4041
}

Tests/Unit/ResourceRest/PayloadAliasRegistryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public function testRegistry($aliases, $resource, $expectedAlias)
5151
{
5252
$registry = $this->createRegistry($aliases);
5353

54-
$this->repositoryRegistry->getName(
54+
$this->repositoryRegistry->getRepositoryAlias(
5555
$this->repository->reveal()
5656
)->willReturn($resource['repository']);
5757

5858
$this->resource->getRepository()->willReturn($this->repository->reveal());
5959
$this->resource->getPayloadType()->willReturn($resource['type']);
6060

61-
$alias = $registry->getAlias($this->resource->reveal());
61+
$alias = $registry->getPayloadAlias($this->resource->reveal());
6262
$this->assertEquals($expectedAlias, $alias);
6363
}
6464

0 commit comments

Comments
 (0)