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

Commit 0cb2c56

Browse files
committed
ODM serializer works
- Started working on PHPCR serializer
1 parent 44002c0 commit 0cb2c56

File tree

15 files changed

+235
-57
lines changed

15 files changed

+235
-57
lines changed

Controller/ResourceController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Hateoas\UrlGenerator\UrlGeneratorInterface;
1111
use JMS\Serializer\SerializerBuilder;
1212
use JMS\Serializer\SerializerInterface;
13+
use JMS\Serializer\SerializationContext;
1314

1415
class ResourceController
1516
{
@@ -44,7 +45,11 @@ public function resourceAction($repositoryName, $path)
4445
$repository = $this->registry->get($repositoryName);
4546
$resource = $repository->get('/' . $path);
4647

47-
$json = $this->serializer->serialize($resource, 'json');
48+
$json = $this->serializer->serialize(
49+
$resource,
50+
'json',
51+
SerializationContext::create()->enableMaxDepthChecks()
52+
);
4853

4954
return new Response($json);
5055
}

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function prepend(ContainerBuilder $container)
3535
'path' => __DIR__ . '/../Resources/config/serializer',
3636
'namespace_prefix' => 'Puli\Repository\Resource',
3737
),
38+
array(
39+
'path' => __DIR__ . '/../Resources/config/serializer',
40+
'namespace_prefix' => 'PHPCR',
41+
),
3842
),
3943
),
4044
));

Helper/PathHelper.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Helper;
13+
14+
class PathHelper
15+
{
16+
/**
17+
* Remove the leading "/" from the given path
18+
* This is required when generating the URL to the resource
19+
* via. expression language.
20+
*
21+
* @param string $path;
22+
* @return string
23+
*/
24+
public function relativize($path)
25+
26+
{
27+
if (substr($path, 0, 1) == '/') {
28+
return substr($path, 1);
29+
}
30+
31+
return $path;
32+
}
33+
}

Resources/config/resource-rest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<parameters>
88
<parameter key="cmf_resource_rest.controller.resource.class">Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController</parameter>
9+
<parameter key="cmf_resource_rest.path_helper.class">Symfony\Cmf\Bundle\ResourceRestBundle\Helper\PathHelper</parameter>
910
</parameters>
1011

1112
<services>
@@ -16,7 +17,7 @@
1617
<argument type="service" id="hateoas.generator.symfony" />
1718
</service>
1819

19-
20+
<service id="cmf_resource_rest.path_helper" class="%cmf_resource_rest.path_helper.class%" />
2021

2122
</services>
2223
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<serializer>
3+
<class name="PHPCR\NodeInterface" xmlns:h="https://github.com/willdurand/Hateoas" exclude-policy="all" exclude="true">
4+
</class>
5+
</serializer>
6+

Resources/config/serializer/PhpcrOdmResource.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
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="testrepo" />
7-
<h:parameter name="path" value="expr(object.getPath())" />
6+
<h:parameter name="repositoryName" value="expr(service('cmf_resource.registry').getName(object.getRepository()))" />
7+
<h:parameter name="path" value="expr(service('cmf_resource_rest.path_helper').relativize(object.getPath()))" />
88
</h:href>
99
</h:relation>
10-
<virtual-property serialized-name="children" max-depth="1" method="listChildren" />
10+
<virtual-property serialized-name="children" max-depth="3" method="listChildren"/>
1111
</class>
1212
</serializer>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<serializer>
3+
<class name="Symfony\Cmf\Component\Resource\Repository\Resource\PhpcrResource" xmlns:h="https://github.com/willdurand/Hateoas">
4+
<h:relation rel="self">
5+
<h:href route="_cmf_resource">
6+
<h:parameter name="repositoryName" value="expr(service('cmf_resource.registry').getName(object.getRepository()))" />
7+
<h:parameter name="path" value="expr(service('cmf_resource_rest.path_helper').relativize(object.getPath()))" />
8+
</h:href>
9+
</h:relation>
10+
</class>
11+
</serializer>

Tests/Features/Context/ResourceContext.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,42 @@
22

33
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Features\Context;
44

5-
require_once(__DIR__ . '/../../Resources/app/TestKernel.php');
6-
75
use Behat\WebApiExtension\Context\WebApiContext;
86
use Behat\Symfony2Extension\Context\KernelDictionary;
97
use Behat\Gherkin\Node\PyStringNode;
108
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
119
use Behat\Gherkin\Node\TableNode;
1210
use PHPCR\Util\NodeHelper;
1311
use PHPCR\Util\PathHelper;
12+
use Behat\Symfony2Extension\Context\KernelAwareContext;
13+
use Behat\Behat\Context\Context;
14+
use Symfony\Component\HttpKernel\KernelInterface;
1415

15-
class ResourceContext extends WebApiContext
16+
class ResourceContext implements Context, KernelAwareContext
1617
{
17-
use KernelDictionary;
18-
1918
private $session;
2019
private $manager;
20+
private $kernel;
2121

22+
/**
23+
* Return the path of the configuration file used by the AppKernel
24+
*
25+
* @static
26+
* @return string
27+
*/
2228
public static function getConfigurationFile()
2329
{
2430
return __DIR__ . '/../../Resources/app/cache/resource.yml';
2531
}
2632

33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function setKernel(KernelInterface $kernel)
37+
{
38+
$this->kernel = $kernel;
39+
}
40+
2741
/**
2842
* @BeforeScenario
2943
*/
@@ -33,7 +47,7 @@ public function beforeScenario(BeforeScenarioScope $scope)
3347
unlink(self::getConfigurationFile());
3448
}
3549

