From 8e03ef99fa6d966cc5624ed52be64bec88435853 Mon Sep 17 00:00:00 2001 From: Vyacheslav Slinko Date: Tue, 16 Oct 2012 23:41:16 +0400 Subject: [PATCH 1/4] Make JSON declaration method configurable --- DependencyInjection/Configuration.php | 7 +++++ DependencyInjection/NelmioApiDocExtension.php | 1 + Formatter/HtmlFormatter.php | 28 ++++++++++++++----- Resources/config/formatters.xml | 3 ++ Resources/views/layout.html.twig | 11 ++++++-- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7a03f78..721c962 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -28,6 +28,13 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('enabled')->defaultTrue()->end() ->scalarNode('endpoint')->defaultValue('/app_dev.php')->end() + ->scalarNode('json_declaration_method') + ->defaultValue('format_param') + ->validate() + ->ifNotInArray(array('format_param', 'accept_header')) + ->thenInvalid("Unknown json declaration method '%s'.") + ->end() + ->end() ->arrayNode('authentication') ->children() ->scalarNode('name')->isRequired()->end() diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 321a2cc..1852fcf 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -31,6 +31,7 @@ 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.json_declaration_method', $config['sandbox']['json_declaration_method']); $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('formatters.xml'); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 0e570c7..5c1dd30 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -40,6 +40,11 @@ class HtmlFormatter extends AbstractFormatter */ private $engine; + /** + * @var string + */ + private $jsonDeclarationMethod; + /** * @param array $authentication */ @@ -80,6 +85,14 @@ class HtmlFormatter extends AbstractFormatter $this->engine = $engine; } + /** + * @param string $method + */ + public function setJsonDeclarationMethod($method) + { + $this->jsonDeclarationMethod = $method; + } + /** * {@inheritdoc} */ @@ -133,13 +146,14 @@ class HtmlFormatter extends AbstractFormatter private function getGlobalVars() { return array( - 'apiName' => $this->apiName, - 'authentication' => $this->authentication, - 'endpoint' => $this->endpoint, - 'enableSandbox' => $this->enableSandbox, - '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, + 'jsonDeclarationMethod' => $this->jsonDeclarationMethod, + '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 bc1dcbf..60373b5 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -37,6 +37,9 @@ %nelmio_api_doc.sandbox.endpoint% + + %nelmio_api_doc.sandbox.json_declaration_method% + %nelmio_api_doc.sandbox.authentication% diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 306befb..95cd134 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -134,13 +134,18 @@ var url = $(this).attr('action'), method = $(this).attr('method'), self = this, - params = { - _format: 'json' // default format if not overriden in the form - }, + params = {}, headers = {}, content = $(this).find('textarea.content').val(); result_container = $('.result', $(this).parent()); + var jsonDeclarationMethod = '{{ jsonDeclarationMethod }}'; + if (jsonDeclarationMethod == 'format_param') { + params['_format'] = 'json'; + } else if (jsonDeclarationMethod == 'accept_header') { + headers['Accept'] = 'application/json'; + } + // retrieve all the parameters to send $('.parameters .tuple', $(this)).each(function() { var key, value; From f25c2f1eeb4b1e691180b1026c30f2c8a9e8d520 Mon Sep 17 00:00:00 2001 From: Vyacheslav Slinko Date: Wed, 17 Oct 2012 05:12:47 +0400 Subject: [PATCH 2/4] Simplify enum nodes --- DependencyInjection/Configuration.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 721c962..c079a5e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -28,23 +28,17 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('enabled')->defaultTrue()->end() ->scalarNode('endpoint')->defaultValue('/app_dev.php')->end() - ->scalarNode('json_declaration_method') + ->enumNode('json_declaration_method') + ->values(array('format_param', 'accept_header')) ->defaultValue('format_param') - ->validate() - ->ifNotInArray(array('format_param', 'accept_header')) - ->thenInvalid("Unknown json declaration method '%s'.") - ->end() ->end() ->arrayNode('authentication') ->children() ->scalarNode('name')->isRequired()->end() - ->scalarNode('delivery') + ->enumNode('delivery') + // header|query|request, but only query is implemented for now + ->values(array('query')) ->isRequired() - ->validate() - // header|query|request, but only query is implemented for now - ->ifNotInArray(array('query')) - ->thenInvalid("Unknown authentication delivery type '%s'.") - ->end() ->end() ->end() ->end() From 3540bcfcd8dd52a74b894e90a48429c9fd6179eb Mon Sep 17 00:00:00 2001 From: Vyacheslav Slinko Date: Wed, 17 Oct 2012 15:15:35 +0400 Subject: [PATCH 3/4] Make response format more configurable --- DependencyInjection/Configuration.php | 11 +- DependencyInjection/NelmioApiDocExtension.php | 2 +- Formatter/HtmlFormatter.php | 22 ++-- Resources/config/formatters.xml | 4 +- Resources/public/css/screen.css | 6 +- Resources/views/layout.html.twig | 124 +++++++++--------- 6 files changed, 87 insertions(+), 82 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c079a5e..b9a2bab 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -28,17 +28,20 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('enabled')->defaultTrue()->end() ->scalarNode('endpoint')->defaultValue('/app_dev.php')->end() - ->enumNode('json_declaration_method') + ->enumNode('request_format_method') ->values(array('format_param', 'accept_header')) ->defaultValue('format_param') ->end() ->arrayNode('authentication') ->children() ->scalarNode('name')->isRequired()->end() - ->enumNode('delivery') - // header|query|request, but only query is implemented for now - ->values(array('query')) + ->scalarNode('delivery') ->isRequired() + ->validate() + // header|query|request, but only query is implemented for now + ->ifNotInArray(array('query')) + ->thenInvalid("Unknown authentication delivery type '%s'.") + ->end() ->end() ->end() ->end() diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 1852fcf..501a00e 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -31,7 +31,7 @@ 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.json_declaration_method', $config['sandbox']['json_declaration_method']); + $container->setParameter('nelmio_api_doc.sandbox.request_format_method', $config['sandbox']['request_format_method']); $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('formatters.xml'); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 5c1dd30..06dde5a 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -43,7 +43,7 @@ class HtmlFormatter extends AbstractFormatter /** * @var string */ - private $jsonDeclarationMethod; + private $requestFormatMethod; /** * @param array $authentication @@ -88,9 +88,9 @@ class HtmlFormatter extends AbstractFormatter /** * @param string $method */ - public function setJsonDeclarationMethod($method) + public function setRequestFormatMethod($method) { - $this->jsonDeclarationMethod = $method; + $this->requestFormatMethod = $method; } /** @@ -146,14 +146,14 @@ class HtmlFormatter extends AbstractFormatter private function getGlobalVars() { return array( - 'apiName' => $this->apiName, - 'authentication' => $this->authentication, - 'endpoint' => $this->endpoint, - 'enableSandbox' => $this->enableSandbox, - 'jsonDeclarationMethod' => $this->jsonDeclarationMethod, - '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, + '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 60373b5..888268b 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -37,8 +37,8 @@ %nelmio_api_doc.sandbox.endpoint% - - %nelmio_api_doc.sandbox.json_declaration_method% + + %nelmio_api_doc.sandbox.request_format_method% %nelmio_api_doc.sandbox.authentication% diff --git a/Resources/public/css/screen.css b/Resources/public/css/screen.css index 4b73b61..d0960bc 100644 --- a/Resources/public/css/screen.css +++ b/Resources/public/css/screen.css @@ -148,9 +148,9 @@ table tbody tr:last-child td { text-decoration: none; } -#api_key_wrapper { - float: right; - padding: 10px 0 10px 0; +#sandbox_configuration { + float: right; + padding: 10px 0 10px 0; } #colophon { diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 95cd134..ff773fa 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -15,11 +15,16 @@