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

[WIP] first steps to an RESTController by the help of sylius resource-bundle #144

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

2.0.0 (unreleased)
------------------

* **2016-05-05**: REST write and delete support enabled

1.3.0
-----

Expand Down
118 changes: 118 additions & 0 deletions Controller/RESTContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Symfony\Cmf\Bundle\ContentBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Cmf\Bundle\ContentBundle\Model\Collection;
use Symfony\Cmf\Bundle\ContentBundle\Model\ManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use FOS\RestBundle\View\ViewHandlerInterface;

/**
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
class RESTContentController extends ContentController
{
/**
* @var ManagerInterface
*/
private $manager;

public function __construct(
EngineInterface $templating,
$defaultTemplate,
ManagerInterface $manager,
ViewHandlerInterface $viewHandler = null
) {
parent::__construct($templating, $defaultTemplate, $viewHandler);

$this->manager = $manager;
}

/**
* The GET action should behave as the normal ContentController::indexAction().
*
* @param Request $request
* @param object $contentDocument
* @param string $contentTemplate Symfony path of the template to render
* the content document. If omitted, the
* default template is used.
*
* @return Response
*/
public function getAction(Request $request, $contentDocument, $contentTemplate = null)
{
return parent::indexAction($request, $contentDocument, $contentTemplate);
}

/**
* A PUT request updates a given contentDocument.
*
* @param Request $request
* @param object $contentDocument
* @param string $contentTemplate Symfony path of the template to render
* the content document. If omitted, the
* default template is used.
*
* @return Response
*/
public function putAction(Request $request, $contentDocument, $contentTemplate = null)
{
return new Response('', Response::HTTP_OK);
}

/**
* The contentDocument should be of type Collection to add a new child in it.
*
* @param Request $request
* @param object $contentDocument
* @param string $contentTemplate Symfony path of the template to render
* the content document. If omitted, the
* default template is used.
*
* @return Response
*/
public function postAction(Request $request, $contentDocument, $contentTemplate = null)
{
if (!$contentDocument instanceof Collection) {
return new Response('', Response::HTTP_NOT_FOUND);
}

return new Response('', Response::HTTP_CREATED);
}

/**
* Deletes a contentDocument and redirects (by redirect or link hint) to the parent
* Collection.
*
* @param Request $request
* @param object $contentDocument
* @param string $contentTemplate Symfony path of the template to render
* the content document. If omitted, the
* default template is used.
*
* @return Response
*/
public function deleteAction(Request $request, $contentDocument, $contentTemplate = null)
{
return new Response('', Response::HTTP_NO_CONTENT);
}

/**
* You can update special fields of a given contentDocument by using the PATCH request.
*
* @param Request $request
* @param object $contentDocument
* @param string $contentTemplate Symfony path of the template to render
* the content document. If omitted, the
* default template is used.
*
* @return Response
*/
public function patchAction(Request $request, $contentDocument, $contentTemplate = null)
{
return new Response('', Response::HTTP_OK);
}
}
104 changes: 104 additions & 0 deletions Doctrine/Phpcr/ModelManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr;

use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Cmf\Bundle\ContentBundle\Model\ManagerInterface;
use Symfony\Cmf\Bundle\ContentBundle\Model\ModelManagerException;

/**
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
class ModelManager implements ManagerInterface
{
/**
* @var DocumentManager
*/
private $documentManager;

/**
* ModelManager constructor.
*
* @param DocumentManager $documentManager
*/
public function __construct(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
}

/**
* {@inheritdoc}
*
* @throws ModelManagerException if the document manager throws any exception
*/
public function create($object)
{
try {
$this->documentManager->persist($object);
$this->documentManager->flush();
} catch (\Exception $e) {
throw new ModelManagerException('', 0, $e);
}
}

/**
* {@inheritdoc}
*
* @throws ModelManagerException if the document manager throws any exception
*/
public function update($object)
{
try {
$this->documentManager->persist($object);
$this->documentManager->flush();
} catch (\Exception $e) {
throw new ModelManagerException('', 0, $e);
}
}

/**
* {@inheritdoc}
*
* @throws ModelManagerException if the document manager throws any exception
*/
public function delete($object)
{
try {
$this->documentManager->remove($object);
$this->documentManager->flush();
} catch (\Exception $e) {
throw new ModelManagerException('', 0, $e);
}
}

