Added request formats configuration

This commit is contained in:
Yosh de Vos 2014-05-17 22:45:30 +02:00
parent 968a162544
commit 8402c748ee
6 changed files with 41 additions and 12 deletions

View File

@ -57,14 +57,18 @@ class Configuration implements ConfigurationInterface
->arrayNode('request_format') ->arrayNode('request_format')
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()
->children() ->children()
->arrayNode('formats')
->defaultValue(array(
'json' => 'application/json',
'xml' => 'application/xml'
))
->prototype('scalar')->end()
->end()
->enumNode('method') ->enumNode('method')
->values(array('format_param', 'accept_header')) ->values(array('format_param', 'accept_header'))
->defaultValue('format_param') ->defaultValue('format_param')
->end() ->end()
->enumNode('default_format') ->scalarNode('default_format')->defaultValue('json')->end()
->values(array('json', 'xml'))
->defaultValue('json')
->end()
->end() ->end()
->end() ->end()
->arrayNode('authentication') ->arrayNode('authentication')

View File

@ -37,6 +37,7 @@ class NelmioApiDocExtension extends Extension
$container->setParameter('nelmio_api_doc.sandbox.accept_type', $config['sandbox']['accept_type']); $container->setParameter('nelmio_api_doc.sandbox.accept_type', $config['sandbox']['accept_type']);
$container->setParameter('nelmio_api_doc.sandbox.body_format', $config['sandbox']['body_format']); $container->setParameter('nelmio_api_doc.sandbox.body_format', $config['sandbox']['body_format']);
$container->setParameter('nelmio_api_doc.sandbox.request_format.default_format', $config['sandbox']['request_format']['default_format']); $container->setParameter('nelmio_api_doc.sandbox.request_format.default_format', $config['sandbox']['request_format']['default_format']);
$container->setParameter('nelmio_api_doc.sandbox.request_format.formats', $config['sandbox']['request_format']['formats']);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('formatters.xml'); $loader->load('formatters.xml');

View File

@ -40,6 +40,11 @@ class HtmlFormatter extends AbstractFormatter
*/ */
private $enableSandbox; private $enableSandbox;
/**
* @var array
*/
private $requestFormats;
/** /**
* @var string * @var string
*/ */
@ -129,6 +134,14 @@ class HtmlFormatter extends AbstractFormatter
$this->requestFormatMethod = $method; $this->requestFormatMethod = $method;
} }
/**
* @param array $formats
*/
public function setRequestFormats(array $formats)
{
$this->requestFormats = $formats;
}
/** /**
* @param string $format * @param string $format
*/ */
@ -193,6 +206,7 @@ class HtmlFormatter extends AbstractFormatter
'requestFormatMethod' => $this->requestFormatMethod, 'requestFormatMethod' => $this->requestFormatMethod,
'acceptType' => $this->acceptType, 'acceptType' => $this->acceptType,
'bodyFormat' => $this->bodyFormat, 'bodyFormat' => $this->bodyFormat,
'requestFormats' => $this->requestFormats,
'defaultRequestFormat' => $this->defaultRequestFormat, 'defaultRequestFormat' => $this->defaultRequestFormat,
'date' => date(DATE_RFC822), 'date' => date(DATE_RFC822),
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),

View File

@ -37,6 +37,9 @@
<call method="setRequestFormatMethod"> <call method="setRequestFormatMethod">
<argument>%nelmio_api_doc.sandbox.request_format.method%</argument> <argument>%nelmio_api_doc.sandbox.request_format.method%</argument>
</call> </call>
<call method="setRequestFormats">
<argument>%nelmio_api_doc.sandbox.request_format.formats%</argument>
</call>
<call method="setDefaultRequestFormat"> <call method="setDefaultRequestFormat">
<argument>%nelmio_api_doc.sandbox.request_format.default_format%</argument> <argument>%nelmio_api_doc.sandbox.request_format.default_format%</argument>
</call> </call>

View File

@ -326,11 +326,15 @@ configure this sandbox using the following parameters:
# sandbox requests # sandbox requests
request_format: request_format:
formats: # default `json` and `xml`,
json: application/json # override to add custom formats or disable
xml: application/xml # default formats
method: format_param # default `format_param`, alternately `accept_header`, method: format_param # default `format_param`, alternately `accept_header`,
# decides how to request the response format # decides how to request the response format
default_format: json # default `json`, alternately `xml`, determines which default_format: json # default `json`,
# content format to request back by default # default content format to request (see formats)
### Command ### Command
@ -416,6 +420,9 @@ nelmio_api_doc:
accept_type: ~ accept_type: ~
body_format: form body_format: form
request_format: request_format:
formats:
json: application/json
xml: application/xml
method: format_param method: format_param
default_format: json default_format: json
authentication: authentication:

View File

@ -24,8 +24,9 @@
</select> </select>
request format: request format:
<select id="request_format"> <select id="request_format">
<option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option> {% for format, header in requestFormats %}
<option value="xml"{{ defaultRequestFormat == 'xml' ? ' selected' : '' }}>XML</option> <option value="{{ header }}"{{ defaultRequestFormat == format ? ' selected' : '' }}>{{ format }}</option>
{% endfor %}
</select> </select>
{% if authentication and authentication.delivery in ['query', 'http_basic', 'header'] %} {% if authentication and authentication.delivery in ['query', 'http_basic', 'header'] %}
api key: <input type="text" id="api_key" value=""/> api key: <input type="text" id="api_key" value=""/>
@ -197,13 +198,12 @@
method = method.split('|').sort().pop(); method = method.split('|').sort().pop();
} }
// set default requestFormat // set requestFormat
var requestFormat = $('#request_format').val();
var requestFormatMethod = '{{ requestFormatMethod }}'; var requestFormatMethod = '{{ requestFormatMethod }}';
if (requestFormatMethod == 'format_param') { if (requestFormatMethod == 'format_param') {
params['_format'] = requestFormat; params['_format'] = $('#request_format option:selected').text();
} else if (requestFormatMethod == 'accept_header') { } else if (requestFormatMethod == 'accept_header') {
headers['Accept'] = 'application/' + requestFormat; headers['Accept'] = $('#request_format').val();
} }
// set default bodyFormat // set default bodyFormat