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

Commit 02e054d

Browse files
committed
Merge pull request #1 from symfony-cmf/develop
Develop
2 parents 48fe03a + 256ad82 commit 02e054d

34 files changed

+1178
-7
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ before_script:
2626
- composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source
2727
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
2828

29-
script: phpunit --coverage-text
29+
script:
30+
- phpunit --coverage-text
31+
- ./vendor/bin/behat
3032

3133
notifications:
3234
irc: "irc.freenode.org#symfony-cmf"

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Changelog
2+
=========
3+

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Contributing
2+
------------
3+
4+
Symfony2 CMF is an open source, community-driven project. We follow the same
5+
guidelines as core Symfony2. If you'd like to contribute, please read the
6+
[Contributing Code][1] part of the documentation. If you're submitting a pull
7+
request, please follow the guidelines in the [Submitting a Patch][2] section
8+
and use the [Pull Request Template][3].
9+
10+
[1]: http://symfony.com/doc/current/contributing/code/index.html
11+
[2]: http://symfony.com/doc/current/contributing/code/patches.html#check-list
12+
[3]: http://symfony.com/doc/current/contributing/code/patches.html#make-a-pull-request

CmfResourceRestBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use Symfony\Component\HttpKernel\Bundle\Bundle;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16-
use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Compiler\FactoryPass;
17-
use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Compiler\CompositeRepositoryPass;
1816

