Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Cmf\Bundle\ResourceRestBundle\Controller;

use Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Jms\Handler\ResourceHandler;
use Symfony\Cmf\Component\Resource\Puli\Api\ResourceRepository;
use Symfony\Cmf\Component\Resource\Puli\Api\ResourceNotFoundException;
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
Expand Down Expand Up @@ -38,6 +39,11 @@ class ResourceController
*/
private $serializer;

/**
* @var ResourceHandler
*/
private $resourceHandler;

/**
* @var AuthorizationCheckerInterface|null
*/
Expand All @@ -47,11 +53,12 @@ class ResourceController
* @param SerializerInterface $serializer
* @param RepositoryRegistryInterface $registry
*/
public function __construct(SerializerInterface $serializer, RepositoryRegistryInterface $registry, AuthorizationCheckerInterface $authorizationChecker = null)
public function __construct(SerializerInterface $serializer, RepositoryRegistryInterface $registry, ResourceHandler $resourceHandler, AuthorizationCheckerInterface $authorizationChecker = null)
{
$this->serializer = $serializer;
$this->registry = $registry;
$this->authorizationChecker = $authorizationChecker;
$this->resourceHandler = $resourceHandler;
}

/**
Expand All @@ -60,8 +67,12 @@ public function __construct(SerializerInterface $serializer, RepositoryRegistryI
* @param string $repositoryName
* @param string $path
*/
public function getResourceAction($repositoryName, $path)
public function getResourceAction(Request $request, $repositoryName, $path)
{
if ($request->query->has('depth')) {
$this->resourceHandler->setMaxDepth($request->query->getInt('depth'));
}

$path = '/'.ltrim($path, '/');

try {
Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/CmfResourceRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);

$bundles = $container->getParameter('kernel.bundles');
if (!array_key_exists('JMSSerializerBundle', $bundles)) {
throw new \LogicException('The JMSSerializerBundle must be registered in order to use the CmfResourceRestBundle.');
}

$container->setParameter('cmf_resource_rest.max_depth', $config['max_depth']);

$loader->load('serializer.xml');
$loader->load('resource-rest.xml');

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getConfigTreeBuilder()
->fixXmlConfig('payload_alias', 'payload_alias_map')
->fixXmlConfig('enhance', 'enhancer_map')
->children()
->integerNode('max_depth')->defaultValue(2)->end()
->arrayNode('payload_alias_map')
->useAttributeAsKey('name')
->prototype('array')
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/resource-rest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<service id="cmf_resource_rest.controller.resource" class="Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController">
<argument type="service" id="serializer" />
<argument type="service" id="cmf_resource.registry" />
<argument type="service" id="cmf_resource_rest.serializer.jms.handler.resource" />
<argument type="service" id="security.authorization_checker" on-invalid="ignore" />
</service>

Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/serializer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<argument type="service" id="cmf_resource.registry" />
<argument type="service" id="cmf_resource_rest.registry.payload_alias" />
<argument type="service" id="cmf_resource_rest.registry.enhancer" />
<argument>%cmf_resource_rest.max_depth%</argument>

<tag name="jms_serializer.subscribing_handler"/>
</service>

Expand Down
12 changes: 10 additions & 2 deletions src/Serializer/Jms/Handler/ResourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ class ResourceHandler implements SubscribingHandlerInterface
private $registry;
private $payloadAliasRegistry;
private $enhancerRegistry;
private $maxDepth;

public function __construct(
RepositoryRegistryInterface $registry,
PayloadAliasRegistry $payloadAliasRegistry,
EnhancerRegistry $enhancerRegistry
EnhancerRegistry $enhancerRegistry,
$maxDepth = 2
) {
$this->registry = $registry;
$this->payloadAliasRegistry = $payloadAliasRegistry;
$this->enhancerRegistry = $enhancerRegistry;
$this->maxDepth = $maxDepth;
}

public static function getSubscribingMethods()
Expand Down Expand Up @@ -72,6 +75,11 @@ public function serializeResource(
$context->accept($data);
}

public function setMaxDepth($maxDepth)
{
$this->maxDepth = $maxDepth;
}

private function doSerializeResource(PuliResource $resource, $depth = 0)
{
$data = array();
Expand All @@ -96,7 +104,7 @@ private function doSerializeResource(PuliResource $resource, $depth = 0)
foreach ($resource->listChildren() as $name => $childResource) {
$children[$name] = array();

if ($depth < 2) {
if ($depth < $this->maxDepth) {
$children[$name] = $this->doSerializeResource($childResource, $depth + 1);
}
}
Expand Down
68 changes: 68 additions & 0 deletions tests/Features/nesting.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Feature: Nesting resources
In order to retrieve a tree of data
As a webservice user
I need to be able to get nested resources

Background:
Given the test application has the following configuration:
"""
cmf_resource:
repositories:
default:
type: doctrine/phpcr-odm
basepath: /tests/cmf/articles
"""
And there exists an "Article" document at "/cmf/articles/foo":
| title | Article 1 |
| body | This is my article |
And there exists an "Article" document at "/cmf/articles/foo/sub":
| title | Sub-article 1 |
| body | This is my article |

Scenario: Retrieving nested resources
When I send a GET request to "/api/default/foo"
Then the response should contain json:
"""
{
"repository_alias": "default",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": null,
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "\/foo",
"node_name": "foo",
"label": "foo",
"repository_path": "\/foo",
"children": {
"sub": {
"repository_alias": "default",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": null,
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "\/foo\/sub",
"node_name": "sub",
"label": "sub",
"repository_path": "\/foo\/sub",
"children": []
}
}
}
"""

Scenario: Specifying a depth
When I send a GET request to "/api/default/foo?depth=0"
Then the response should contain json:
"""
{
"repository_alias": "default",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": null,
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "\/foo",
"node_name": "foo",
"label": "foo",
"repository_path": "\/foo",
"children": {
"sub": []
}
}
"""
4 changes: 2 additions & 2 deletions tests/Features/resource_api_filesystem.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Filesystem resource repository
# cmf_resource:
# repositories:
# default:
# type: filesystem
# type: puli/filesystem
# base_dir: "%kernel.root_dir%/Resources/views/snippets"
# """
# And there is a file named "%kernel.root_dir%/Resources/views/snippets/snippet1.html" with:
Expand All @@ -24,7 +24,7 @@ Feature: Filesystem resource repository
# """
# {
# "repository_alias": "default",
# "repository_type": "filesystem",
# "repository_type": "puli/filesystem",
# "payload_alias": null,
# "payload_type": null,
# "path": "\/snippet1.html",
Expand Down
4 changes: 2 additions & 2 deletions tests/Features/resource_api_phpcr.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: PHPCR resource repository
cmf_resource:
repositories:
phpcr_repo:
type: doctrine_phpcr
type: phpcr/phpcr
basepath: /tests/cmf/articles

cmf_resource_rest:
Expand All @@ -29,7 +29,7 @@ Feature: PHPCR resource repository
"""
{
"repository_alias": "phpcr_repo",
"repository_type": "doctrine_phpcr",
"repository_type": "phpcr/phpcr",
"payload_alias": null,
"payload_type": "nt:unstructured",
"path": "\/foo",
Expand Down
12 changes: 6 additions & 6 deletions tests/Features/resource_api_phpcr_odm.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Feature: PHPCR-ODM resource repository
cmf_resource:
repositories:
phpcrodm_repo:
type: doctrine_phpcr_odm
type: doctrine/phpcr-odm
basepath: /tests/cmf/articles

cmf_resource_rest:
payload_alias_map:
article:
repository: doctrine_phpcr_odm
repository: doctrine/phpcr-odm
type: "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article"
"""

Expand All @@ -30,7 +30,7 @@ Feature: PHPCR-ODM resource repository
"""
{
"repository_alias": "phpcrodm_repo",
"repository_type": "doctrine_phpcr_odm",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": "article",
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "\/foo",
Expand All @@ -57,7 +57,7 @@ Feature: PHPCR-ODM resource repository
"""
{
"repository_alias": "phpcrodm_repo",
"repository_type": "doctrine_phpcr_odm",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": "article",
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "\/foo",
Expand All @@ -67,7 +67,7 @@ Feature: PHPCR-ODM resource repository
"children": {
"bar": {
"repository_alias": "phpcrodm_repo",
"repository_type": "doctrine_phpcr_odm",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": "article",
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "/foo/bar",
Expand All @@ -78,7 +78,7 @@ Feature: PHPCR-ODM resource repository
},
"boo": {
"repository_alias": "phpcrodm_repo",
"repository_type": "doctrine_phpcr_odm",
"repository_type": "doctrine/phpcr-odm",
"payload_alias": "article",
"payload_type": "Symfony\\Cmf\\Bundle\\ResourceRestBundle\\Tests\\Resources\\TestBundle\\Document\\Article",
"path": "/foo/boo",
Expand Down
2 changes: 1 addition & 1 deletion tests/Features/security.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Security
cmf_resource:
repositories:
security:
type: doctrine_phpcr
type: phpcr/phpcr
basepath: /tests/cmf/articles
"""
And there exists an "Article" document at "/private/foo":
Expand Down
2 changes: 1 addition & 1 deletion tests/Resources/app/config/routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

$collection = new RouteCollection();
$collection->addCollection(
$loader->import(__DIR__.'/../../../../Resources/config/routing.yml')
$loader->import('@CmfResourceRestBundle/Resources/config/routing.yml')
);

return $collection;
16 changes: 15 additions & 1 deletion tests/Unit/DependencyInjection/CmfResourceRestExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,23 @@ public function provideExtension()
*/
public function testExtension($config)
{
$this->container->setParameter('kernel.bundles', array());
$this->container->setParameter('kernel.bundles', ['JMSSerializerBundle' => true]);
$this->container->addCompilerPass(new EnhancerPass());

$this->load($config);

$this->compile();
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage The JMSSerializerBundle must be registered
*/
public function testNoJmsSerializerBundleRegistered()
{
$this->container->setParameter('kernel.bundles', []);

$this->load([]);
$this->compile();
}
}
1 change: 1 addition & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function testConfig($source)
'enhancer' => 'my_enhancer',
),
),
'max_depth' => 2,
), array($source));
}
}