From aa9504a80c8ff12adbf97fe099ab5e5a8e3b761b Mon Sep 17 00:00:00 2001 From: William DURAND Date: Thu, 12 Apr 2012 01:28:36 +0200 Subject: [PATCH] Refactored formatters --- EventListener/RequestListener.php | 4 +- ...DocFormatter.php => AbstractFormatter.php} | 9 +++- Formatter/FormatterInterface.php | 11 ++++ Formatter/MarkdownFormatter.php | 54 +++++++++++++++++++ Formatter/SimpleFormatter.php | 11 ++++ Resources/config/request_listener.xml | 2 +- Resources/config/services.xml | 10 +++- 7 files changed, 95 insertions(+), 6 deletions(-) rename Formatter/{ApiDocFormatter.php => AbstractFormatter.php} (84%) create mode 100644 Formatter/FormatterInterface.php create mode 100644 Formatter/MarkdownFormatter.php create mode 100644 Formatter/SimpleFormatter.php diff --git a/EventListener/RequestListener.php b/EventListener/RequestListener.php index fa73d12..1812236 100644 --- a/EventListener/RequestListener.php +++ b/EventListener/RequestListener.php @@ -3,7 +3,7 @@ namespace Nelmio\ApiBundle\EventListener; use Doctrine\Common\Annotations\Reader; -use Nelmio\ApiBundle\Formatter\ApiDocFormatter; +use Nelmio\ApiBundle\Formatter\FormatterInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -20,7 +20,7 @@ class RequestListener protected $formatter; - public function __construct(Reader $reader, RouterInterface $router, ApiDocFormatter $formatter) + public function __construct(Reader $reader, RouterInterface $router, FormatterInterface $formatter) { $this->reader = $reader; $this->router = $router; diff --git a/Formatter/ApiDocFormatter.php b/Formatter/AbstractFormatter.php similarity index 84% rename from Formatter/ApiDocFormatter.php rename to Formatter/AbstractFormatter.php index d44fbe5..32609fa 100644 --- a/Formatter/ApiDocFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -6,7 +6,7 @@ use Nelmio\ApiBundle\Annotation\ApiDoc; use Nelmio\ApiBundle\Parser\FormTypeParser; use Symfony\Component\Routing\Route; -class ApiDocFormatter +abstract class AbstractFormatter implements FormatterInterface { /** * @var \Nelmio\ApiBundle\Parser\FormTypeParser @@ -19,6 +19,13 @@ class ApiDocFormatter } public function format(ApiDoc $apiDoc, Route $route) + { + return $this->render($this->getData($apiDoc, $route)); + } + + protected abstract function render(array $data); + + private function getData(ApiDoc $apiDoc, Route $route) { $method = $route->getRequirement('_method'); $data = array( diff --git a/Formatter/FormatterInterface.php b/Formatter/FormatterInterface.php new file mode 100644 index 0000000..ded81c0 --- /dev/null +++ b/Formatter/FormatterInterface.php @@ -0,0 +1,11 @@ + $value) { + $markdown .= sprintf("* %s: %s\n", $name, $value); + } + + $markdown .= "\n"; + } + + if (isset($data['filters'])) { + $markdown .= "#### Filters ####\n\n"; + + foreach ($data['filters'] as $name => $filter) { + $markdown .= sprintf("%s:\n\n", $name); + + foreach ($filter as $key => $value) { + $markdown .= sprintf(" * %s: %s\n", $key, $value); + } + + $markdown .= "\n"; + } + } + + if (isset($data['parameters'])) { + $markdown .= "#### Parameters ####\n\n"; + + foreach ($data['parameters'] as $parameter) { + $markdown .= sprintf("%s:\n\n", $parameter['name']); + $markdown .= sprintf(" * type: %s\n", $parameter['type']); + $markdown .= sprintf(" * is_required: %s\n", $parameter['is_required'] ? 'true' : 'false'); + $markdown .= "\n"; + } + } + + return $markdown; + } +} diff --git a/Formatter/SimpleFormatter.php b/Formatter/SimpleFormatter.php new file mode 100644 index 0000000..b9738f1 --- /dev/null +++ b/Formatter/SimpleFormatter.php @@ -0,0 +1,11 @@ + - + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 370b52e..51d2351 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -5,16 +5,22 @@ Nelmio\ApiBundle\Parser\FormTypeParser - Nelmio\ApiBundle\Formatter\ApiDocFormatter + Nelmio\ApiBundle\Formatter\AbstractFormatter + Nelmio\ApiBundle\Formatter\MarkdownFormatter + Nelmio\ApiBundle\Formatter\SimpleFormatter - + + +