Merge pull request #98 from willdurand/fix

Fixes
This commit is contained in:
Jordi Boggiano 2012-11-17 09:26:40 -08:00
commit f1e28bb7bb
8 changed files with 110 additions and 81 deletions

View File

@ -285,7 +285,7 @@ class ApiDocExtractor
if ('_method' !== $name) { if ('_method' !== $name) {
$requirements[$name] = array( $requirements[$name] = array(
'requirement' => $value, 'requirement' => $value,
'type' => '', 'dataType' => '',
'description' => '', 'description' => '',
); );
} }
@ -303,7 +303,7 @@ class ApiDocExtractor
$found = false; $found = false;
foreach ($paramDocs as $paramDoc) { foreach ($paramDocs as $paramDoc) {
if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) { if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
$requirements[$var]['type'] = isset($matches[1]) ? $matches[1] : ''; $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
$requirements[$var]['description'] = $matches[2]; $requirements[$var]['description'] = $matches[2];
if (!isset($requirements[$var]['requirement'])) { if (!isset($requirements[$var]['requirement'])) {
@ -316,7 +316,7 @@ class ApiDocExtractor
} }
if (!isset($requirements[$var]) && false === $found) { if (!isset($requirements[$var]) && false === $found) {
$requirements[$var] = array('requirement' => '', 'type' => '', 'description' => ''); $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
} }
} }
@ -344,7 +344,7 @@ class ApiDocExtractor
if ($annot->strict) { if ($annot->strict) {
$annotation->addRequirement($annot->name, array( $annotation->addRequirement($annot->name, array(
'requirement' => $annot->requirements, 'requirement' => $annot->requirements,
'type' => '', 'dataType' => '',
'description' => $annot->description, 'description' => $annot->description,
)); ));
} else { } else {

View File

@ -20,7 +20,9 @@ abstract class AbstractFormatter implements FormatterInterface
*/ */
public function formatOne(ApiDoc $annotation) public function formatOne(ApiDoc $annotation)
{ {
return $this->renderOne($annotation->toArray()); return $this->renderOne(
$this->processAnnotation($annotation->toArray())
);
} }
/** /**
@ -28,12 +30,9 @@ abstract class AbstractFormatter implements FormatterInterface
*/ */
public function format(array $collection) public function format(array $collection)
{ {
$array = array(); return $this->render(
foreach ($collection as $coll) { $this->processCollection($collection)
$array[$coll['resource']][] = $coll['annotation']->toArray(); );
}
return $this->render($array);
} }
/** /**
@ -66,7 +65,6 @@ abstract class AbstractFormatter implements FormatterInterface
protected function compressNestedParameters(array $data, $parentName = null, $ignoreNestedReadOnly = false) protected function compressNestedParameters(array $data, $parentName = null, $ignoreNestedReadOnly = false)
{ {
$newParams = array(); $newParams = array();
foreach ($data as $name => $info) { foreach ($data as $name => $info) {
$newName = $this->getNewName($name, $info, $parentName); $newName = $this->getNewName($name, $info, $parentName);
@ -74,7 +72,7 @@ abstract class AbstractFormatter implements FormatterInterface
'description' => $info['description'], 'description' => $info['description'],
'dataType' => $info['dataType'], 'dataType' => $info['dataType'],
'readonly' => $info['readonly'], 'readonly' => $info['readonly'],
'required' => $info['required'] 'required' => $info['required'],
); );
if (isset($info['children']) && (!$info['readonly'] || !$ignoreNestedReadOnly)) { if (isset($info['children']) && (!$info['readonly'] || !$ignoreNestedReadOnly)) {
@ -99,10 +97,46 @@ abstract class AbstractFormatter implements FormatterInterface
protected function getNewName($name, $data, $parentName = null) protected function getNewName($name, $data, $parentName = null)
{ {
$newName = ($parentName) ? sprintf("%s[%s]", $parentName, $name) : $name; $newName = ($parentName) ? sprintf("%s[%s]", $parentName, $name) : $name;
$array = (false === strpos($data['dataType'], "array of")) ? "" : "[]"; $array = (false === strpos($data['dataType'], "array of")) ? "" : "[]";
return sprintf("%s%s", $newName, $array); return sprintf("%s%s", $newName, $array);
} }
/**
* @param array $annotation
* @return array
*/
protected function processAnnotation($annotation)
{
if (isset($annotation['parameters'])) {
$annotation['parameters'] = $this->compressNestedParameters($annotation['parameters'], null, true);
}
if (isset($annotation['response'])) {
$annotation['response'] = $this->compressNestedParameters($annotation['response']);
}
return $annotation;
}
/**
* @param array[ApiDoc] $collection
* @return array
*/
protected function processCollection(array $collection)
{
$array = array();
foreach ($collection as $coll) {
$array[$coll['resource']][] = $coll['annotation']->toArray();
}
$processedCollection = array();
foreach ($array as $path => $annotations) {
foreach ($annotations as $annotation) {
$processedCollection[$path][] = $this->processAnnotation($annotation);
}
}
return $processedCollection;
}
} }

