From 52df5ebf476768fd0ecc2bbac1a2d61cbcc625b0 Mon Sep 17 00:00:00 2001 From: Vyacheslav Slinko Date: Wed, 17 Oct 2012 15:27:20 +0400 Subject: [PATCH] Allow to configure default request format --- DependencyInjection/Configuration.php | 15 ++++++++--- DependencyInjection/NelmioApiDocExtension.php | 3 ++- Formatter/HtmlFormatter.php | 27 +++++++++++++------ Resources/config/formatters.xml | 5 +++- Resources/views/layout.html.twig | 4 +-- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index b9a2bab..21a73c8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -28,9 +28,18 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('enabled')->defaultTrue()->end() ->scalarNode('endpoint')->defaultValue('/app_dev.php')->end() - ->enumNode('request_format_method') - ->values(array('format_param', 'accept_header')) - ->defaultValue('format_param') + ->arrayNode('request_format') + ->addDefaultsIfNotSet() + ->children() + ->enumNode('method') + ->values(array('format_param', 'accept_header')) + ->defaultValue('format_param') + ->end() + ->enumNode('default_format') + ->values(array('json', 'xml')) + ->defaultValue('json') + ->end() + ->end() ->end() ->arrayNode('authentication') ->children() diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 501a00e..db4867d 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -31,7 +31,8 @@ class NelmioApiDocExtension extends Extension $container->setParameter('nelmio_api_doc.api_name', $config['name']); $container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']); $container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']); - $container->setParameter('nelmio_api_doc.sandbox.request_format_method', $config['sandbox']['request_format_method']); + $container->setParameter('nelmio_api_doc.sandbox.request_format.method', $config['sandbox']['request_format']['method']); + $container->setParameter('nelmio_api_doc.sandbox.request_format.default_format', $config['sandbox']['request_format']['default_format']); $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('formatters.xml'); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 06dde5a..2981ca1 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -45,6 +45,11 @@ class HtmlFormatter extends AbstractFormatter */ private $requestFormatMethod; + /** + * @var string + */ + private $defaultRequestFormat; + /** * @param array $authentication */ @@ -93,6 +98,11 @@ class HtmlFormatter extends AbstractFormatter $this->requestFormatMethod = $method; } + public function setDefaultRequestFormat($format) + { + $this->defaultRequestFormat = $format; + } + /** * {@inheritdoc} */ @@ -146,14 +156,15 @@ class HtmlFormatter extends AbstractFormatter private function getGlobalVars() { return array( - 'apiName' => $this->apiName, - 'authentication' => $this->authentication, - 'endpoint' => $this->endpoint, - 'enableSandbox' => $this->enableSandbox, - 'requestFormatMethod' => $this->requestFormatMethod, - 'date' => date(DATE_RFC822), - 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), - 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), + 'apiName' => $this->apiName, + 'authentication' => $this->authentication, + 'endpoint' => $this->endpoint, + 'enableSandbox' => $this->enableSandbox, + 'requestFormatMethod' => $this->requestFormatMethod, + 'defaultRequestFormat' => $this->defaultRequestFormat, + 'date' => date(DATE_RFC822), + 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), + 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), ); } diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index 888268b..6a8ae76 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -38,7 +38,10 @@ %nelmio_api_doc.sandbox.endpoint% - %nelmio_api_doc.sandbox.request_format_method% + %nelmio_api_doc.sandbox.request_format.method% + + + %nelmio_api_doc.sandbox.request_format.default_format% %nelmio_api_doc.sandbox.authentication% diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index ff773fa..f314231 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -18,8 +18,8 @@
request format: {% if authentication %} api key: