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

Commit cfd2f4c

Browse files
committed
Make the max depth configurable in config and depth query parameter
1 parent d47ba25 commit cfd2f4c

File tree

8 files changed

+98
-5
lines changed

8 files changed

+98
-5
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function load(array $configs, ContainerBuilder $container)
3535
$configuration = new Configuration();
3636
$config = $processor->processConfiguration($configuration, $configs);
3737

38+
$container->setParameter('cmf_resource_rest.max_depth', $config['max_depth']);
39+
3840
$loader->load('serializer.xml');
3941
$loader->load('resource-rest.xml');
4042

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/Resources/app/config/routing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
$collection = new RouteCollection();
1515
$collection->addCollection(
16-
$loader->import(__DIR__.'/../../../../Resources/config/routing.yml')
16+
$loader->import('@CmfResourceRestBundle/Resources/config/routing.yml')
1717
);
1818

1919
return $collection;

0 commit comments

Comments
 (0)