1917
class CmfResourceRestBundle extends Bundle
2018
{

Controller/ResourceController.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Controller;
4+
5+
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Hateoas\UrlGenerator\UrlGeneratorInterface;
8+
use JMS\Serializer\SerializerInterface;
9+
use JMS\Serializer\SerializationContext;
10+
11+
class ResourceController
12+
{
13+
/**
14+
* @var RepositoryRegistryInterface
15+
*/
16+
private $registry;
17+
18+
/**
19+
* @var UrlGeneratorInterface
20+
*/
21+
private $urlGenerator;
22+
23+
/**
24+
* @var SerializerInterface
25+
*/
26+
private $serializer;
27+
28+
/**
29+
* @param RepositoryInterface
30+
*/
31+
public function __construct(
32+
SerializerInterface $serializer,
33+
RepositoryRegistryInterface $registry,
34+
UrlGeneratorInterface $urlGenerator
35+
) {
36+
$this->serializer = $serializer;
37+
$this->registry = $registry;
38+
$this->urlGenerator = $urlGenerator;
39+
}
40+
41+
public function resourceAction($repositoryName, $path)
42+
{
43+
$repository = $this->registry->get($repositoryName);
44+
$resource = $repository->get('/' . $path);
45+
46+
$json = $this->serializer->serialize(
47+
$resource,
48+
'json',
49+
SerializationContext::create()->enableMaxDepthChecks()
50+
);
51+
52+
return new Response($json);
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19+
20+
class CmfResourceRestExtension extends Extension implements PrependExtensionInterface
21+
{
22+
public function prepend(ContainerBuilder $container)
23+
{
24+
$container->prependExtensionConfig('jms_serializer', array(
25+
'metadata' => array(
26+
'directories' => array(
27+
array(
28+
'path' => __DIR__ . '/../Resources/config/serializer',
29+
'namespace_prefix' => 'Symfony\Cmf\Component\Resource\Repository\Resource',
30+
),
31+
array(
32+
'path' => __DIR__ . '/../Resources/config/serializer',
33+
'namespace_prefix' => 'Puli\Repository\Resource',
34+
),
35+
array(
36+
'path' => __DIR__ . '/../Resources/config/serializer',
37+
'namespace_prefix' => 'PHPCR',
38+
),
39+
),
40+
),
41+
));
42+
}
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
public function load(array $configs, ContainerBuilder $container)
48+
{
49+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
50+
$loader->load('serializer.xml');
51+
$loader->load('resource-rest.xml');
52+
}
53+
}

Helper/PathHelper.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/**
15+
* Path helper.
16+
*
17+
* Used in the BazingaHateoasBundle expression language to provide
18+
* the correct value for the route generator.
19+
*
20+
* Should be removed once BazingaHateoasBundle supports custom provider
21+
* registration.
22+
*
23+
* @author Daniel Leech <[email protected]>
24+
*/
25+
class PathHelper
26+
{
27+
/**
28+
* Remove the leading "/" from the given path
29+
* This is required when generating the URL to the resource
30+
* via. expression language.
31+
*
32+
* @param string $path;
33+
* @return string
34+
*/
35+
public function relativize($path)
36+
{
37+
if (substr($path, 0, 1) == '/') {
38+
return substr($path, 1);
39+
}
40+
41+
return $path;
42+
}
43+
}

README.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Symfony CMF Resource Bundle
1+
# Symfony CMF Resource REST API Bundle
22

33
[![Build Status](https://secure.travis-ci.org/symfony-cmf/ResourceRestBundle.png?branch=master)](http://travis-ci.org/symfony-cmf/ResourceRestBundle)
4-
[![Latest Stable Version](https://poser.pugx.org/symfony-cmf/resource-rest-bundle/version.png)](https://packagist.org/packages/symfony-cmf/resource-bundle)
5-
[![Total Downloads](https://poser.pugx.org/symfony-cmf/resource-rest-bundle/d/total.png)](https://packagist.org/packages/symfony-cmf/resource-bundle)
4+
[![Latest Stable Version](https://poser.pugx.org/symfony-cmf/resource-rest-bundle/version.png)](https://packagist.org/packages/symfony-cmf/resource-rest-bundle)
5+
[![Total Downloads](https://poser.pugx.org/symfony-cmf/resource-rest-bundle/d/total.png)](https://packagist.org/packages/symfony-cmf/resource-rest-bundle)
66

7-
WIP: This bundle provides a REST API for resources
7+
This Bundle provides a REST API to [Puli](https://github.com/puliphp/puli)
8+
resources as provided by the
9+
[CmfResource](https://github.com/symfony-cmf/Resource) component.
810

911
## Requirements
1012

@@ -18,6 +20,74 @@ Not yet.
1820
* [All Symfony CMF documentation](http://symfony.com/doc/master/cmf/index.html) - complete Symfony CMF reference
1921
* [Symfony CMF Website](http://cmf.symfony.com/) - introduction, live demo, support and community links
2022

23+
## Example
24+
25+
````bash
26+
$ curl http://localhost:8000/api/phpcrodm_repo/foo | python -m json.tool
27+
{
28+
"_links": {
29+
"self": {
30+
"href": "/api/phpcrodm_repo/foo"
31+
}
32+
},
33+
"children": {
34+
"bar": {
35+
"_links": {
36+
"self": {
37+
"href": "/api/phpcrodm_repo/foo/bar"
38+
}
39+
},
40+
"children": [],
41+
"document": {
42+
"_links": {
43+
"self": {
44+
"href": "/path/to/this"
45+
}
46+
},
47+
"body": "This is my second article",
48+
"id": "/tests/cmf/articles/foo/bar",
49+
"title": "Article 2"
50+
},
51+
"path": "/foo/bar",
52+
"repo_path": "/foo/bar"
53+
},
54+
"boo": {
55+
"_links": {
56+
"self": {
57+
"href": "/api/phpcrodm_repo/foo/boo"
58+
}
59+
},
60+
"children": [],
61+
"document": {
62+
"_links": {
63+
"self": {
64+
"href": "/path/to/this"
65+
}
66+
},
67+
"body": "This is my third article",
68+
"id": "/tests/cmf/articles/foo/boo",
69+
"title": "Article 2"
70+
},
71+
"path": "/foo/boo",
72+
"repo_path": "/foo/boo"
73+
}
74+
},
75+
"document": {
76+
"_links": {
77+
"self": {
78+
"href": "/path/to/this"
79+
}
80+
},
81+
"body": "This is my article",
82+
"id": "/tests/cmf/articles/foo",
83+
"title": "Article 1"
84+
},
85+
"path": "/foo",
86+
"repo_path": "/foo"
87+
}
88+
````
89+
90+
2191
## Contributing
2292

2393
Pull requests are welcome. Please see our

Resources/config/resource-rest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<parameter key="cmf_resource_rest.path_helper.class">Symfony\Cmf\Bundle\ResourceRestBundle\Helper\PathHelper</parameter>
10+
</parameters>
11+
12+
<services>
13+
14+
<service id="cmf_resource_rest.controller.resource" class="%cmf_resource_rest.controller.resource.class%">
15+
<argument type="service" id="serializer" />
16+
<argument type="service" id="cmf_resource.registry" />
17+
<argument type="service" id="hateoas.generator.symfony" />
18+
</service>
19+
20+
<service id="cmf_resource_rest.path_helper" class="%cmf_resource_rest.path_helper.class%" />
21+
22+
</services>
23+
</container>

Resources/config/routing.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_cmf_resource:
2+
pattern: /api/{repositoryName}/{path}
3+
requirements:
4+
path: .*
5+
defaults:
6+
_controller: cmf_resource_rest.controller.resource:resourceAction
7+
_format: json

0 commit comments

Comments
 (0)