View File

@ -18,7 +18,7 @@ interface FormatterInterface
/** /**
* Format a collection of documentation data. * Format a collection of documentation data.
* *
* @param array $collection * @param array[ApiDoc] $collection
* @return string|array * @return string|array
*/ */
public function format(array $collection); public function format(array $collection);

View File

@ -16,39 +16,39 @@ use Symfony\Component\Templating\EngineInterface;
class HtmlFormatter extends AbstractFormatter class HtmlFormatter extends AbstractFormatter
{ {
/** /**
* @var array * @var string
*/ */
private $authentication; protected $apiName;
/** /**
* @var string * @var string
*/ */
private $apiName; protected $endpoint;
/** /**
* @var string * @var string
*/ */
private $endpoint; protected $defaultRequestFormat;
/**
* @var EngineInterface
*/
protected $engine;
/** /**
* @var boolean * @var boolean
*/ */
private $enableSandbox; private $enableSandbox;
/**
* @var \Symfony\Component\Templating\EngineInterface
*/
private $engine;
/** /**
* @var string * @var string
*/ */
private $requestFormatMethod; private $requestFormatMethod;
/** /**
* @var string * @var array
*/ */
private $defaultRequestFormat; private $authentication;
/** /**
* @param array $authentication * @param array $authentication
@ -98,6 +98,9 @@ class HtmlFormatter extends AbstractFormatter
$this->requestFormatMethod = $method; $this->requestFormatMethod = $method;
} }
/**
* @param string $format
*/
public function setDefaultRequestFormat($format) public function setDefaultRequestFormat($format)
{ {
$this->defaultRequestFormat = $format; $this->defaultRequestFormat = $format;
@ -108,16 +111,11 @@ class HtmlFormatter extends AbstractFormatter
*/ */
protected function renderOne(array $data) protected function renderOne(array $data)
{ {
if (isset($data['parameters'])) {
$data['parameters'] = $this->compressNestedParameters($data['parameters'], null, true);
}
if (isset($data['response'])) {
$data['response'] = $this->compressNestedParameters($data['response']);
}
return $this->engine->render('NelmioApiDocBundle::resource.html.twig', array_merge( return $this->engine->render('NelmioApiDocBundle::resource.html.twig', array_merge(
array('data' => $data, 'displayContent' => true), array(
'data' => $data,
'displayContent' => true,
),
$this->getGlobalVars() $this->getGlobalVars()
)); ));
} }
@ -127,25 +125,10 @@ class HtmlFormatter extends AbstractFormatter
*/ */
protected function render(array $collection) protected function render(array $collection)
{ {
$processedCollection = array();
foreach ($collection as $path => $methods) {
$processedCollection[$path] = array();
foreach ($methods as $method) {
if (isset($method['parameters'])) {
$method['parameters'] = $this->compressNestedParameters($method['parameters'], null, true);
}
if (isset($method['response'])) {
$method['response'] = $this->compressNestedParameters($method['response']);
}
$processedCollection[$path][] = $method;
}
}
return $this->engine->render('NelmioApiDocBundle::resources.html.twig', array_merge( return $this->engine->render('NelmioApiDocBundle::resources.html.twig', array_merge(
array('resources' => $processedCollection), array(
'resources' => $collection,
),
$this->getGlobalVars() $this->getGlobalVars()
)); ));
} }
@ -167,5 +150,4 @@ class HtmlFormatter extends AbstractFormatter
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
); );
} }
} }

View File

