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

Commit 7067e25

Browse files
committed
trying to get hateoas integration/serialization to work
1 parent e109581 commit 7067e25

File tree

11 files changed

+90
-29
lines changed

11 files changed

+90
-29
lines changed

Controller/ResourceController.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,50 @@
22

33
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Controller;
44

5+
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\JsonResponse;
8+
use Hateoas\HateoasBuilder;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use Hateoas\UrlGenerator\UrlGeneratorInterface;
11+
use JMS\Serializer\SerializerBuilder;
12+
use JMS\Serializer\SerializerInterface;
13+
514
class ResourceController
615
{
716
/**
8-
* @var RepositoryInterface
17+
* @var RepositoryRegistryInterface
918
*/
10-
private $repository;
19+
private $registry;
20+
21+
/**
22+
* @var UrlGeneratorInterface
23+
*/
24+
private $urlGenerator;
25+
26+
private $serializer;
1127

1228
/**
1329
* @param RepositoryInterface
1430
*/
1531
public function __construct(
16-
RepositoryInterface $repository
32+
SerializerInterface $serializer,
33+
RepositoryRegistryInterface $registry,
34+
UrlGeneratorInterface $urlGenerator
1735
)
1836
{
19-
$this->repository = $repository;
37+
$this->serializer = $serializer;
38+
$this->registry = $registry;
39+
$this->urlGenerator = $urlGenerator;
2040
}
2141

22-
public function resourceAction(Request $request)
42+
public function resourceAction($repositoryName, $path)
2343
{
24-
$path = $request->query->get('path');
25-
var_dump($path);die();;
44+
$repository = $this->registry->get($repositoryName);
45+
$resource = $repository->get('/' . $path);
46+
47+
$json = $this->serializer->serialize($resource, 'json');
48+
49+
return new Response($json);
2650
}
2751
}

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ class CmfResourceRestExtension extends Extension
2626
*/
2727
public function load(array $configs, ContainerBuilder $container)
2828
{
29+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
30+
$loader->load('resource-rest.xml');
2931
}
3032
}

Resources/config/resource-rest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="cmf_resource_rest.controller.resource.class">Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController</parameter>
9+
</parameters>
10+
11+
<services>
12+
13+
<service id="cmf_resource_rest.controller.resource" class="%cmf_resource_rest.controller.resource.class%">
14+
<argument type="service" id="serializer" />
15+
<argument type="service" id="cmf_resource.registry" />
16+
<argument type="service" id="hateoas.generator.symfony" />
17+
</service>
18+
19+
</services>
20+
</container>

Resources/config/resource.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ j
1111

1212
<services>
1313

14-
<service id="cmf_resource_rest.controller" class=
14+
<service id="cmf_resource_rest.controller" class="Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController">
15+
<argument type="service" id="cmf_resource_rest.controller.resource" />
16+
</service>
1517

1618
</services>
1719
</container>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<serializer>
3+
<class name="Symfony\Cmf\Component\Resource\Repository\Resource\PhpcrOdmResource" exclusion-policy="ALL" exclude="true">
4+
<h:relation rel="self">
5+
<h:href route="_cmf_resource">
6+
<h:parameter name="repositoryName" value="testrepo" />
7+
<h:parameter name="path" value="expr(object.getPath())" />
8+
</h:href>
9+
</h:relation>
10+
<h:relation rel="children">
11+
<h:embedded>
12+
<h:content>expr(object.listChildren())</h:content>
13+
</h:embedded>
14+
</h:relation>
15+
</class>
16+
</serializer>

Tests/Features/Context/ResourceContext.php

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

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

5-
require_once(__DIR__ . '/../../Resources/app/AppKernel.php');
5+
require_once(__DIR__ . '/../../Resources/app/TestKernel.php');
66

77
use Behat\WebApiExtension\Context\WebApiContext;
88
use Behat\Symfony2Extension\Context\KernelDictionary;

Tests/Features/resource_api.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Feature: Request Resources from the REST API
1010
repository:
1111
doctrine_phpcr_odm:
1212
testrepo:
13-
basepath: /cmf/articles
13+
basepath: /tests/cmf/articles
1414
"""
1515

1616

@@ -19,7 +19,7 @@ Feature: Request Resources from the REST API
1919
| title | Article 1 |
2020
| body | This is my article |
2121
Then I send a GET request to "/api/testrepo/foo"
22-
Then open response
22+
Then print response
2323
And the response code should be 200
2424
And the response should contain json:
2525
"""

Tests/Resources/app/AppKernel.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ public function configure()
1717

1818
$this->addBundles(array(
1919
new \Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\TestBundle(),
20+
new \Symfony\Cmf\Bundle\ResourceRestBundle\CmfResourceRestBundle(),
21+
new \Symfony\Cmf\Bundle\ResourceBundle\CmfResourceBundle(),
22+
new \Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
23+
new \JMS\SerializerBundle\JMSSerializerBundle(),
2024
));
2125
}
2226

2327
public function registerContainerConfiguration(LoaderInterface $loader)
2428
{
25-
$loader->load(__DIR__.'/config/config.php');
29+
$loader->load(__DIR__.'/config/config.test.php');
2630
}
2731
}

Tests/Resources/app/TestKernel.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
<?php
22

3-
use Symfony\Cmf\Component\Testing\HttpKernel\TestKernel;
3+
require_once(__DIR__ . '/AppKernel.php');
4+
45
use Symfony\Component\Config\Loader\LoaderInterface;
56

67
/**
78
* This is the application which is tested.
89
*/
9-
class AppKernel extends TestKernel
10+
class TestKernel extends AppKernel
1011
{
11-
public function configure()
12-
{
13-
$this->requireBundleSets(array(
14-
'default', 'phpcr_odm',
15-
));
16-
17-
$this->addBundles(array(
18-
new \Symfony\Cmf\Bundle\ResourceRestBundle\CmfResourceRestBundle(),
19-
));
20-
}
21-
2212
public function registerContainerConfiguration(LoaderInterface $loader)
2313
{
2414
$loader->load(__DIR__.'/config/config.test.php');

Tests/Resources/app/config/config.test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
require(__DIR__ . '/config.php');
66

7-
$loader->import(ResourceContext::getConfigurationFile());
7+
if (file_exists(ResourceContext::getConfigurationFile())) {
8+
$loader->import(ResourceContext::getConfigurationFile());
9+
}

0 commit comments

Comments
 (0)