Merge pull request #28 from stof/service_form_types

Added the support of form types defined as services
This commit is contained in:
William Durand 2012-06-21 00:19:49 -07:00
commit 5bcdbcb808
10 changed files with 115 additions and 9 deletions

View File

@ -120,7 +120,7 @@ abstract class AbstractFormatter implements FormatterInterface
$data['requirements'] = $requirements;
if (null !== $formType = $apiDoc->getFormType()) {
$data['parameters'] = $this->parser->parse(new $formType());
$data['parameters'] = $this->parser->parse($formType);
if ('PUT' === $method) {
// All parameters are optional with PUT (update)

View File

@ -11,7 +11,6 @@
namespace Nelmio\ApiDocBundle\Parser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;
@ -47,11 +46,14 @@ class FormTypeParser
* - required
* - description
*
* @param AbstractType $type
* @param string|\Symfony\Component\Form\FormTypeInterface $type
* @return array
*/
public function parse(AbstractType $type)
public function parse($type)
{
if (is_string($type) && class_exists($type)) {
$type = new $type();
}
$form = $this->formFactory->create($type);
$parameters = array();

View File

@ -95,7 +95,8 @@ The following properties are available:
* `filters`: an array of filters;
* `formType`: the Form Type associated to the method, useful for POST|PUT methods.
* `formType`: the Form Type associated to the method, useful for POST|PUT methods, either as FQCN or
as form type (if it is registered in the form factory in the container)
Each _filter_ has to define a `name` parameter, but other parameters are free. Filters are often optional
parameters, and you can document them as you want, but keep in mind to be consistent for the whole documentation.

View File

@ -22,7 +22,7 @@ class ApiDocExtractorTest extends WebTestCase
$data = $extractor->all();
$this->assertTrue(is_array($data));
$this->assertCount(8, $data);
$this->assertCount(9, $data);
foreach ($data as $d) {
$this->assertTrue(is_array($d));

View File

@ -71,4 +71,14 @@ class TestController
public function yetAnotherAction()
{
}
/**
* @ApiDoc(
* description="create another test",
* formType="dependency_type"
* )
*/
public function anotherPostAction()
{
}
}

View File

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class DependencyType extends AbstractType
{
public function __construct(array $mandatoryArgument)
{
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a', null, array('description' => 'A nice description'))
;
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
));
return;
}
public function getName()
{
return 'dependency_type';
}
}

View File

@ -22,3 +22,9 @@ twig:
services:
nemlio.test.controller:
class: Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestServiceController
nelmio.test.type:
class: Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType
arguments:
- [foo, bar]
tags:
- { name: form.type, alias: dependency_type }

View File

@ -28,6 +28,12 @@ test_route_6:
requirements:
id: \d+
test_route_7:
pattern: /another-post
defaults: { _controller: NelmioApiDocTestBundle:Test:anotherPost, _format: json }
requirements:
_method: POST
test_service_route_1:
pattern: /tests
defaults: { _controller: nemlio.test.controller:indexAction, _format: json }

View File

@ -107,6 +107,19 @@ b:
# others #
### `POST` /another-post ###
_create another test_
#### Parameters ####
a:
* type: string
* required: true
* description: A nice description
### `ANY` /any ###
_Action without HTTP verb_

View File

@ -140,6 +140,24 @@ class SimpleFormatterTest extends WebTestCase
'others' =>
array(
0 =>
array(
'method' => 'POST',
'uri' => '/another-post',
'requirements' =>
array(
),
'parameters' =>
array(
'a' =>
array(
'dataType' => 'string',
'required' => true,
'description' => 'A nice description',
),
),
'description' => 'create another test',
),
1 =>
array(
'method' => 'ANY',
'uri' => '/any',
@ -148,7 +166,7 @@ class SimpleFormatterTest extends WebTestCase
),
'description' => 'Action without HTTP verb',
),
1 =>
2 =>
array(
'method' => 'ANY',
'uri' => '/any/{foo}',
@ -158,7 +176,7 @@ class SimpleFormatterTest extends WebTestCase
),
'description' => 'Action without HTTP verb',
),
2 =>
3 =>
array(
'method' => 'ANY',
'uri' => '/my-commented/{id}/{page}',
@ -169,7 +187,7 @@ class SimpleFormatterTest extends WebTestCase
),
'description' => 'This method is useful to test if the getDocComment works. And, it supports multilines until the first \'@\' char.',
),
3 =>
4 =>
array(
'method' => 'ANY',
'uri' => '/yet-another/{id}',