/**
* {@inheritdoc}
*/
public function findBy($class, array $criteria = array())
{
return $this->documentManager->getRepository($class)->findBy($criteria);
}
/**
* {@inheritdoc}
*/
public function findOneBy($class, array $criteria = array())
{
return $this->documentManager->getRepository($class)->findOneBy($criteria);
}

/**
* Find one object from the given class repository.
*
* {@inheritdoc}
*/
public function find($class, $id)
{
if (!isset($id)) {
return;
}
if (null === $class) {
return $this->documentManager->find(null, $id);
}
return $this->documentManager->getRepository($class)->find($id);
}
}
11 changes: 11 additions & 0 deletions Model/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Cmf\Bundle\ContentBundle\Model;

/**
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
class Collection
{

}
54 changes: 54 additions & 0 deletions Model/ManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Symfony\Cmf\Bundle\ContentBundle\Model;

/**
* A model manager is a bridge between several persistence implementations.
*
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
interface ManagerInterface
{
/**
* @param mixed $object
*
* @return mixed
*/
public function create($object);

/**
* @param mixed $object
*
* @return mixed
*/
public function update($object);

/**
* @param object $object
*/
public function delete($object);

/**
* @param string $class
* @param array $criteria
*
* @return array all objects matching the criteria
*/
public function findBy($class, array $criteria = array());

/**
* @param string $class
* @param array $criteria
*
* @return object an object matching the criteria or null if none match
*/
public function findOneBy($class, array $criteria = array());

/**
* @param string $class
* @param mixed $id
*
* @return object the object with id or null if not found
*/
public function find($class, $id);
}
11 changes: 11 additions & 0 deletions Model/ModelManagerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Cmf\Bundle\ContentBundle\Model;

/**
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
class ModelManagerException extends \RuntimeException
{

}
4 changes: 4 additions & 0 deletions Resources/config/persistence-phpcr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<tag name="doctrine_phpcr.initializer"/>
</service>

<service id="cmf_content.model_manager" class="Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\ModelManager">
<argument type="service" id="doctrine_phpcr.odm.default_document_manager"/>
</service>

</services>

</container>
2 changes: 2 additions & 0 deletions Resources/config/serializer/cmf/Model.StaticContent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Symfony\Cmf\Bundle\ContentBundle\Model\StaticContent:
exclusion_policy: ALL
7 changes: 7 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<argument type="service" id="fos_rest.view_handler" on-invalid="ignore"/>
</service>

<service id="cmf_content.rest_controller" class="Symfony\Cmf\Bundle\ContentBundle\Controller\RESTContentController">
<argument type="service" id="templating" />
<argument>%cmf_content.default_template%</argument>
<argument type="service" id="cmf_content.model_manager"/>
<argument type="service" id="fos_rest.view_handler" on-invalid="ignore"/>
</service>

</services>

</container>
12 changes: 12 additions & 0 deletions Tests/Resources/DataFixtures/Phpcr/LoadContentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ODM\PHPCR\Document\Generic;
use PHPCR\Util\NodeHelper;
use Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;

class LoadContentData implements FixtureInterface
{
Expand All @@ -29,13 +30,24 @@ public function load(ObjectManager $manager)
$contentRoot->setParent($root);
$manager->persist($contentRoot);

$routeRoot = new Generic();
$routeRoot->setNodename('routes');
$routeRoot->setParent($root);
$manager->persist($routeRoot);

$content = new StaticContent();
$content->setName('content-1');
$content->setTitle('Content 1');
$content->setBody('Content 1');
$content->setParentDocument($contentRoot);
$manager->persist($content);

$route = new Route();
$route->setContent($content);
$route->setParentDocument($routeRoot);
$route->setName('content-1');
$manager->persist($route);

$content = new StaticContent();
$content->setName('content-2');
$content->setTitle('Content 2');
Expand Down
3 changes: 3 additions & 0 deletions Tests/Resources/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function configure()
new \Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
new \Symfony\Cmf\Bundle\MenuBundle\CmfMenuBundle(),
new \Symfony\Cmf\Bundle\CoreBundle\CmfCoreBundle(),
new \FOS\RestBundle\FOSRestBundle(),
new \JMS\SerializerBundle\JMSSerializerBundle(),
new \Symfony\Cmf\Bundle\RestBundle\CmfRestBundle(),
));
}

Expand Down
Loading