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

Commit 9ed2460

Browse files
authored
Merge pull request #41 from symfony-cmf/issue-10-15
Make max depth configurable (config + query param) and throw exception if JmsSerializerBundle is not registered
2 parents 4446e1e + 2c945bb commit 9ed2460

File tree

14 files changed

+130
-17
lines changed

14 files changed

+130
-17
lines changed

src/Controller/ResourceController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Controller;
1313

14+
use Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Jms\Handler\ResourceHandler;
1415
use Symfony\Cmf\Component\Resource\Puli\Api\ResourceRepository;
1516
use Symfony\Cmf\Component\Resource\Puli\Api\ResourceNotFoundException;
1617
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
@@ -38,6 +39,11 @@ class ResourceController
3839
*/
3940
private $serializer;
4041

42+
/**
43+
* @var ResourceHandler
44+
*/
45+
private $resourceHandler;
46+
4147
/**
4248
* @var AuthorizationCheckerInterface|null
4349
*/
@@ -47,11 +53,12 @@ class ResourceController
4753
* @param SerializerInterface $serializer
4854
* @param RepositoryRegistryInterface $registry
4955
*/
50-
public function __construct(SerializerInterface $serializer, RepositoryRegistryInterface $registry, AuthorizationCheckerInterface $authorizationChecker = null)
56+
public function __construct(SerializerInterface $serializer, RepositoryRegistryInterface $registry, ResourceHandler $resourceHandler, AuthorizationCheckerInterface $authorizationChecker = null)
5157
{
5258
$this->serializer = $serializer;
5359
$this->registry = $registry;
5460
$this->authorizationChecker = $authorizationChecker;
61+
$this->resourceHandler = $resourceHandler;
5562
}
5663