@ -18,14 +18,6 @@ class MarkdownFormatter extends AbstractFormatter
*/ */
protected function renderOne(array $data) protected function renderOne(array $data)
{ {
if (isset($data['parameters'])) {
$data['parameters'] = $this->compressNestedParameters($data['parameters'], null, true);
}
if (isset($data['response'])) {
$data['response'] = $this->compressNestedParameters($data['response']);
}
$markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']); $markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']);
if (isset($data['description'])) { if (isset($data['description'])) {
@ -51,8 +43,8 @@ class MarkdownFormatter extends AbstractFormatter
$markdown .= sprintf(" - Requirement: %s\n", $infos['requirement']); $markdown .= sprintf(" - Requirement: %s\n", $infos['requirement']);
} }
if (!empty($infos['type'])) { if (!empty($infos['dataType'])) {
$markdown .= sprintf(" - Type: %s\n", $infos['type']); $markdown .= sprintf(" - Type: %s\n", $infos['dataType']);
} }
if (!empty($infos['description'])) { if (!empty($infos['description'])) {

View File

@ -11,14 +11,36 @@
namespace Nelmio\ApiDocBundle\Formatter; namespace Nelmio\ApiDocBundle\Formatter;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
class SimpleFormatter extends AbstractFormatter class SimpleFormatter extends AbstractFormatter
{ {
/**
* {@inheritdoc}
*/
public function formatOne(ApiDoc $annotation)
{
return $annotation->toArray();
}
/**
* {@inheritdoc}
*/
public function format(array $collection)
{
$array = array();
foreach ($collection as $coll) {
$array[$coll['resource']][] = $coll['annotation']->toArray();
}
return $array;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function renderOne(array $data) protected function renderOne(array $data)
{ {
return $data;
} }
/** /**
@ -26,6 +48,5 @@ class SimpleFormatter extends AbstractFormatter
*/ */
protected function render(array $collection) protected function render(array $collection)
{ {
return $collection;
} }
} }

View File

@ -46,7 +46,7 @@
<tr> <tr>
<td>{{ name }}</td> <td>{{ name }}</td>
<td>{{ infos.requirement }}</td> <td>{{ infos.requirement }}</td>
<td>{{ infos.type }}</td> <td>{{ infos.dataType }}</td>
<td>{{ infos.description }}</td> <td>{{ infos.description }}</td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -48,7 +48,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'description' => 'index action', 'description' => 'index action',
'requirements' => array( 'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''), '_format' => array('dataType' => '', 'description' => '', 'requirement' => ''),
), ),
), ),
1 => 1 =>
@ -73,7 +73,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'description' => 'index action', 'description' => 'index action',
'requirements' => array( 'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''), '_format' => array('dataType' => '', 'description' => '', 'requirement' => ''),
), ),
), ),
2 => 2 =>
@ -106,7 +106,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'description' => 'create test', 'description' => 'create test',
'requirements' => array( 'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''), '_format' => array('dataType' => '', 'description' => '', 'requirement' => ''),
), ),
), ),
3 => 3 =>
@ -139,7 +139,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'description' => 'create test', 'description' => 'create test',
'requirements' => array( 'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''), '_format' => array('dataType' => '', 'description' => '', 'requirement' => ''),
), ),
), ),
), ),
@ -173,7 +173,7 @@ class SimpleFormatterTest extends WebTestCase
'uri' => '/any/{foo}', 'uri' => '/any/{foo}',
'requirements' => 'requirements' =>
array( array(
'foo' => array('type' => '', 'description' => '', 'requirement' => ''), 'foo' => array('dataType' => '', 'description' => '', 'requirement' => ''),
), ),
'description' => 'Action without HTTP verb', 'description' => 'Action without HTTP verb',
), ),
@ -290,8 +290,8 @@ With multiple lines.',
'uri' => '/my-commented/{id}/{page}', 'uri' => '/my-commented/{id}/{page}',
'requirements' => 'requirements' =>
array( array(
'id' => array('type' => 'int', 'description' => 'A nice comment', 'requirement' => ''), 'id' => array('dataType' => 'int', 'description' => 'A nice comment', 'requirement' => ''),
'page' => array('type' => 'int', 'description' => '', 'requirement' => ''), 'page' => array('dataType' => 'int', 'description' => '', 'requirement' => ''),
), ),
'description' => 'This method is useful to test if the getDocComment works.', 'description' => 'This method is useful to test if the getDocComment works.',
'documentation' => "This method is useful to test if the getDocComment works.\nAnd, it supports multilines until the first '@' char." 'documentation' => "This method is useful to test if the getDocComment works.\nAnd, it supports multilines until the first '@' char."
@ -302,7 +302,7 @@ With multiple lines.',
'uri' => '/yet-another/{id}', 'uri' => '/yet-another/{id}',
'requirements' => 'requirements' =>
array( array(
'id' => array('type' => '', 'description' => '', 'requirement' => '\d+') 'id' => array('dataType' => '', 'description' => '', 'requirement' => '\d+')
), ),
), ),
7 => 7 =>
@ -354,7 +354,7 @@ With multiple lines.',
), ),
'description' => 'index action', 'description' => 'index action',
'requirements' => array( 'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''), '_format' => array('dataType' => '', 'description' => '', 'requirement' => ''),
), ),
); );