Refactored formatters

This commit is contained in:
William DURAND 2012-04-12 01:28:36 +02:00
parent 0dc8883e09
commit aa9504a80c
7 changed files with 95 additions and 6 deletions

View File

@ -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;

View File

@ -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(

View File

@ -0,0 +1,11 @@
<?php
namespace Nelmio\ApiBundle\Formatter;
use Nelmio\ApiBundle\Annotation\ApiDoc;
use Symfony\Component\Routing\Route;
interface FormatterInterface
{
function format(ApiDoc $apiDoc, Route $route);
}

View File

@ -0,0 +1,54 @@
<?php
namespace Nelmio\ApiBundle\Formatter;
class MarkdownFormatter extends AbstractFormatter
{
protected function render(array $data)
{
$markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']);
if (isset($data['comment'])) {
$markdown .= sprintf("\n_%s_", $data['comment']);
}
$markdown .= "\n\n";
if (isset($data['requirements']) && !empty($data['requirements'])) {
$markdown .= "#### Requirements ####\n\n";
foreach ($data['requirements'] as $name => $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;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Nelmio\ApiBundle\Formatter;
class SimpleFormatter extends AbstractFormatter
{
protected function render(array $data)
{
return $data;
}
}

View File

@ -11,7 +11,7 @@
<service id="nelmio.api.event_listener.request" class="%nelmio.api.event_listener.request.class%">
<argument type="service" id="annotation_reader" />
<argument type="service" id="router" />
<argument type="service" id="nelmio.api.formatter.api_doc_formatter" />
<argument type="service" id="nelmio.api.formatter.simple_formatter" />
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
</service>
</services>

View File

@ -5,16 +5,22 @@
<parameters>
<parameter key="nelmio.api.parser.form_type_parser.class">Nelmio\ApiBundle\Parser\FormTypeParser</parameter>
<parameter key="nelmio.api.formatter.api_doc_formatter.class">Nelmio\ApiBundle\Formatter\ApiDocFormatter</parameter>
<parameter key="nelmio.api.formatter.abstract_formatter.class">Nelmio\ApiBundle\Formatter\AbstractFormatter</parameter>
<parameter key="nelmio.api.formatter.markdown_formatter.class">Nelmio\ApiBundle\Formatter\MarkdownFormatter</parameter>
<parameter key="nelmio.api.formatter.simple_formatter.class">Nelmio\ApiBundle\Formatter\SimpleFormatter</parameter>
</parameters>
<services>
<service id="nelmio.api.parser.form_type_parser" class="%nelmio.api.parser.form_type_parser.class%">
<argument type="service" id="form.factory" />
</service>
<service id="nelmio.api.formatter.api_doc_formatter" class="%nelmio.api.formatter.api_doc_formatter.class%">
<service id="nelmio.api.formatter.abstract_formatter" class="%nelmio.api.formatter.abstract_formatter.class%">
<argument type="service" id="nelmio.api.parser.form_type_parser" />
</service>
<service id="nelmio.api.formatter.markdown_formatter" class="%nelmio.api.formatter.markdown_formatter.class%"
parent="nelmio.api.formatter.abstract_formatter" />
<service id="nelmio.api.formatter.simple_formatter" class="%nelmio.api.formatter.simple_formatter.class%"
parent="nelmio.api.formatter.abstract_formatter" />
</services>
</container>