36-
$this->manager = $this->getContainer()->get('doctrine_phpcr.odm.document_manager');
50+
$this->manager = $this->kernel->getContainer()->get('doctrine_phpcr.odm.document_manager');
3751
$this->session = $this->manager->getPhpcrSession();
3852

3953
if ($this->session->getRootNode()->hasNode('tests')) {

Tests/Features/resource_api.feature

Lines changed: 134 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,145 @@ Feature: Request Resources from the REST API
55

66
Background:
77
Given the test application has the following configuration:
8-
"""
9-
cmf_resource:
10-
repository:
11-
doctrine_phpcr_odm:
12-
testrepo:
13-
basepath: /tests/cmf/articles
14-
"""
8+
"""
9+
cmf_resource:
10+
repository:
11+
doctrine_phpcr_odm:
12+
phpcrodm_repo:
13+
basepath: /tests/cmf/articles
14+
doctrine_phpcr:
15+
phpcr_repo:
16+
basepath: /tests/cmf/articles
17+
"""
1518

1619

17-
Scenario: Retrieve a resource
20+
Scenario: Retrieve a PHPCR-ODM resource with children
1821
Given there exists a "Article" document at "/cmf/articles/foo":
1922
| title | Article 1 |
2023
| body | This is my article |
21-
Then I send a GET request to "/api/testrepo/foo"
22-
Then print response
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 |
30+
Then I send a GET request to "/api/phpcrodm_repo/foo"
2331
And the response code should be 200
2432
And the response should contain json:
25-
"""
26-
{
27-
"name": "foo"
28-
"path": "/foo"
29-
"type": "PhpcrOdmResource
30-
}
31-
"""
33+
"""
34+
{
35+
"_links": {
36+
"self": {
37+
"href": "/api/phpcrodm_repo/foo"
38+
}
39+
},
40+
"children": {
41+
"bar": {
42+
"_links": {
43+
"self": {
44+
"href": "/api/phpcrodm_repo/foo/bar"
45+
}
46+
},
47+
"children": [],
48+
"document": {
49+
"_links": {
50+
"self": {
51+
"href": "/path/to/this"
52+
}
53+
},
54+
"body": "This is my second article",
55+
"id": "/tests/cmf/articles/foo/bar",
56+
"title": "Article 2"
57+
},
58+
"path": "/foo/bar",
59+
"repo_path": "/foo/bar"
60+
},
61+
"boo": {
62+
"_links": {
63+
"self": {
64+
"href": "/api/phpcrodm_repo/foo/boo"
65+
}
66+
},
67+
"children": [],
68+
"document": {
69+
"_links": {
70+
"self": {
71+
"href": "/path/to/this"
72+
}
73+
},
74+
"body": "This is my third article",
75+
"id": "/tests/cmf/articles/foo/boo",
76+
"title": "Article 2"
77+
},
78+
"path": "/foo/boo",
79+
"repo_path": "/foo/boo"
80+
}
81+
},
82+
"document": {
83+
"_links": {
84+
"self": {
85+
"href": "/path/to/this"
86+
}
87+
},
88+
"body": "This is my article",
89+
"id": "/tests/cmf/articles/foo",
90+
"title": "Article 1"
91+
},
92+
"path": "/foo",
93+
"repo_path": "/foo"
94+
}
95+
"""
3296

97+
Scenario: Retrieve PHPCR resource with children
98+
Given there exists a "Article" document at "/cmf/articles/foo":
99+
| title | Article 1 |
100+
| body | This is my article |
101+
And there exists a "Article" document at "/cmf/articles/foo/bar":
102+
| title | Article 2 |
103+
| body | This is my second article |
104+
And there exists a "Article" document at "/cmf/articles/foo/boo":
105+
| title | Article 2 |
106+
| body | This is my third article |
107+
Then I send a GET request to "/api/phpcr_repo/foo"
108+
And the response code should be 200
109+
And the response should contain json:
110+
"""
111+
{
112+
"_links": {
113+
"self": {
114+
"href": "/api/phpcr_repo/foo"
115+
}
116+
},
117+
"children": {
118+
"bar": {
119+
"_links": {
120+
"self": {
121+
"href": "/api/phpcr_repo/foo/bar"
122+
}
123+
},
124+
"children": [],
125+
"path": "/foo/bar",
126+
"repo_path": "/foo/bar"
127+
},
128+
"boo": {
129+
"_links": {
130+
"self": {
131+
"href": "/api/phpcr_repo/foo/boo"
132+
}
133+
},
134+
"children": [],
135+
"path": "/foo/boo",
136+
"repo_path": "/foo/boo"
137+
}
138+
},
139+
"node": {
140+
"jcr:primaryType": "nt:unstructured",
141+
"jcr:mixinTypes": ['phpcr:managed'],
142+
"phpcr:class": "Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\Documen...",
143+
"title": "Article",
144+
"body": "This",
145+
},
146+
"path": "/foo",
147+
"repo_path": "/foo"
148+
}
149+
"""

Tests/Resources/TestBundle/Document/Article.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\Document;
44

55
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
6+
use JMS\Serializer\Annotation as Serializer;
7+
use Hateoas\Configuration\Annotation as Hateoas;
68

79
/**
810
* @PHPCR\Document()
11+
* @Hateoas\Relation("self", href="/path/to/this")
912
*/
1013
class Article
1114
{

0 commit comments

Comments
 (0)