From 5a13dd349936410681bd1637c8e290fe3569fb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 13 Nov 2014 10:40:09 -0400 Subject: [PATCH 01/16] Initial test --- .../Resources/skeleton/rest/actions/delete.php.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 11bce1f..9021817 100644 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -1,6 +1,6 @@ /** {% block phpdoc_method_header %} - * Delete a {{ entity }} entity. + * Delete a {{ entity }} entity. Wberredo version * * @View(statusCode=204) * From 3c9a713b38e6401c9cec112af6af19b9bdb6b982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 20 Nov 2014 13:31:36 -0400 Subject: [PATCH 02/16] Work with handlers as service --- .../Controller/VoryxController.php | 47 ---- .../Generator/DoctrineRESTGenerator.php | 264 +++++++++++++++--- .../skeleton/rest/actions/delete.php.twig | 13 +- .../skeleton/rest/actions/getAll.php.twig | 22 +- .../skeleton/rest/actions/getById.php.twig | 5 +- .../skeleton/rest/actions/getOr404.php.twig | 14 + .../skeleton/rest/actions/patch.php.twig | 5 +- .../skeleton/rest/actions/post.php.twig | 20 +- .../skeleton/rest/actions/put.php.twig | 26 +- .../skeleton/rest/controller.php.twig | 8 +- .../skeleton/rest/form_exception.php.twig | 29 ++ .../Resources/skeleton/rest/handler.php.twig | 43 +++ .../skeleton/rest/handler/construct.php.twig | 12 + .../skeleton/rest/handler/delete.php.twig | 16 ++ .../skeleton/rest/handler/extras.php.twig | 20 ++ .../skeleton/rest/handler/get.php.twig | 9 + .../skeleton/rest/handler/getAll.php.twig | 9 + .../skeleton/rest/handler/patch.php.twig | 9 + .../skeleton/rest/handler/post.php.twig | 11 + .../skeleton/rest/handler/put.php.twig | 9 + .../skeleton/rest/service/services.xml.twig | 6 + 21 files changed, 449 insertions(+), 148 deletions(-) delete mode 100644 src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/form_exception.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/construct.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/extras.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/get.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/patch.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/post.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/put.php.twig create mode 100644 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/service/services.xml.twig diff --git a/src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php b/src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php deleted file mode 100644 index 51cca5c..0000000 --- a/src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php +++ /dev/null @@ -1,47 +0,0 @@ -container->get('form.factory')->createNamed( - null, //since we're not including the form name in the request, set this to null - $type, - $data, - $options - ); - - return $form; - } - - /** - * Get rid on any fields that don't appear in the form - * - * @param Request $request - * @param Form $form - */ - protected function removeExtraFields(Request $request, Form $form) - { - $data = $request->request->all(); - $children = $form->all(); - $data = array_intersect_key($data, $children); - $request->request->replace($data); - } -} diff --git a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php index d664e2e..ccef8a1 100644 --- a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php +++ b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php @@ -11,6 +11,7 @@ namespace Voryx\RESTGeneratorBundle\Generator; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Doctrine\ORM\Mapping\ClassMetadataInfo; @@ -38,41 +39,52 @@ class DoctrineRESTGenerator extends Generator */ public function __construct(Filesystem $filesystem) { - $this->filesystem = $filesystem; + $this->filesystem = $filesystem; } /** * Generate the REST controller. * - * @param BundleInterface $bundle A bundle object - * @param string $entity The entity relative class name - * @param ClassMetadataInfo $metadata The entity class metadata - * @param string $routePrefix The route name prefix - * @param array $forceOverwrite Whether or not to overwrite an existing controller + * @param BundleInterface $bundle A bundle object + * @param string $entity The entity relative class name + * @param ClassMetadataInfo $metadata The entity class metadata + * @param string $routePrefix The route name prefix + * @param array $forceOverwrite Whether or not to overwrite an existing controller * * @throws \RuntimeException */ - public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $routePrefix, $forceOverwrite) - { + public function generate( + BundleInterface $bundle, + $entity, + ClassMetadataInfo $metadata, + $routePrefix, + $forceOverwrite + ) { $this->routePrefix = $routePrefix; $this->routeNamePrefix = str_replace('/', '_', $routePrefix); $this->actions = array('getById', 'getAll', 'post', 'put', 'delete'); if (count($metadata->identifier) > 1) { - throw new \RuntimeException('The REST api generator does not support entity classes with multiple primary keys.'); + throw new \RuntimeException( + 'The REST api generator does not support entity classes with multiple primary keys.' + ); } if (!in_array('id', $metadata->identifier)) { - throw new \RuntimeException('The REST api generator expects the entity object has a primary key field named "id" with a getId() method.'); + throw new \RuntimeException( + 'The REST api generator expects the entity object has a primary key field named "id" with a getId() method.' + ); } - $this->entity = $entity; - $this->bundle = $bundle; + $this->entity = $entity; + $this->bundle = $bundle; $this->metadata = $metadata; $this->setFormat('yml'); $this->generateControllerClass($forceOverwrite); - + $this->generateHandler($forceOverwrite); + $this->generateExceptionClass(); + $this->declareService(); } /** @@ -112,13 +124,17 @@ protected function generateConfiguration() $this->format ); - $this->renderFile('rest/config/routing.'.$this->format.'.twig', $target, array( - 'actions' => $this->actions, - 'route_prefix' => $this->routePrefix, - 'route_name_prefix' => $this->routeNamePrefix, - 'bundle' => $this->bundle->getName(), - 'entity' => $this->entity, - )); + $this->renderFile( + 'rest/config/routing.' . $this->format . '.twig', + $target, + array( + 'actions' => $this->actions, + 'route_prefix' => $this->routePrefix, + 'route_name_prefix' => $this->routeNamePrefix, + 'bundle' => $this->bundle->getName(), + 'entity' => $this->entity, + ) + ); } /** @@ -144,17 +160,166 @@ protected function generateControllerClass($forceOverwrite) throw new \RuntimeException('Unable to generate the controller as it already exists.'); } - $this->renderFile('rest/controller.php.twig', $target, array( - 'actions' => $this->actions, - 'route_prefix' => $this->routePrefix, - 'route_name_prefix' => $this->routeNamePrefix, - 'bundle' => $this->bundle->getName(), - 'entity' => $this->entity, - 'entity_class' => $entityClass, - 'namespace' => $this->bundle->getNamespace(), - 'entity_namespace' => $entityNamespace, - 'format' => $this->format, - )); + $this->renderFile( + 'rest/controller.php.twig', + $target, + array( + 'route_prefix' => $this->routePrefix, + 'route_name_prefix' => $this->routeNamePrefix, + 'bundle' => $this->bundle->getName(), + 'entity' => $this->entity, + 'entity_class' => $entityClass, + 'namespace' => $this->bundle->getNamespace(), + 'entity_namespace' => $entityNamespace, + 'format' => $this->format, + ) + ); + } + + /** + * Generates the Handle only. + */ + protected function generateHandler($forceOverwrite) + { + $dir = $this->bundle->getPath(); + + $parts = explode('\\', $this->entity); + $entityClass = array_pop($parts); + $entityNamespace = implode('\\', $parts); + + $target = sprintf( + '%s/Handler/%s/%sRESTHandler.php', + $dir, + str_replace('\\', '/', $entityNamespace), + $entityClass + ); + + if (!is_dir(dirname($target))) { + mkdir(dirname($target), 0777, true); + } + + if (!$forceOverwrite && file_exists($target)) { + throw new \RuntimeException('Unable to generate the controller as it already exists.'); + } + + $this->renderFile( + 'rest/handler.php.twig', + $target, + array( + 'route_prefix' => $this->routePrefix, + 'route_name_prefix' => $this->routeNamePrefix, + 'bundle' => $this->bundle->getName(), + 'entity' => $this->entity, + 'entity_class' => $entityClass, + 'namespace' => $this->bundle->getNamespace(), + 'entity_namespace' => $entityNamespace, + 'format' => $this->format, + ) + ); + } + + public function generateExceptionClass() + { + $dir = $this->bundle->getPath(); + + $target = sprintf('%s/Exception/InvalidFormException.php', $dir); + + if (!is_dir(dirname($target))) { + mkdir(dirname($target), 0777, true); + } + + $this->renderFile( + 'rest/form_exception.php.twig', + $target, + array('namespace' => $this->bundle->getNamespace()) + ); + } + + /** + * Declares the handler as a service + */ + public function declareService() + { + $dir = $this->bundle->getPath(); + + $parts = explode('\\', $this->entity); + $entityClass = array_pop($parts); + $entityNamespace = implode('\\', $parts); + $namespace = $this->bundle->getNamespace(); + + $bundleName = strtolower($this->bundle->getName()); + $entityName = strtolower($this->entity); + + $services = sprintf( + "%s/Resources/config/servicesREST.xml", + $dir + ); + + $handlerClass = sprintf( + "%s\\Handler\\%s%sRESTHandler", + $namespace, + $entityNamespace, + $entityClass + ); + + $newId = sprintf( + "%s.%s.handler", + str_replace("bundle", "", $bundleName), + $entityName + ); + + $fileName = sprintf( + "%s/DependencyInjection/%s.php", + $dir, + str_replace("Bundle", "Extension", $this->bundle->getName()) + ); + + if (!is_file($services)) { + $this->renderFile("rest/service/services.xml.twig", $services, array()); + } + + $newXML = simplexml_load_file($services); + + if (!($servicesTag = $newXML->services)) { + $servicesTag = $newXML->addChild("services"); + } + + $search = $newXML->xpath("//*[@id='$newId']"); + if(!$search) { + $newServiceTag = $servicesTag->addChild("service"); + $newServiceTag->addAttribute("id", $newId); + $newServiceTag->addAttribute("class", $handlerClass); + + $entityManagerTag = $newServiceTag->addChild("argument"); + $entityManagerTag->addAttribute("type", "service"); + $entityManagerTag->addAttribute("id", "doctrine.orm.entity_manager"); + + $newServiceTag->addChild( + "argument", + sprintf( + "%s\\Entity\\%s%s", + $namespace, + $entityNamespace, + $entityClass + ) + ); + + $formFactoryTag = $newServiceTag->addChild("argument"); + $formFactoryTag->addAttribute("type", "service"); + $formFactoryTag->addAttribute("id", "form.factory"); + } + + $newXML->saveXML($services); + + $content = file_get_contents($fileName); + if (!strpos($content, "servicesREST.xml")) { + $content = str_replace("}", "", $content); + + $content .= "\t\t\$loader2 = new Loader\\XmlFileLoader(\$container, new FileLocator(__DIR__ . '/../Resources/config'));" . PHP_EOL . + "\t\t\$loader2->load('servicesREST.xml');" . PHP_EOL . "\t}" . PHP_EOL . "}"; + + file_put_contents($fileName, $content); + } } /** @@ -167,20 +332,29 @@ protected function generateTestClass() $entityClass = array_pop($parts); $entityNamespace = implode('\\', $parts); - $dir = $this->bundle->getPath() .'/Tests/Controller'; - $target = $dir .'/'. str_replace('\\', '/', $entityNamespace).'/'. $entityClass .'RESTControllerTest.php'; - - $this->renderFile('rest/tests/test.php.twig', $target, array( - 'route_prefix' => $this->routePrefix, - 'route_name_prefix' => $this->routeNamePrefix, - 'entity' => $this->entity, - 'bundle' => $this->bundle->getName(), - 'entity_class' => $entityClass, - 'namespace' => $this->bundle->getNamespace(), - 'entity_namespace' => $entityNamespace, - 'actions' => $this->actions, - 'form_type_name' => strtolower(str_replace('\\', '_', $this->bundle->getNamespace()).($parts ? '_' : '').implode('_', $parts).'_'.$entityClass.'Type'), - )); + $dir = $this->bundle->getPath() . '/Tests/Controller'; + $target = $dir . '/' . str_replace('\\', '/', $entityNamespace) . '/' . $entityClass . 'RESTControllerTest.php'; + + $this->renderFile( + 'rest/tests/test.php.twig', + $target, + array( + 'route_prefix' => $this->routePrefix, + 'route_name_prefix' => $this->routeNamePrefix, + 'entity' => $this->entity, + 'bundle' => $this->bundle->getName(), + 'entity_class' => $entityClass, + 'namespace' => $this->bundle->getNamespace(), + 'entity_namespace' => $entityNamespace, + 'actions' => $this->actions, + 'form_type_name' => strtolower( + str_replace('\\', '_', $this->bundle->getNamespace()) . ($parts ? '_' : '') . implode( + '_', + $parts + ) . '_' . $entityClass . 'Type' + ), + ) + ); } } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig old mode 100644 new mode 100755 index 9021817..7511e13 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -15,18 +15,15 @@ {% endblock phpdoc_method_annotations %} */ {% block method_definition %} - public function deleteAction(Request $request, {{ entity }} $entity) + public function deleteAction($id) {% endblock method_definition %} { {% block method_body %} + ${{ entity|lower }} = $this->getOr404($id); try { - $em = $this->getDoctrine()->getManager(); - $em->remove($entity); - $em->flush(); - - return null; - } catch (\Exception $e) { - return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR); + return $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->delete(${{ entity|lower }}); + } catch (\Exception $exception) { + return $exception; } {% endblock method_body %} {% block method_return '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig old mode 100644 new mode 100755 index 0acdc63..e9f44bf --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -22,22 +22,16 @@ {% endblock method_definition %} { {% block method_body %} - try { - $offset = $paramFetcher->get('offset'); - $limit = $paramFetcher->get('limit'); - $order_by = $paramFetcher->get('order_by'); - $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); + $offset = $paramFetcher->get('offset'); + $limit = $paramFetcher->get('limit'); + $order_by = $paramFetcher->get('order_by'); + $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); - $em = $this->getDoctrine()->getManager(); - $entities = $em->getRepository('{{ bundle }}:{{ entity }}')->findBy($filters, $order_by, $limit, $offset); - if ($entities) { - return $entities; - } - - return FOSView::create('Not Found', Codes::HTTP_NO_CONTENT); - } catch (\Exception $e) { - return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR); + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); + if ($answer['{{ entity|lower }}']) { + return $answer; } + return null; {% endblock method_body %} {% block method_return '' %} } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig old mode 100644 new mode 100755 index 848910e..4461e82 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -13,11 +13,12 @@ {% endblock phpdoc_method_annotations %} */ {% block method_definition %} - public function getAction({{ entity }} $entity) + public function getAction($id) {% endblock method_definition %} { {% block method_body %} - return $entity; + $answer['{{ entity|lower }}'] = $this->getOr404($id); + return $answer; {% endblock method_body %} {% block method_return '' %} } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig new file mode 100644 index 0000000..35ded1b --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig @@ -0,0 +1,14 @@ + +{% block method_definition %} + protected function getOr404($id) +{% endblock method_definition %} + { +{% block method_body %} + if (!($entity = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id))) { + throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.',$id)); + } + + return $entity; +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig old mode 100644 new mode 100755 index 323cb7a..fb43876 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -14,11 +14,12 @@ {% endblock phpdoc_method_annotations %} */ {% block method_definition %} - public function patchAction(Request $request, {{ entity }} $entity) + public function patchAction(Request $request, $id) {% endblock method_definition %} { {% block method_body %} - return $this->putAction($request, $entity); + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); + return $answer; {% endblock method_body %} {% block method_return '' %} } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig old mode 100644 new mode 100755 index 54beeec..47c604b --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -18,22 +18,14 @@ {% endblock method_definition %} { {% block method_body %} - $entity = new {{ entity }}(); - $form = $this->createForm(new {{ entity }}Type(), $entity, array("method" => $request->getMethod())); - $this->removeExtraFields($request, $form); - $form->handleRequest($request); + try { + $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $answer['{{ entity|lower }}'] = $new; - if ($form->isValid()) { - $em = $this->getDoctrine()->getManager(); - $em->persist($entity); - $em->flush(); - - return $entity; + return $answer; + } catch (InvalidFormException $exception) { + return $exception->getForm(); } - {% endblock method_body %} -{% block method_return %} - return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR); -{% endblock method_return %} } {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig old mode 100644 new mode 100755 index 31cf008..7d39cd5 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -14,26 +14,24 @@ {% endblock phpdoc_method_annotations %} */ {% block method_definition %} - public function putAction(Request $request, {{ entity }} $entity) + public function putAction(Request $request, $id) {% endblock method_definition %} { {% block method_body %} try { - $em = $this->getDoctrine()->getManager(); - $request->setMethod('PATCH'); //Treat all PUTs as PATCH - $form = $this->createForm(new {{ entity }}Type(), $entity, array("method" => $request->getMethod())); - $this->removeExtraFields($request, $form); - $form->handleRequest($request); - if ($form->isValid()) { - $em->flush(); - - return $entity; + if ($bairro = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); + $code = Codes::HTTP_OK; + } else { + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $code = Codes::HTTP_CREATED; } - - return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR); - } catch (\Exception $e) { - return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR); + } catch (InvalidFormException $exception) { + return $exception->getForm(); } + + $view = $this->view($answer, $code); + return $this->handleView($view); {% endblock method_body %} {% block method_return '' %} } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig old mode 100644 new mode 100755 index 6cf716a..6ffcb8e --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig @@ -5,6 +5,7 @@ namespace {{ namespace }}\Controller{{ entity_namespace ? '\\' ~ entity_namespac {% block use_statements %} use {{ namespace }}\Entity\{{ entity }}; use {{ namespace }}\Form\{{ entity }}Type; +use {{ namespace }}\Exception\InvalidFormException; use FOS\RestBundle\Controller\Annotations\QueryParam; use FOS\RestBundle\Controller\Annotations\RouteResource; @@ -22,8 +23,9 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Form\Form; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Voryx\RESTGeneratorBundle\Controller\VoryxController; +use FOS\RestBundle\Controller\FOSRestController; {% endblock use_statements %} /** @@ -38,7 +40,7 @@ use Voryx\RESTGeneratorBundle\Controller\VoryxController; {% endblock phpdoc_class_annotations %} */ {% block class_definition %} -class {{ entity_class }}RESTController extends VoryxController +class {{ entity_class }}RESTController extends FOSRestController {% endblock class_definition %} { {% block class_body %} @@ -53,5 +55,7 @@ class {{ entity_class }}RESTController extends VoryxController {%- include 'rest/actions/patch.php.twig' %} {%- include 'rest/actions/delete.php.twig' %} + + {%- include 'rest/actions/getOr404.php.twig' %} {% endblock class_body %} } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/form_exception.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/form_exception.php.twig new file mode 100644 index 0000000..14a0b4c --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/form_exception.php.twig @@ -0,0 +1,29 @@ +form = $form; + } + + public function getForm() + { + return $this->form; + } +{% endblock class_body %} +} \ No newline at end of file diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler.php.twig new file mode 100644 index 0000000..0eaeace --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler.php.twig @@ -0,0 +1,43 @@ +om = $om; + $this->entityClass = $entityClass; + $this->repository = $this->om->getRepository($this->entityClass); + $this->formFactory = $formFactory; +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig new file mode 100644 index 0000000..c5ead31 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig @@ -0,0 +1,16 @@ +{% block method_definition %} + public function delete({{ entity }} ${{ entity|lower }}) +{% endblock method_definition %} + { +{% block method_body %} + try { + $this->om->remove(${{ entity|lower }}); + $this->om->flush(); + + return null; + } catch (\Exception $e) { + throw new \RuntimeException("Exclusion not allowed"); + } +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/extras.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/extras.php.twig new file mode 100644 index 0000000..903caf5 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/extras.php.twig @@ -0,0 +1,20 @@ +{% block extras_methods %} + private function processForm({{ entity }} ${{ entity|lower }}, array $parameters, $method = "PUT") + { + $form = $this->formFactory->create(new {{ entity }}Type(), ${{ entity|lower }}, array('method' => $method)); + $form->submit($parameters, 'PATCH' !== $method); + if ($form->isValid()) { + ${{ entity|lower }} = $form->getData(); + $this->om->persist(${{ entity|lower }}); + $this->om->flush(${{ entity|lower }}); + + return ${{ entity|lower }}; + } + throw new InvalidFormException('Invalid submitted data', $form); + } + + private function create{{ entity }}() + { + return new $this->entityClass(); + } +{% endblock extras_methods %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/get.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/get.php.twig new file mode 100644 index 0000000..202e2e7 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/get.php.twig @@ -0,0 +1,9 @@ +{% block method_definition %} + public function get($id) +{% endblock method_definition %} + { +{% block method_body %} + return $this->repository->find($id); +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig new file mode 100644 index 0000000..74fa0b5 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig @@ -0,0 +1,9 @@ +{% block method_definition %} + public function getAll($filters, $order_by, $limit, $offset) +{% endblock method_definition %} + { +{% block method_body %} + return $this->repository->findBy($filters, $order_by, $limit, $offset); +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/patch.php.twig new file mode 100644 index 0000000..fbe9b54 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/patch.php.twig @@ -0,0 +1,9 @@ +{% block method_definition %} + public function patch({{ entity }} ${{ entity|lower }}, array $parameters) +{% endblock method_definition %} + { +{% block method_body %} + return $this->processForm(${{ entity|lower }}, $parameters, 'PATCH'); +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/post.php.twig new file mode 100644 index 0000000..7cfa712 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/post.php.twig @@ -0,0 +1,11 @@ +{% block method_definition %} + public function post($parameters) +{% endblock method_definition %} + { +{% block method_body %} + ${{ entity|lower }} = $this->create{{ entity }}(); + + return $this->processForm(${{ entity|lower }}, $parameters, 'POST'); +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/put.php.twig new file mode 100644 index 0000000..5c44191 --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/put.php.twig @@ -0,0 +1,9 @@ +{% block method_definition %} + public function put({{ entity }} ${{ entity|lower }}, array $parameters) +{% endblock method_definition %} + { +{% block method_body %} + return $this->processForm(${{ entity|lower }}, $parameters, 'PUT'); +{% endblock method_body %} + } + diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/service/services.xml.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/service/services.xml.twig new file mode 100644 index 0000000..124152f --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/service/services.xml.twig @@ -0,0 +1,6 @@ + + + + \ No newline at end of file From 8c096804093c0bb3b74f5bc61afb01a7c179f5d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 20 Nov 2014 13:38:11 -0400 Subject: [PATCH 03/16] Revert "Initial test" This reverts commit 5a13dd349936410681bd1637c8e290fe3569fb90. Only a commit test --- .../Resources/skeleton/rest/actions/delete.php.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 7511e13..2dce68a 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -1,6 +1,6 @@ /** {% block phpdoc_method_header %} - * Delete a {{ entity }} entity. Wberredo version + * Delete a {{ entity }} entity. * * @View(statusCode=204) * From 812692d30a9b228816737066ab36ef74b2af1dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 20 Nov 2014 14:19:12 -0400 Subject: [PATCH 04/16] restore VoryxController class, for older controllers. --- .../Controller/VoryxController.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php diff --git a/src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php b/src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php new file mode 100644 index 0000000..95847dd --- /dev/null +++ b/src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php @@ -0,0 +1,41 @@ +container->get('form.factory')->createNamed( + null, //since we're not including the form name in the request, set this to null + $type, + $data, + $options + ); + return $form; + } + /** + * Get rid on any fields that don't appear in the form + * + * @param Request $request + * @param Form $form + */ + protected function removeExtraFields(Request $request, Form $form) + { + $data = $request->request->all(); + $children = $form->all(); + $data = array_intersect_key($data, $children); + $request->request->replace($data); + } +} \ No newline at end of file From eb08d00b8b855b350481697ffe1f661d72256133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 20 Nov 2014 14:42:48 -0400 Subject: [PATCH 05/16] refactory of put template --- .../Resources/skeleton/rest/actions/put.php.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index 7d39cd5..c8cd398 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -19,7 +19,7 @@ { {% block method_body %} try { - if ($bairro = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { + if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); $code = Codes::HTTP_OK; } else { From 28d43e9ebca0d3875ec772f4b8514a63eaf9f7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 20 Nov 2014 16:27:41 -0400 Subject: [PATCH 06/16] Use the NelmioApiDocBundle to document the API --- README.md | 3 +++ composer.json | 3 ++- .../Resources/skeleton/rest/actions/delete.php.twig | 9 +++++++++ .../Resources/skeleton/rest/actions/getAll.php.twig | 8 ++++++++ .../Resources/skeleton/rest/actions/getById.php.twig | 9 +++++++++ .../Resources/skeleton/rest/actions/getOr404.php.twig | 0 .../Resources/skeleton/rest/actions/patch.php.twig | 10 ++++++++++ .../Resources/skeleton/rest/actions/post.php.twig | 9 +++++++++ .../Resources/skeleton/rest/actions/put.php.twig | 11 +++++++++++ 9 files changed, 61 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig diff --git a/README.md b/README.md index af64c7c..2e0b399 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ public function registerBundles() new FOS\RestBundle\FOSRestBundle(), new JMS\SerializerBundle\JMSSerializerBundle($this), new Nelmio\CorsBundle\NelmioCorsBundle(), + new Nelmio\ApiDocBundle\NelmioApiDocBundle(), //... ); //... @@ -73,6 +74,8 @@ sensio_framework_extra: request: { converters: true } view: { annotations: false } router: { annotations: true } + +nelmio_api_doc: ~ ``` ## Generating the Controller diff --git a/composer.json b/composer.json index 1aab61a..533e3cb 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "php": ">=5.3.0", "friendsofsymfony/rest-bundle": "1.4.*", "jms/serializer-bundle": "0.13.*", - "nelmio/cors-bundle": "1.3.*" + "nelmio/cors-bundle": "1.3.*", + "nelmio/api-doc-bundle": "~2.7" }, "autoload": { "psr-0": { diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 2dce68a..c282306 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -2,6 +2,15 @@ {% block phpdoc_method_header %} * Delete a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Delete a {{ entity }} entity.", + * statusCodes = { + * 204 = "No content. Successfully excluded.", + * 404 = "Not found." + * } + * ) + * * @View(statusCode=204) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig index e9f44bf..33bb934 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -2,6 +2,14 @@ {% block phpdoc_method_header %} * Get all {{ entity }} entities. * + * @ApiDoc( + * resource = true, + * description = "Get all {{ entity }} entities.", + * statusCodes = { + * 200 = "List of {{ entity }}", + * 204 = "No content. Nothing to list." + * } + * ) * @View(serializerEnableMaxDepthChecks=true) * * @param ParamFetcherInterface $paramFetcher diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index 4461e82..55aaeaa 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -2,6 +2,15 @@ {% block phpdoc_method_header %} * Get a {{ entity }} entity * + * @ApiDoc( + * resource = true, + * description = "Get a {{ entity }} entity.", + * statusCodes = { + * 200 = "{{ entity }}'s object.", + * 404 = "Not Found." + * } + * ) + * * @View(serializerEnableMaxDepthChecks=true) * * @return Response diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig old mode 100644 new mode 100755 diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index fb43876..45ccd01 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -2,6 +2,16 @@ {% block phpdoc_method_header %} * Partial Update to a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Partial Update to a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + * * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 47c604b..2f88538 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -2,6 +2,15 @@ {% block phpdoc_method_header %} * Create a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Create a {{ entity }} entity.", + * statusCodes = { + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) * @View(statusCode=201, serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index c8cd398..81fdcbc 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -2,6 +2,17 @@ {% block phpdoc_method_header %} * Update a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Update a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + * * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request From 72219c34580700805c348c000bf0f9d4cade64aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Thu, 20 Nov 2014 16:27:41 -0400 Subject: [PATCH 07/16] Add 'use' statement to NelmioAPiDoc --- README.md | 3 +++ composer.json | 3 ++- .../Resources/skeleton/rest/actions/delete.php.twig | 9 +++++++++ .../Resources/skeleton/rest/actions/getAll.php.twig | 8 ++++++++ .../Resources/skeleton/rest/actions/getById.php.twig | 9 +++++++++ .../Resources/skeleton/rest/actions/getOr404.php.twig | 0 .../Resources/skeleton/rest/actions/patch.php.twig | 10 ++++++++++ .../Resources/skeleton/rest/actions/post.php.twig | 9 +++++++++ .../Resources/skeleton/rest/actions/put.php.twig | 11 +++++++++++ .../Resources/skeleton/rest/controller.php.twig | 1 + 10 files changed, 62 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig diff --git a/README.md b/README.md index af64c7c..2e0b399 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ public function registerBundles() new FOS\RestBundle\FOSRestBundle(), new JMS\SerializerBundle\JMSSerializerBundle($this), new Nelmio\CorsBundle\NelmioCorsBundle(), + new Nelmio\ApiDocBundle\NelmioApiDocBundle(), //... ); //... @@ -73,6 +74,8 @@ sensio_framework_extra: request: { converters: true } view: { annotations: false } router: { annotations: true } + +nelmio_api_doc: ~ ``` ## Generating the Controller diff --git a/composer.json b/composer.json index 1aab61a..533e3cb 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "php": ">=5.3.0", "friendsofsymfony/rest-bundle": "1.4.*", "jms/serializer-bundle": "0.13.*", - "nelmio/cors-bundle": "1.3.*" + "nelmio/cors-bundle": "1.3.*", + "nelmio/api-doc-bundle": "~2.7" }, "autoload": { "psr-0": { diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 2dce68a..c282306 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -2,6 +2,15 @@ {% block phpdoc_method_header %} * Delete a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Delete a {{ entity }} entity.", + * statusCodes = { + * 204 = "No content. Successfully excluded.", + * 404 = "Not found." + * } + * ) + * * @View(statusCode=204) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig index e9f44bf..33bb934 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -2,6 +2,14 @@ {% block phpdoc_method_header %} * Get all {{ entity }} entities. * + * @ApiDoc( + * resource = true, + * description = "Get all {{ entity }} entities.", + * statusCodes = { + * 200 = "List of {{ entity }}", + * 204 = "No content. Nothing to list." + * } + * ) * @View(serializerEnableMaxDepthChecks=true) * * @param ParamFetcherInterface $paramFetcher diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index 4461e82..55aaeaa 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -2,6 +2,15 @@ {% block phpdoc_method_header %} * Get a {{ entity }} entity * + * @ApiDoc( + * resource = true, + * description = "Get a {{ entity }} entity.", + * statusCodes = { + * 200 = "{{ entity }}'s object.", + * 404 = "Not Found." + * } + * ) + * * @View(serializerEnableMaxDepthChecks=true) * * @return Response diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig old mode 100644 new mode 100755 diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index fb43876..45ccd01 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -2,6 +2,16 @@ {% block phpdoc_method_header %} * Partial Update to a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Partial Update to a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + * * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 47c604b..2f88538 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -2,6 +2,15 @@ {% block phpdoc_method_header %} * Create a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Create a {{ entity }} entity.", + * statusCodes = { + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) * @View(statusCode=201, serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index c8cd398..81fdcbc 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -2,6 +2,17 @@ {% block phpdoc_method_header %} * Update a {{ entity }} entity. * + * @ApiDoc( + * resource = true, + * description = "Update a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + * * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig index 6ffcb8e..1794ea2 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig @@ -26,6 +26,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use FOS\RestBundle\Controller\FOSRestController; +use Nelmio\ApiDocBundle\Annotation\ApiDoc; {% endblock use_statements %} /** From 5c1b219f815dd8416e5f2ffc10f3e8cb330d0cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 24 Nov 2014 10:45:07 -0400 Subject: [PATCH 08/16] Fix the function that update DependencyInjection file --- .../Generator/DoctrineRESTGenerator.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php index ccef8a1..28c6504 100644 --- a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php +++ b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php @@ -310,18 +310,25 @@ public function declareService() } $newXML->saveXML($services); + $this->updateDIFile($fileName); + } + + private function updateDIFile($fileName) + { + $toInput = PHP_EOL."\t\t\$loader2 = new Loader\\XmlFileLoader(\$container, new FileLocator(__DIR__ . '/../Resources/config'));" .PHP_EOL. + "\t\t\$loader2->load('servicesREST.xml');".PHP_EOL."\t"; - $content = file_get_contents($fileName); - if (!strpos($content, "servicesREST.xml")) { - $content = str_replace("}", "", $content); + $text = file_get_contents($fileName); - $content .= "\t\t\$loader2 = new Loader\\XmlFileLoader(\$container, new FileLocator(__DIR__ . '/../Resources/config'));" . PHP_EOL . - "\t\t\$loader2->load('servicesREST.xml');" . PHP_EOL . "\t}" . PHP_EOL . "}"; + if (strpos($text, "servicesREST.xml") == false) { + $position = strpos($text, "}", strpos($text, "function load(")); - file_put_contents($fileName, $content); + $newContent = substr_replace($text, $toInput, $position, 0); + file_put_contents($fileName, $newContent); } } + /** * Generates the functional test class only. * From e12a93b828261be7cf9864db4eb0961bda86baef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 24 Nov 2014 14:57:44 -0400 Subject: [PATCH 09/16] Make documentation be optional --- .../Command/GenerateDoctrineRESTCommand.php | 54 ++++++------- .../Generator/DoctrineRESTGenerator.php | 8 +- .../skeleton/rest/actions/delete.php.twig | 61 ++++++++------- .../skeleton/rest/actions/getAll.php.twig | 72 +++++++++-------- .../skeleton/rest/actions/getById.php.twig | 49 ++++++------ .../skeleton/rest/actions/patch.php.twig | 51 ++++++------ .../skeleton/rest/actions/post.php.twig | 62 ++++++++------- .../skeleton/rest/actions/put.php.twig | 77 ++++++++++--------- .../skeleton/rest/controller.php.twig | 2 + 9 files changed, 231 insertions(+), 205 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php b/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php index 65ed376..821ce40 100644 --- a/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php +++ b/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php @@ -33,10 +33,11 @@ protected function configure() { $this ->setDefinition(array( - new InputOption('entity', '', InputOption::VALUE_REQUIRED, 'The entity class name to initialize (shortcut notation)'), - new InputOption('route-prefix', '', InputOption::VALUE_REQUIRED, 'The route prefix'), - new InputOption('overwrite', '', InputOption::VALUE_NONE, 'Do not stop the generation if rest api controller already exist, thus overwriting all generated files'), - )) + new InputOption('entity', '', InputOption::VALUE_REQUIRED, 'The entity class name to initialize (shortcut notation)'), + new InputOption('route-prefix', '', InputOption::VALUE_REQUIRED, 'The route prefix'), + new InputOption('overwrite', '', InputOption::VALUE_NONE, 'Do not stop the generation if rest api controller already exist, thus overwriting all generated files'), + new InputOption('document', '', InputOption::VALUE_NONE, 'Use NelmioApiDocBundle to document the controller') + )) ->setDescription('Generates a REST api based on a Doctrine entity') ->setHelp(<<voryx:generate:rest command generates a REST api based on a Doctrine entity. @@ -88,9 +89,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle).'\\'.$entity; $metadata = $this->getEntityMetadata($entityClass); $bundle = $this->getContainer()->get('kernel')->getBundle($bundle); + $document = $input->getOption('document'); $generator = $this->getGenerator($bundle); - $generator->generate($bundle, $entity, $metadata[0], $prefix, $forceOverwrite); + $generator->generate($bundle, $entity, $metadata[0], $prefix, $forceOverwrite, $document); $output->writeln('Generating the REST api code: OK'); @@ -116,16 +118,16 @@ protected function interact(InputInterface $input, OutputInterface $output) // namespace $output->writeln(array( - '', - 'This command helps you generate a REST api controller.', - '', - 'First, you need to give the entity for which you want to generate a REST api.', - 'You can give an entity that does not exist yet and the wizard will help', - 'you defining it.', - '', - 'You must use the shortcut notation like AcmeBlogBundle:Post.', - '', - )); + '', + 'This command helps you generate a REST api controller.', + '', + 'First, you need to give the entity for which you want to generate a REST api.', + 'You can give an entity that does not exist yet and the wizard will help', + 'you defining it.', + '', + 'You must use the shortcut notation like AcmeBlogBundle:Post.', + '', + )); $entity = $dialog->askAndValidate($output, $dialog->getQuestion('The Entity shortcut name', $input->getOption('entity')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'), false, $input->getOption('entity')); $input->setOption('entity', $entity); @@ -134,22 +136,22 @@ protected function interact(InputInterface $input, OutputInterface $output) // route prefix $prefix = $this->getRoutePrefix($input, $entity); $output->writeln(array( - '', - 'Determine the routes prefix (all the routes will be "mounted" under this', - 'prefix: /prefix/, /prefix/new, ...).', - '', - )); + '', + 'Determine the routes prefix (all the routes will be "mounted" under this', + 'prefix: /prefix/, /prefix/new, ...).', + '', + )); $prefix = $dialog->ask($output, $dialog->getQuestion('Routes prefix', '/'.$prefix), '/'.$prefix); $input->setOption('route-prefix', $prefix); // summary $output->writeln(array( - '', - $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), - '', - sprintf("You are going to generate a REST api controller for \"%s:%s\"", $bundle, $entity), - '', - )); + '', + $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), + '', + sprintf("You are going to generate a REST api controller for \"%s:%s\"", $bundle, $entity), + '', + )); } /** diff --git a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php index 28c6504..25f9321 100644 --- a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php +++ b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php @@ -58,7 +58,8 @@ public function generate( $entity, ClassMetadataInfo $metadata, $routePrefix, - $forceOverwrite + $forceOverwrite, + $document ) { $this->routePrefix = $routePrefix; $this->routeNamePrefix = str_replace('/', '_', $routePrefix); @@ -81,7 +82,7 @@ public function generate( $this->metadata = $metadata; $this->setFormat('yml'); - $this->generateControllerClass($forceOverwrite); + $this->generateControllerClass($forceOverwrite, $document); $this->generateHandler($forceOverwrite); $this->generateExceptionClass(); $this->declareService(); @@ -141,7 +142,7 @@ protected function generateConfiguration() * Generates the controller class only. * */ - protected function generateControllerClass($forceOverwrite) + protected function generateControllerClass($forceOverwrite, $document) { $dir = $this->bundle->getPath(); @@ -172,6 +173,7 @@ protected function generateControllerClass($forceOverwrite) 'namespace' => $this->bundle->getNamespace(), 'entity_namespace' => $entityNamespace, 'format' => $this->format, + 'document' => $document, ) ); } diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index c282306..a7357e5 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -1,40 +1,43 @@ - /** +/** {% block phpdoc_method_header %} - * Delete a {{ entity }} entity. - * - * @ApiDoc( - * resource = true, - * description = "Delete a {{ entity }} entity.", - * statusCodes = { - * 204 = "No content. Successfully excluded.", - * 404 = "Not found." - * } - * ) - * - * @View(statusCode=204) - * - * @param Request $request - * @param $entity - * @internal param $id - * - * @return Response + * Delete a {{ entity }} entity. + * + {% block documentation %} + {% if document %} + * @ApiDoc( + * resource = true, + * description = "Delete a {{ entity }} entity.", + * statusCodes = { + * 204 = "No content. Successfully excluded.", + * 404 = "Not found." + * } + * ) + {% endif %} + {% endblock documentation %} + * @View(statusCode=204) + * + * @param Request $request + * @param $entity + * @internal param $id + * + * @return Response {% endblock phpdoc_method_header %} {% block phpdoc_method_annotations %} -{% if 'annotation' == format %}{% endif %} + {% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} - */ +*/ {% block method_definition %} public function deleteAction($id) {% endblock method_definition %} - { +{ {% block method_body %} - ${{ entity|lower }} = $this->getOr404($id); - try { - return $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->delete(${{ entity|lower }}); - } catch (\Exception $exception) { - return $exception; - } + ${{ entity|lower }} = $this->getOr404($id); + try { + return $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->delete(${{ entity|lower }}); + } catch (\Exception $exception) { + return $exception; + } {% endblock method_body %} {% block method_return '' %} - } +} {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig index 33bb934..8845fe3 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -1,46 +1,50 @@ - /** +/** {% block phpdoc_method_header %} - * Get all {{ entity }} entities. - * - * @ApiDoc( - * resource = true, - * description = "Get all {{ entity }} entities.", - * statusCodes = { - * 200 = "List of {{ entity }}", - * 204 = "No content. Nothing to list." - * } - * ) - * @View(serializerEnableMaxDepthChecks=true) - * - * @param ParamFetcherInterface $paramFetcher - * - * @return Response - * - * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.") - * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.") - * @QueryParam(name="order_by", nullable=true, array=true, description="Order by fields. Must be an array ie. &order_by[name]=ASC&order_by[description]=DESC") - * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3") + * Get all {{ entity }} entities. + * + {% block documentation %} + {% if document %} + * @ApiDoc( + * resource = true, + * description = "Get all {{ entity }} entities.", + * statusCodes = { + * 200 = "List of {{ entity }}", + * 204 = "No content. Nothing to list." + * } + * ) + {% endif %} + {% endblock documentation %} + * @View(serializerEnableMaxDepthChecks=true) + * + * @param ParamFetcherInterface $paramFetcher + * + * @return Response + * + * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.") + * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.") + * @QueryParam(name="order_by", nullable=true, array=true, description="Order by fields. Must be an array ie. &order_by[name]=ASC&order_by[description]=DESC") + * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3") {% endblock phpdoc_method_header %} {% block phpdoc_method_annotations %} -{% if 'annotation' == format %}{% endif %} + {% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} - */ +*/ {% block method_definition %} public function cgetAction(ParamFetcherInterface $paramFetcher) {% endblock method_definition %} - { +{ {% block method_body %} - $offset = $paramFetcher->get('offset'); - $limit = $paramFetcher->get('limit'); - $order_by = $paramFetcher->get('order_by'); - $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); + $offset = $paramFetcher->get('offset'); + $limit = $paramFetcher->get('limit'); + $order_by = $paramFetcher->get('order_by'); + $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); - if ($answer['{{ entity|lower }}']) { - return $answer; - } - return null; + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); + if ($answer['{{ entity|lower }}']) { + return $answer; + } + return null; {% endblock method_body %} {% block method_return '' %} - } +} {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index 55aaeaa..9f4f5c8 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -1,34 +1,37 @@ - /** +/** {% block phpdoc_method_header %} - * Get a {{ entity }} entity - * - * @ApiDoc( - * resource = true, - * description = "Get a {{ entity }} entity.", - * statusCodes = { - * 200 = "{{ entity }}'s object.", - * 404 = "Not Found." - * } - * ) - * - * @View(serializerEnableMaxDepthChecks=true) - * - * @return Response + * Get a {{ entity }} entity + * + {% block documentation %} + {% if document %} + * @ApiDoc( + * resource = true, + * description = "Get a {{ entity }} entity.", + * statusCodes = { + * 200 = "{{ entity }}'s object.", + * 404 = "Not Found." + * } + * ) + {% endif %} + {% endblock documentation %} + * @View(serializerEnableMaxDepthChecks=true) + * + * @return Response {% endblock phpdoc_method_header %} - * +* {% block phpdoc_method_annotations %} -{% if 'annotation' == format %} -{% endif %} + {% if 'annotation' == format %} + {% endif %} {% endblock phpdoc_method_annotations %} - */ +*/ {% block method_definition %} public function getAction($id) {% endblock method_definition %} - { +{ {% block method_body %} - $answer['{{ entity|lower }}'] = $this->getOr404($id); - return $answer; + $answer['{{ entity|lower }}'] = $this->getOr404($id); + return $answer; {% endblock method_body %} {% block method_return '' %} - } +} {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index 45ccd01..13c53ca 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -1,36 +1,39 @@ - /** +/** {% block phpdoc_method_header %} - * Partial Update to a {{ entity }} entity. - * - * @ApiDoc( - * resource = true, - * description = "Partial Update to a {{ entity }} entity.", - * statusCodes = { - * 200 = "Updated object.", - * 400 = "Bad Request. Verify your params.", - * 404 = "Not Found." - * } - * ) - * - * @View(serializerEnableMaxDepthChecks=true) - * - * @param Request $request - * @param $entity - * - * @return Response + * Partial Update to a {{ entity }} entity. + * + {% block documentation %} + {% if document %} + * @ApiDoc( + * resource = true, + * description = "Partial Update to a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + {% endif %} + {% endblock documentation %} + * @View(serializerEnableMaxDepthChecks=true) + * + * @param Request $request + * @param $entity + * + * @return Response {% endblock phpdoc_method_header %} {% block phpdoc_method_annotations %} -{% if 'annotation' == format %}{% endif %} + {% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} */ {% block method_definition %} public function patchAction(Request $request, $id) {% endblock method_definition %} - { +{ {% block method_body %} - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); - return $answer; + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); + return $answer; {% endblock method_body %} {% block method_return '' %} - } +} {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 2f88538..0ae9e24 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -1,40 +1,44 @@ - /** +/** {% block phpdoc_method_header %} - * Create a {{ entity }} entity. - * - * @ApiDoc( - * resource = true, - * description = "Create a {{ entity }} entity.", - * statusCodes = { - * 201 = "Created object.", - * 400 = "Bad Request. Verify your params.", - * 404 = "Not Found." - * } - * ) - * @View(statusCode=201, serializerEnableMaxDepthChecks=true) - * - * @param Request $request - * - * @return Response + * Create a {{ entity }} entity. + * + {% block documentation %} + {% if document %} + * @ApiDoc( + * resource = true, + * description = "Create a {{ entity }} entity.", + * statusCodes = { + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + {% endif %} + {% endblock documentation %} + * @View(statusCode=201, serializerEnableMaxDepthChecks=true) + * + * @param Request $request + * + * @return Response {% endblock phpdoc_method_header %} - * +* {% block phpdoc_method_annotations %} -{% if 'annotation' == format %}{% endif %} + {% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} - */ +*/ {% block method_definition %} public function postAction(Request $request) {% endblock method_definition %} - { +{ {% block method_body %} - try { - $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); - $answer['{{ entity|lower }}'] = $new; + try { + $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $answer['{{ entity|lower }}'] = $new; - return $answer; - } catch (InvalidFormException $exception) { - return $exception->getForm(); - } -{% endblock method_body %} + return $answer; + } catch (InvalidFormException $exception) { + return $exception->getForm(); } +{% endblock method_body %} +} {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index 81fdcbc..a728ffe 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -1,49 +1,52 @@ - /** +/** {% block phpdoc_method_header %} - * Update a {{ entity }} entity. - * - * @ApiDoc( - * resource = true, - * description = "Update a {{ entity }} entity.", - * statusCodes = { - * 200 = "Updated object.", - * 201 = "Created object.", - * 400 = "Bad Request. Verify your params.", - * 404 = "Not Found." - * } - * ) - * - * @View(serializerEnableMaxDepthChecks=true) - * - * @param Request $request - * @param $entity - * - * @return Response + * Update a {{ entity }} entity. + * + {% block documentation %} + {% if document %} + * @ApiDoc( + * resource = true, + * description = "Update a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) + {% endif %} + {% endblock documentation %} + * @View(serializerEnableMaxDepthChecks=true) + * + * @param Request $request + * @param $entity + * + * @return Response {% endblock phpdoc_method_header %} {% block phpdoc_method_annotations %} -{% if 'annotation' == format %}{% endif %} + {% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} - */ +*/ {% block method_definition %} public function putAction(Request $request, $id) {% endblock method_definition %} - { +{ {% block method_body %} - try { - if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); - $code = Codes::HTTP_OK; - } else { - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); - $code = Codes::HTTP_CREATED; - } - } catch (InvalidFormException $exception) { - return $exception->getForm(); - } + try { + if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); + $code = Codes::HTTP_OK; + } else { + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $code = Codes::HTTP_CREATED; + } + } catch (InvalidFormException $exception) { + return $exception->getForm(); + } - $view = $this->view($answer, $code); - return $this->handleView($view); + $view = $this->view($answer, $code); + return $this->handleView($view); {% endblock method_body %} {% block method_return '' %} - } +} {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig index 1794ea2..624a63c 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/controller.php.twig @@ -26,7 +26,9 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use FOS\RestBundle\Controller\FOSRestController; +{% if document %} use Nelmio\ApiDocBundle\Annotation\ApiDoc; +{% endif %} {% endblock use_statements %} /** From a4f924772878249661cf2fdb1a98dbc202b448ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 24 Nov 2014 15:04:01 -0400 Subject: [PATCH 10/16] Fix ident on documentation --- .../skeleton/rest/actions/delete.php.twig | 24 ++++++++-------- .../skeleton/rest/actions/getAll.php.twig | 24 ++++++++-------- .../skeleton/rest/actions/getById.php.twig | 24 ++++++++-------- .../skeleton/rest/actions/patch.php.twig | 26 ++++++++--------- .../skeleton/rest/actions/post.php.twig | 26 ++++++++--------- .../skeleton/rest/actions/put.php.twig | 28 +++++++++---------- 6 files changed, 76 insertions(+), 76 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index a7357e5..6207291 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -2,18 +2,18 @@ {% block phpdoc_method_header %} * Delete a {{ entity }} entity. * - {% block documentation %} - {% if document %} - * @ApiDoc( - * resource = true, - * description = "Delete a {{ entity }} entity.", - * statusCodes = { - * 204 = "No content. Successfully excluded.", - * 404 = "Not found." - * } - * ) - {% endif %} - {% endblock documentation %} +{% block documentation %} +{% if document %} + * @ApiDoc( + * resource = true, + * description = "Delete a {{ entity }} entity.", + * statusCodes = { + * 204 = "No content. Successfully excluded.", + * 404 = "Not found." + * } + * ) +{% endif %} +{% endblock documentation %} * @View(statusCode=204) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig index 8845fe3..84550be 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -2,18 +2,18 @@ {% block phpdoc_method_header %} * Get all {{ entity }} entities. * - {% block documentation %} - {% if document %} - * @ApiDoc( - * resource = true, - * description = "Get all {{ entity }} entities.", - * statusCodes = { - * 200 = "List of {{ entity }}", - * 204 = "No content. Nothing to list." - * } - * ) - {% endif %} - {% endblock documentation %} +{% block documentation %} +{% if document %} + * @ApiDoc( + * resource = true, + * description = "Get all {{ entity }} entities.", + * statusCodes = { + * 200 = "List of {{ entity }}", + * 204 = "No content. Nothing to list." + * } + * ) +{% endif %} +{% endblock documentation %} * @View(serializerEnableMaxDepthChecks=true) * * @param ParamFetcherInterface $paramFetcher diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index 9f4f5c8..088485e 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -2,18 +2,18 @@ {% block phpdoc_method_header %} * Get a {{ entity }} entity * - {% block documentation %} - {% if document %} - * @ApiDoc( - * resource = true, - * description = "Get a {{ entity }} entity.", - * statusCodes = { - * 200 = "{{ entity }}'s object.", - * 404 = "Not Found." - * } - * ) - {% endif %} - {% endblock documentation %} +{% block documentation %} +{% if document %} + * @ApiDoc( + * resource = true, + * description = "Get a {{ entity }} entity.", + * statusCodes = { + * 200 = "{{ entity }}'s object.", + * 404 = "Not Found." + * } + * ) +{% endif %} +{% endblock documentation %} * @View(serializerEnableMaxDepthChecks=true) * * @return Response diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index 13c53ca..bb801b7 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -2,19 +2,19 @@ {% block phpdoc_method_header %} * Partial Update to a {{ entity }} entity. * - {% block documentation %} - {% if document %} - * @ApiDoc( - * resource = true, - * description = "Partial Update to a {{ entity }} entity.", - * statusCodes = { - * 200 = "Updated object.", - * 400 = "Bad Request. Verify your params.", - * 404 = "Not Found." - * } - * ) - {% endif %} - {% endblock documentation %} +{% block documentation %} +{% if document %} + * @ApiDoc( + * resource = true, + * description = "Partial Update to a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) +{% endif %} +{% endblock documentation %} * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 0ae9e24..28e21b0 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -2,19 +2,19 @@ {% block phpdoc_method_header %} * Create a {{ entity }} entity. * - {% block documentation %} - {% if document %} - * @ApiDoc( - * resource = true, - * description = "Create a {{ entity }} entity.", - * statusCodes = { - * 201 = "Created object.", - * 400 = "Bad Request. Verify your params.", - * 404 = "Not Found." - * } - * ) - {% endif %} - {% endblock documentation %} +{% block documentation %} +{% if document %} + * @ApiDoc( + * resource = true, + * description = "Create a {{ entity }} entity.", + * statusCodes = { + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) +{% endif %} +{% endblock documentation %} * @View(statusCode=201, serializerEnableMaxDepthChecks=true) * * @param Request $request diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index a728ffe..4b256c9 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -2,20 +2,20 @@ {% block phpdoc_method_header %} * Update a {{ entity }} entity. * - {% block documentation %} - {% if document %} - * @ApiDoc( - * resource = true, - * description = "Update a {{ entity }} entity.", - * statusCodes = { - * 200 = "Updated object.", - * 201 = "Created object.", - * 400 = "Bad Request. Verify your params.", - * 404 = "Not Found." - * } - * ) - {% endif %} - {% endblock documentation %} +{% block documentation %} +{% if document %} + * @ApiDoc( + * resource = true, + * description = "Update a {{ entity }} entity.", + * statusCodes = { + * 200 = "Updated object.", + * 201 = "Created object.", + * 400 = "Bad Request. Verify your params.", + * 404 = "Not Found." + * } + * ) +{% endif %} +{% endblock documentation %} * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request From ad5bc0736497955cfcd05f1eafd2ec1063b8ae35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 24 Nov 2014 15:13:44 -0400 Subject: [PATCH 11/16] Indent of functions on controller --- .../skeleton/rest/actions/delete.php.twig | 16 +++++----- .../skeleton/rest/actions/getAll.php.twig | 22 +++++++------- .../skeleton/rest/actions/getById.php.twig | 9 +++--- .../skeleton/rest/actions/patch.php.twig | 8 ++--- .../skeleton/rest/actions/post.php.twig | 18 +++++------ .../skeleton/rest/actions/put.php.twig | 30 +++++++++---------- 6 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 6207291..f9db7e6 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -29,15 +29,15 @@ {% block method_definition %} public function deleteAction($id) {% endblock method_definition %} -{ + { {% block method_body %} - ${{ entity|lower }} = $this->getOr404($id); - try { - return $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->delete(${{ entity|lower }}); - } catch (\Exception $exception) { - return $exception; - } + ${{ entity|lower }} = $this->getOr404($id); + try { + return $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->delete(${{ entity|lower }}); + } catch (\Exception $exception) { + return $exception; + } {% endblock method_body %} {% block method_return '' %} -} + } {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig index 84550be..d8de8f4 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -32,19 +32,19 @@ {% block method_definition %} public function cgetAction(ParamFetcherInterface $paramFetcher) {% endblock method_definition %} -{ + { {% block method_body %} - $offset = $paramFetcher->get('offset'); - $limit = $paramFetcher->get('limit'); - $order_by = $paramFetcher->get('order_by'); - $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); + $offset = $paramFetcher->get('offset'); + $limit = $paramFetcher->get('limit'); + $order_by = $paramFetcher->get('order_by'); + $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); - if ($answer['{{ entity|lower }}']) { - return $answer; - } - return null; + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); + if ($answer['{{ entity|lower }}']) { + return $answer; + } + return null; {% endblock method_body %} {% block method_return '' %} -} + } {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index 088485e..a4621e4 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -18,7 +18,6 @@ * * @return Response {% endblock phpdoc_method_header %} -* {% block phpdoc_method_annotations %} {% if 'annotation' == format %} {% endif %} @@ -27,11 +26,11 @@ {% block method_definition %} public function getAction($id) {% endblock method_definition %} -{ + { {% block method_body %} - $answer['{{ entity|lower }}'] = $this->getOr404($id); - return $answer; + $answer['{{ entity|lower }}'] = $this->getOr404($id); + return $answer; {% endblock method_body %} {% block method_return '' %} -} + } {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index bb801b7..0a50021 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -29,11 +29,11 @@ {% block method_definition %} public function patchAction(Request $request, $id) {% endblock method_definition %} -{ + { {% block method_body %} - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); - return $answer; + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); + return $answer; {% endblock method_body %} {% block method_return '' %} -} + } {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 28e21b0..4b639ae 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -29,16 +29,16 @@ {% block method_definition %} public function postAction(Request $request) {% endblock method_definition %} -{ + { {% block method_body %} - try { - $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); - $answer['{{ entity|lower }}'] = $new; + try { + $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $answer['{{ entity|lower }}'] = $new; - return $answer; - } catch (InvalidFormException $exception) { - return $exception->getForm(); - } + return $answer; + } catch (InvalidFormException $exception) { + return $exception->getForm(); + } {% endblock method_body %} -} + } {% block form '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index 4b256c9..14134e5 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -30,23 +30,23 @@ {% block method_definition %} public function putAction(Request $request, $id) {% endblock method_definition %} -{ + { {% block method_body %} - try { - if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); - $code = Codes::HTTP_OK; - } else { - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); - $code = Codes::HTTP_CREATED; - } - } catch (InvalidFormException $exception) { - return $exception->getForm(); - } + try { + if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); + $code = Codes::HTTP_OK; + } else { + $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $code = Codes::HTTP_CREATED; + } + } catch (InvalidFormException $exception) { + return $exception->getForm(); + } - $view = $this->view($answer, $code); - return $this->handleView($view); + $view = $this->view($answer, $code); + return $this->handleView($view); {% endblock method_body %} {% block method_return '' %} -} + } {% block form '' %} From 19c410c0f2e4f813e99bc00a7598bed90d4d43f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 24 Nov 2014 16:12:17 -0400 Subject: [PATCH 12/16] Make the resource as object be optional. --- .../Command/GenerateDoctrineRESTCommand.php | 132 +++++++++++++----- .../Generator/DoctrineRESTGenerator.php | 12 +- .../skeleton/rest/actions/delete.php.twig | 2 +- .../skeleton/rest/actions/getAll.php.twig | 6 +- .../skeleton/rest/actions/getById.php.twig | 4 +- .../skeleton/rest/actions/patch.php.twig | 4 +- .../skeleton/rest/actions/post.php.twig | 7 +- .../skeleton/rest/actions/put.php.twig | 6 +- 8 files changed, 119 insertions(+), 54 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php b/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php index 821ce40..568a4bc 100644 --- a/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php +++ b/src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php @@ -32,15 +32,39 @@ class GenerateDoctrineRESTCommand extends GenerateDoctrineCommand protected function configure() { $this - ->setDefinition(array( - new InputOption('entity', '', InputOption::VALUE_REQUIRED, 'The entity class name to initialize (shortcut notation)'), + ->setDefinition( + array( + new InputOption( + 'entity', + '', + InputOption::VALUE_REQUIRED, + 'The entity class name to initialize (shortcut notation)' + ), new InputOption('route-prefix', '', InputOption::VALUE_REQUIRED, 'The route prefix'), - new InputOption('overwrite', '', InputOption::VALUE_NONE, 'Do not stop the generation if rest api controller already exist, thus overwriting all generated files'), - new InputOption('document', '', InputOption::VALUE_NONE, 'Use NelmioApiDocBundle to document the controller') - )) + new InputOption( + 'overwrite', + '', + InputOption::VALUE_NONE, + 'Do not stop the generation if rest api controller already exist, thus overwriting all generated files' + ), + new InputOption( + 'resource', + '', + InputOption::VALUE_NONE, + 'The object will return with the resource name' + ), + new InputOption( + 'document', + '', + InputOption::VALUE_NONE, + 'Use NelmioApiDocBundle to document the controller' + ), + ) + ) ->setDescription('Generates a REST api based on a Doctrine entity') - ->setHelp(<<voryx:generate:rest command generates a REST api based on a Doctrine entity. + ->setHelp( + <<voryx:generate:rest command generates a REST api based on a Doctrine entity. php app/console voryx:generate:rest --entity=AcmeBlogBundle:Post --route-prefix=post_admin @@ -59,8 +83,7 @@ protected function configure() EOT ) ->setName('voryx:generate:rest') - ->setAliases(array('generate:voryx:rest')) - ; + ->setAliases(array('generate:voryx:rest')); } /** @@ -71,7 +94,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $dialog = $this->getDialogHelper(); if ($input->isInteractive()) { - if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) { + if (!$dialog->askConfirmation( + $output, + $dialog->getQuestion('Do you confirm generation', 'yes', '?'), + true + ) + ) { $output->writeln('Command aborted'); return 1; @@ -86,13 +114,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $dialog->writeSection($output, 'REST api generation'); - $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle).'\\'.$entity; - $metadata = $this->getEntityMetadata($entityClass); - $bundle = $this->getContainer()->get('kernel')->getBundle($bundle); - $document = $input->getOption('document'); + $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle) . '\\' . $entity; + $metadata = $this->getEntityMetadata($entityClass); + $bundle = $this->getContainer()->get('kernel')->getBundle($bundle); + $resource = $input->getOption('resource'); + $document = $input->getOption('document'); $generator = $this->getGenerator($bundle); - $generator->generate($bundle, $entity, $metadata[0], $prefix, $forceOverwrite, $document); + $generator->generate($bundle, $entity, $metadata[0], $prefix, $forceOverwrite, $resource, $document); $output->writeln('Generating the REST api code: OK'); @@ -117,7 +146,8 @@ protected function interact(InputInterface $input, OutputInterface $output) $dialog->writeSection($output, 'Welcome to the Doctrine2 REST api generator'); // namespace - $output->writeln(array( + $output->writeln( + array( '', 'This command helps you generate a REST api controller.', '', @@ -127,31 +157,42 @@ protected function interact(InputInterface $input, OutputInterface $output) '', 'You must use the shortcut notation like AcmeBlogBundle:Post.', '', - )); - - $entity = $dialog->askAndValidate($output, $dialog->getQuestion('The Entity shortcut name', $input->getOption('entity')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'), false, $input->getOption('entity')); + ) + ); + + $entity = $dialog->askAndValidate( + $output, + $dialog->getQuestion('The Entity shortcut name', $input->getOption('entity')), + array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'), + false, + $input->getOption('entity') + ); $input->setOption('entity', $entity); list($bundle, $entity) = $this->parseShortcutNotation($entity); // route prefix $prefix = $this->getRoutePrefix($input, $entity); - $output->writeln(array( + $output->writeln( + array( '', 'Determine the routes prefix (all the routes will be "mounted" under this', 'prefix: /prefix/, /prefix/new, ...).', '', - )); - $prefix = $dialog->ask($output, $dialog->getQuestion('Routes prefix', '/'.$prefix), '/'.$prefix); + ) + ); + $prefix = $dialog->ask($output, $dialog->getQuestion('Routes prefix', '/' . $prefix), '/' . $prefix); $input->setOption('route-prefix', $prefix); // summary - $output->writeln(array( + $output->writeln( + array( '', $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), '', sprintf("You are going to generate a REST api controller for \"%s:%s\"", $bundle, $entity), '', - )); + ) + ); } /** @@ -161,38 +202,61 @@ protected function generateForm($bundle, $entity, $metadata) { try { $this->getFormGenerator($bundle)->generate($bundle, $entity, $metadata[0]); - } catch (\RuntimeException $e ) { + } catch (\RuntimeException $e) { // form already exists } } - protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, BundleInterface $bundle, $entity, $prefix) - { + protected function updateRouting( + DialogHelper $dialog, + InputInterface $input, + OutputInterface $output, + BundleInterface $bundle, + $entity, + $prefix + ) { $auto = true; if ($input->isInteractive()) { - $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true); + $auto = $dialog->askConfirmation( + $output, + $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), + true + ); } $output->write('Importing the REST api routes: '); - $this->getContainer()->get('filesystem')->mkdir($bundle->getPath().'/Resources/config/'); - $routing = new RoutingManipulator($bundle->getPath().'/Resources/config/routing.yml'); + $this->getContainer()->get('filesystem')->mkdir($bundle->getPath() . '/Resources/config/'); + $routing = new RoutingManipulator($bundle->getPath() . '/Resources/config/routing.yml'); try { // TODO: fix the format parameter - leaving it for now $format = "annotation"; - $ret = $auto ? $routing->addResource($bundle->getName(), $format, '/'.$prefix, 'routing/'.strtolower(str_replace('\\', '_', $entity))) : false; + $ret = $auto ? $routing->addResource( + $bundle->getName(), + $format, + '/' . $prefix, + 'routing/' . strtolower(str_replace('\\', '_', $entity)) + ) : false; } catch (\RuntimeException $exc) { $ret = false; } if (!$ret) { - $help = sprintf(" resource: \"@%s/Resources/config/routing/%s.%s\"\n", $bundle->getName(), strtolower(str_replace('\\', '_', $entity)), $format); + $help = sprintf( + " resource: \"@%s/Resources/config/routing/%s.%s\"\n", + $bundle->getName(), + strtolower(str_replace('\\', '_', $entity)), + $format + ); $help .= sprintf(" prefix: /%s\n", $prefix); return array( '- Import the bundle\'s routing resource in the bundle routing file', - sprintf(' (%s).', $bundle->getPath().'/Resources/config/routing.yml'), + sprintf(' (%s).', $bundle->getPath() . '/Resources/config/routing.yml'), '', - sprintf(' %s:', $bundle->getName().('' !== $prefix ? '_'.str_replace('/', '_', $prefix) : '')), + sprintf( + ' %s:', + $bundle->getName() . ('' !== $prefix ? '_' . str_replace('/', '_', $prefix) : '') + ), $help, '', ); diff --git a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php index 25f9321..64ff329 100644 --- a/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php +++ b/src/Voryx/RESTGeneratorBundle/Generator/DoctrineRESTGenerator.php @@ -59,6 +59,7 @@ public function generate( ClassMetadataInfo $metadata, $routePrefix, $forceOverwrite, + $resource, $document ) { $this->routePrefix = $routePrefix; @@ -82,7 +83,7 @@ public function generate( $this->metadata = $metadata; $this->setFormat('yml'); - $this->generateControllerClass($forceOverwrite, $document); + $this->generateControllerClass($forceOverwrite, $document, $resource); $this->generateHandler($forceOverwrite); $this->generateExceptionClass(); $this->declareService(); @@ -142,7 +143,7 @@ protected function generateConfiguration() * Generates the controller class only. * */ - protected function generateControllerClass($forceOverwrite, $document) + protected function generateControllerClass($forceOverwrite, $document, $resource) { $dir = $this->bundle->getPath(); @@ -173,6 +174,7 @@ protected function generateControllerClass($forceOverwrite, $document) 'namespace' => $this->bundle->getNamespace(), 'entity_namespace' => $entityNamespace, 'format' => $this->format, + 'resource' => $resource, 'document' => $document, ) ); @@ -287,7 +289,7 @@ public function declareService() } $search = $newXML->xpath("//*[@id='$newId']"); - if(!$search) { + if (!$search) { $newServiceTag = $servicesTag->addChild("service"); $newServiceTag->addAttribute("id", $newId); $newServiceTag->addAttribute("class", $handlerClass); @@ -317,8 +319,8 @@ public function declareService() private function updateDIFile($fileName) { - $toInput = PHP_EOL."\t\t\$loader2 = new Loader\\XmlFileLoader(\$container, new FileLocator(__DIR__ . '/../Resources/config'));" .PHP_EOL. - "\t\t\$loader2->load('servicesREST.xml');".PHP_EOL."\t"; + $toInput = PHP_EOL . "\t\t\$loader2 = new Loader\\XmlFileLoader(\$container, new FileLocator(__DIR__ . '/../Resources/config'));" . PHP_EOL . + "\t\t\$loader2->load('servicesREST.xml');" . PHP_EOL . "\t"; $text = file_get_contents($fileName); diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index f9db7e6..37c4dce 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -1,4 +1,4 @@ -/** + /** {% block phpdoc_method_header %} * Delete a {{ entity }} entity. * diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig index d8de8f4..50e3245 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getAll.php.twig @@ -1,4 +1,4 @@ -/** + /** {% block phpdoc_method_header %} * Get all {{ entity }} entities. * @@ -39,8 +39,8 @@ $order_by = $paramFetcher->get('order_by'); $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array(); - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); - if ($answer['{{ entity|lower }}']) { + $answer{{ resource ? "['" ~ entity|lower ~ "']" }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->getAll($filters, $order_by, $limit, $offset); + if ($answer{{ resource ? "['" ~ entity|lower ~ "']" }}) { return $answer; } return null; diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index a4621e4..45b14f8 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -1,4 +1,4 @@ -/** + /** {% block phpdoc_method_header %} * Get a {{ entity }} entity * @@ -28,7 +28,7 @@ {% endblock method_definition %} { {% block method_body %} - $answer['{{ entity|lower }}'] = $this->getOr404($id); + $answer{{ resource ? "['" ~ entity|lower ~ "']" }} = $this->getOr404($id); return $answer; {% endblock method_body %} {% block method_return '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index 0a50021..15a5878 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -1,4 +1,4 @@ -/** + /** {% block phpdoc_method_header %} * Partial Update to a {{ entity }} entity. * @@ -31,7 +31,7 @@ {% endblock method_definition %} { {% block method_body %} - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); + $answer{{ resource ? "['" ~ entity|lower ~ "']" }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->patch($this->getOr404($id), $request->request->all()); return $answer; {% endblock method_body %} {% block method_return '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 4b639ae..5a73ec3 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -1,4 +1,4 @@ -/** + /** {% block phpdoc_method_header %} * Create a {{ entity }} entity. * @@ -21,9 +21,8 @@ * * @return Response {% endblock phpdoc_method_header %} -* {% block phpdoc_method_annotations %} - {% if 'annotation' == format %}{% endif %} +{% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} */ {% block method_definition %} @@ -33,7 +32,7 @@ {% block method_body %} try { $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); - $answer['{{ entity|lower }}'] = $new; + {{ resource ? "['" ~ entity|lower ~ "']" }} = $new; return $answer; } catch (InvalidFormException $exception) { diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index 14134e5..38e5743 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -1,4 +1,4 @@ -/** + /** {% block phpdoc_method_header %} * Update a {{ entity }} entity. * @@ -34,10 +34,10 @@ {% block method_body %} try { if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); + {{ resource ? "['" ~ entity|lower ~ "']" }}= $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); $code = Codes::HTTP_OK; } else { - $answer['{{ entity|lower }}'] = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); + $answer{{ resource ? "['" ~ entity|lower ~ "']" }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); $code = Codes::HTTP_CREATED; } } catch (InvalidFormException $exception) { From 5cd1c9e4eecbdac0d726310e4cabdc1ba8468cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 24 Nov 2014 16:20:19 -0400 Subject: [PATCH 13/16] Update the templates. --- .../Resources/skeleton/rest/actions/post.php.twig | 2 +- .../Resources/skeleton/rest/actions/put.php.twig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 5a73ec3..074d273 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -32,7 +32,7 @@ {% block method_body %} try { $new = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); - {{ resource ? "['" ~ entity|lower ~ "']" }} = $new; + $answer{{ resource ? "['" ~ entity|lower ~ "']" }} = $new; return $answer; } catch (InvalidFormException $exception) { diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index 38e5743..7019fe7 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -34,7 +34,7 @@ {% block method_body %} try { if (${{ entity|lower }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->get($id)) { - {{ resource ? "['" ~ entity|lower ~ "']" }}= $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); + $answer{{ resource ? "['" ~ entity|lower ~ "']" }}= $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->put(${{ entity|lower }}, $request->request->all()); $code = Codes::HTTP_OK; } else { $answer{{ resource ? "['" ~ entity|lower ~ "']" }} = $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->post($request->request->all()); From 45fddae19c9be3ad7c27a924cefc361abc260bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Fri, 28 Nov 2014 09:42:09 -0400 Subject: [PATCH 14/16] Allow getAll function on handler without parameters --- .../Resources/skeleton/rest/actions/post.php.twig | 2 +- .../Resources/skeleton/rest/handler/getAll.php.twig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig index 074d273..8cadcd4 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/post.php.twig @@ -24,7 +24,7 @@ {% block phpdoc_method_annotations %} {% if 'annotation' == format %}{% endif %} {% endblock phpdoc_method_annotations %} -*/ + */ {% block method_definition %} public function postAction(Request $request) {% endblock method_definition %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig index 74fa0b5..d8d5bc8 100644 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/getAll.php.twig @@ -1,5 +1,5 @@ {% block method_definition %} - public function getAll($filters, $order_by, $limit, $offset) + public function getAll($filters = array(), $order_by = null, $limit = null, $offset = null) {% endblock method_definition %} { {% block method_body %} From e99058a314691c12b3d82692ecbc82b540839282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 1 Dec 2014 12:58:45 -0400 Subject: [PATCH 15/16] Permit show delete message. --- .../Resources/skeleton/rest/actions/delete.php.twig | 2 +- .../Resources/skeleton/rest/handler/delete.php.twig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 37c4dce..05f3f83 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -35,7 +35,7 @@ try { return $this->container->get('{{ bundle|replace({'Bundle': ''})|lower}}.{{ entity|lower }}.handler')->delete(${{ entity|lower }}); } catch (\Exception $exception) { - return $exception; + throw new \RuntimeException("Exclusion not allowed"); } {% endblock method_body %} {% block method_return '' %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig index c5ead31..8824fc8 100644 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/handler/delete.php.twig @@ -9,7 +9,7 @@ return null; } catch (\Exception $e) { - throw new \RuntimeException("Exclusion not allowed"); + throw new \RuntimeException(); } {% endblock method_body %} } From 4e55ef365bf4a107a671441236ba882009323968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Berr=C3=AAdo?= Date: Mon, 22 Dec 2014 09:27:24 -0400 Subject: [PATCH 16/16] Fix PHPdocs messages --- .../Resources/skeleton/rest/actions/delete.php.twig | 3 +-- .../Resources/skeleton/rest/actions/getById.php.twig | 1 + .../Resources/skeleton/rest/actions/getOr404.php.twig | 9 ++++++++- .../Resources/skeleton/rest/actions/patch.php.twig | 2 +- .../Resources/skeleton/rest/actions/put.php.twig | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig index 05f3f83..b1ab62e 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/delete.php.twig @@ -17,8 +17,7 @@ * @View(statusCode=204) * * @param Request $request - * @param $entity - * @internal param $id + * @param $id * * @return Response {% endblock phpdoc_method_header %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig index 45b14f8..f6f1b42 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getById.php.twig @@ -17,6 +17,7 @@ * @View(serializerEnableMaxDepthChecks=true) * * @return Response + * @param $id {% endblock phpdoc_method_header %} {% block phpdoc_method_annotations %} {% if 'annotation' == format %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig index 35ded1b..ffbea13 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/getOr404.php.twig @@ -1,4 +1,11 @@ - + /** +{% block phpdoc_method_header %} + * Get a entity or throw a exception + * + * @param $id + * @return $entity +{% endblock phpdoc_method_header %} + */ {% block method_definition %} protected function getOr404($id) {% endblock method_definition %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig index 15a5878..4a7c498 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/patch.php.twig @@ -18,7 +18,7 @@ * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request - * @param $entity + * @param $id * * @return Response {% endblock phpdoc_method_header %} diff --git a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig index 7019fe7..eeb7447 100755 --- a/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig +++ b/src/Voryx/RESTGeneratorBundle/Resources/skeleton/rest/actions/put.php.twig @@ -19,7 +19,7 @@ * @View(serializerEnableMaxDepthChecks=true) * * @param Request $request - * @param $entity + * @param $id * * @return Response {% endblock phpdoc_method_header %}