5764
/**
@@ -60,8 +67,12 @@ public function __construct(SerializerInterface $serializer, RepositoryRegistryI
6067
* @param string $repositoryName
6168
* @param string $path
6269
*/
63-
public function getResourceAction($repositoryName, $path)
70+
public function getResourceAction(Request $request, $repositoryName, $path)
6471
{
72+
if ($request->query->has('depth')) {
73+
$this->resourceHandler->setMaxDepth($request->query->getInt('depth'));
74+
}
75+
6576
$path = '/'.ltrim($path, '/');
6677

6778
try {

src/DependencyInjection/CmfResourceRestExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public function load(array $configs, ContainerBuilder $container)
3535
$configuration = new Configuration();
3636
$config = $processor->processConfiguration($configuration, $configs);
3737

38+
$bundles = $container->getParameter('kernel.bundles');
39+
if (!array_key_exists('JMSSerializerBundle', $bundles)) {
40+
throw new \LogicException('The JMSSerializerBundle must be registered in order to use the CmfResourceRestBundle.');
41+
}
42+
43+
$container->setParameter('cmf_resource_rest.max_depth', $config['max_depth']);
44+
3845
$loader->load('serializer.xml');
3946
$loader->load('resource-rest.xml');
4047

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function getConfigTreeBuilder()
2828
->fixXmlConfig('payload_alias', 'payload_alias_map')
2929
->fixXmlConfig('enhance', 'enhancer_map')
3030
->children()
31+
->integerNode('max_depth')->defaultValue(2)->end()
3132
->arrayNode('payload_alias_map')
3233
->useAttributeAsKey('name')
3334
->prototype('array')

src/Resources/config/resource-rest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<service id="cmf_resource_rest.controller.resource" class="Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController">
1010
<argument type="service" id="serializer" />
1111
<argument type="service" id="cmf_resource.registry" />
12+
<argument type="service" id="cmf_resource_rest.serializer.jms.handler.resource" />
1213
<argument type="service" id="security.authorization_checker" on-invalid="ignore" />
1314
</service>
1415

src/Resources/config/serializer.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<argument type="service" id="cmf_resource.registry" />
1010
<argument type="service" id="cmf_resource_rest.registry.payload_alias" />
1111
<argument type="service" id="cmf_resource_rest.registry.enhancer" />
12+
<argument>%cmf_resource_rest.max_depth%</argument>
13+
1214
<tag name="jms_serializer.subscribing_handler"/>
1315
</service>
1416

src/Serializer/Jms/Handler/ResourceHandler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ class ResourceHandler implements SubscribingHandlerInterface
3333
private $registry;
3434
private $payloadAliasRegistry;
3535
private $enhancerRegistry;
36+
private $maxDepth;
3637

3738
public function __construct(
3839
RepositoryRegistryInterface $registry,
3940
PayloadAliasRegistry $payloadAliasRegistry,
40-
EnhancerRegistry $enhancerRegistry
41+
EnhancerRegistry $enhancerRegistry,
42+
$maxDepth = 2
4143
) {
4244
$this->registry = $registry;
4345
$this->payloadAliasRegistry = $payloadAliasRegistry;
4446
$this->enhancerRegistry = $enhancerRegistry;
47+
$this->maxDepth = $maxDepth;
4548
}
4649

4750
public static function getSubscribingMethods()
@@ -72,6 +75,11 @@ public function serializeResource(
7275
$context->accept($data);
7376
}
7477

78+
public function setMaxDepth($maxDepth)
79+
{
80+
$this->maxDepth = $maxDepth;
81+
}
82+
7583
private function doSerializeResource(PuliResource $resource, $depth = 0)
7684
{
7785
$data = array();
@@ -96,7 +104,7 @@ private function doSerializeResource(PuliResource $resource, $depth = 0)
96104
foreach ($resource->listChildren() as $name => $childResource) {
97105
$children[$name] = array();
98106

99-
if ($depth < 2) {
107+
if ($depth < $this->maxDepth) {
100108
$children[$name] = $this->doSerializeResource($childResource, $depth + 1);
101109
}
102110
}

tests/Features/nesting.feature

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Feature: Nesting resources
2+
In order to retrieve a tree of data
3+
As a webservice user
4+
I need to be able to get nested resources
5+
6+
Background:
7+
Given the test application has the following configuration:
8+
"""
9+
cmf_resource:
10+
repositories:
11+
default:
12+
type: doctrine/phpcr-odm
13+
basepath: /tests/cmf/articles
14+
"""
15+
And there exists an "Article" document at "/cmf/articles/foo":
16+
| title | Article 1 |
17+
| body | This is my article |
18+
And there exists an "Article" document at "/cmf/articles/foo/sub":
19+
| title | Sub-article 1 |
20+
| body | This is my article |
21+
22+
Scenario: Retrieving nested resources
23+
When I send a GET request to "/api/default/foo"
24+
Then the response should contain json:
25+
"""
26+
{
27+
"repository_alias": "default",
28+
"repository_type": "doctrine/phpcr-odm",
29+
"payload_alias": null,
30+
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
31+
"path": "\/foo",
32+
"node_name": "foo",
33+
"label": "foo",
34+
"repository_path": "\/foo",
35+
"children": {
36+
"sub": {
37+
"repository_alias": "default",
38+
"repository_type": "doctrine/phpcr-odm",
39+
"payload_alias": null,
40+
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
41+
"path": "\/foo\/sub",
42+
"node_name": "sub",
43+
"label": "sub",
44+
"repository_path": "\/foo\/sub",
45+
"children": []
46+
}
47+
}
48+
}
49+
"""
50+
51+
Scenario: Specifying a depth
52+
When I send a GET request to "/api/default/foo?depth=0"
53+
Then the response should contain json:
54+
"""
55+
{
56+
"repository_alias": "default",
57+
"repository_type": "doctrine/phpcr-odm",
58+
"payload_alias": null,
59+
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
60+
"path": "\/foo",
61+
"node_name": "foo",
62+
"label": "foo",
63+
"repository_path": "\/foo",
64+
"children": {
65+
"sub": []
66+
}
67+
}
68+
"""

tests/Features/resource_api_filesystem.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Feature: Filesystem resource repository
99
# cmf_resource:
1010
# repositories:
1111
# default:
12-
# type: filesystem
12+
# type: puli/filesystem
1313
# base_dir: "%kernel.root_dir%/Resources/views/snippets"
1414
# """
1515
# And there is a file named "%kernel.root_dir%/Resources/views/snippets/snippet1.html" with:
@@ -24,7 +24,7 @@ Feature: Filesystem resource repository
2424
# """
2525
# {
2626
# "repository_alias": "default",
27-
# "repository_type": "filesystem",
27+
# "repository_type": "puli/filesystem",
2828
# "payload_alias": null,
2929
# "payload_type": null,
3030
# "path": "\/snippet1.html",

tests/Features/resource_api_phpcr.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Feature: PHPCR resource repository
99
cmf_resource:
1010
repositories:
1111
phpcr_repo:
12-
type: doctrine_phpcr
12+
type: phpcr/phpcr
1313
basepath: /tests/cmf/articles
1414
1515
cmf_resource_rest:
@@ -29,7 +29,7 @@ Feature: PHPCR resource repository
2929
"""
3030
{
3131
"repository_alias": "phpcr_repo",
32-
"repository_type": "doctrine_phpcr",
32+
"repository_type": "phpcr/phpcr",
3333
"payload_alias": null,
3434
"payload_type": "nt:unstructured",
3535
"path": "\/foo",

tests/Features/resource_api_phpcr_odm.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Feature: PHPCR-ODM resource repository
99
cmf_resource:
1010
repositories:
1111
phpcrodm_repo:
12-
type: doctrine_phpcr_odm
12+
type: doctrine/phpcr-odm
1313
basepath: /tests/cmf/articles
1414
1515
cmf_resource_rest:
1616
payload_alias_map:
1717
article:
18-
repository: doctrine_phpcr_odm
18+
repository: doctrine/phpcr-odm
1919
type: "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article"
2020
"""
2121

@@ -30,7 +30,7 @@ Feature: PHPCR-ODM resource repository
3030
"""
3131
{
3232
"repository_alias": "phpcrodm_repo",
33-
"repository_type": "doctrine_phpcr_odm",
33+
"repository_type": "doctrine/phpcr-odm",
3434
"payload_alias": "article",
3535
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
3636
"path": "\/foo",
@@ -57,7 +57,7 @@ Feature: PHPCR-ODM resource repository
5757
"""
5858
{
5959
"repository_alias": "phpcrodm_repo",
60-
"repository_type": "doctrine_phpcr_odm",
60+
"repository_type": "doctrine/phpcr-odm",
6161
"payload_alias": "article",
6262
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
6363
"path": "\/foo",
@@ -67,7 +67,7 @@ Feature: PHPCR-ODM resource repository
6767
"children": {
6868
"bar": {
6969
"repository_alias": "phpcrodm_repo",
70-
"repository_type": "doctrine_phpcr_odm",
70+
"repository_type": "doctrine/phpcr-odm",
7171
"payload_alias": "article",
7272
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
7373
"path": "/foo/bar",
@@ -78,7 +78,7 @@ Feature: PHPCR-ODM resource repository
7878
},
7979
"boo": {
8080
"repository_alias": "phpcrodm_repo",
81-
"repository_type": "doctrine_phpcr_odm",
81+
"repository_type": "doctrine/phpcr-odm",
8282
"payload_alias": "article",
8383
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
8484
"path": "/foo/boo",

0 commit comments

Comments
 (0)