Allow to disable the sandbox, refactor the configuration. Fix #37

This commit is contained in:
William DURAND 2012-07-18 13:18:26 +02:00
parent 208d25f825
commit bee518f92e
7 changed files with 427 additions and 388 deletions

View File

@ -23,7 +23,13 @@ class Configuration implements ConfigurationInterface
->root('nelmio_api_doc') ->root('nelmio_api_doc')
->children() ->children()
->scalarNode('name')->defaultValue('API documentation')->end() ->scalarNode('name')->defaultValue('API documentation')->end()
->scalarNode('sandbox_target')->defaultValue('/app_dev.php')->end() ->arrayNode('sandbox')
->addDefaultsIfNotSet()
->children()
->scalarNode('enabled')->defaultTrue()->end()
->scalarNode('endpoint')->defaultValue('/app_dev.php')->end()
->end()
->end()
->end(); ->end();
return $treeBuilder; return $treeBuilder;

View File

@ -29,7 +29,8 @@ class NelmioApiDocExtension extends Extension
$config = $processor->processConfiguration($configuration, $configs); $config = $processor->processConfiguration($configuration, $configs);
$container->setParameter('nelmio_api_doc.api_name', $config['name']); $container->setParameter('nelmio_api_doc.api_name', $config['name']);
$container->setParameter('nelmio_api_doc.api_sandbox_target', $config['sandbox_target']); $container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']);
$container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']);
$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

@ -23,7 +23,12 @@ class HtmlFormatter extends AbstractFormatter
/** /**
* @var string * @var string
*/ */
private $sandboxTarget; private $endpoint;
/**
* @var boolean
*/
private $enableSandbox;
/** /**
* @var \Symfony\Component\Templating\EngineInterface * @var \Symfony\Component\Templating\EngineInterface
@ -39,11 +44,19 @@ class HtmlFormatter extends AbstractFormatter
} }
/** /**
* @param string $sandboxTarget * @param string $endpoint
*/ */
public function setSandboxTarget($sandboxTarget) public function setEndpoint($endpoint)
{ {
$this->sandboxTarget = $sandboxTarget; $this->endpoint = $endpoint;
}
/**
* @param boolean $enableSandbox
*/
public function setEnableSandbox($enableSandbox)
{
$this->enableSandbox = $enableSandbox;
} }
/** /**
@ -83,7 +96,8 @@ class HtmlFormatter extends AbstractFormatter
{ {
return array( return array(
'apiName' => $this->apiName, 'apiName' => $this->apiName,
'sandboxTarget' => $this->sandboxTarget, 'endpoint' => $this->endpoint,
'enableSandbox' => $this->enableSandbox,
'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'),
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),

View File

@ -174,6 +174,15 @@ You can specify your own API name:
nelmio_api_doc: nelmio_api_doc:
name: My API name: My API
This bundle provides a sandbox mode in order to test API methods. You can
configure this sandbox using the following parameters:
# app/config/config.yml
nelmio_api_doc:
sandbox:
enabled: true # default: true, you can set this parameter to `false` to disable the sandbox
endpoint: http://sandbox.example.com/ # default: /app_dev.php, use this parameter to define which URL to call through the sandbox
## Credits ## ## Credits ##

View File

@ -30,8 +30,11 @@
<call method="setApiName"> <call method="setApiName">
<argument>%nelmio_api_doc.api_name%</argument> <argument>%nelmio_api_doc.api_name%</argument>
</call> </call>
<call method="setSandboxTarget"> <call method="setEnableSandbox">
<argument>%nelmio_api_doc.api_sandbox_target%</argument> <argument>%nelmio_api_doc.sandbox.enabled%</argument>
</call>
<call method="setEndpoint">
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
</call> </call>
</service> </service>
</services> </services>

View File

@ -29,6 +29,7 @@
$(this).next().slideToggle('slow'); $(this).next().slideToggle('slow');
}); });
{% if enableSandbox %}
var toggleButtonText = function ($btn) { var toggleButtonText = function ($btn) {
if ($btn.text() === 'Default') { if ($btn.text() === 'Default') {
$btn.text('Raw'); $btn.text('Raw');
@ -171,7 +172,7 @@
// and trigger the API call // and trigger the API call
$.ajax({ $.ajax({
url: '{{ sandboxTarget }}' + url, url: '{{ endpoint }}' + url,
type: method, type: method,
data: content.length ? content : params, data: content.length ? content : params,
headers: headers, headers: headers,
@ -256,6 +257,7 @@
$element.find('input.value').val(content_type); $element.find('input.value').val(content_type);
}); });
{% endif %}
</script> </script>
</body> </body>
</html> </html>

View File

@ -18,7 +18,9 @@
<div class="content" style="display: {% if displayContent is defined and displayContent == true %}display{% else %}none{% endif %};"> <div class="content" style="display: {% if displayContent is defined and displayContent == true %}display{% else %}none{% endif %};">
<ul class="tabs"> <ul class="tabs">
<li class="selected" data-pane="content">Documentation</li> <li class="selected" data-pane="content">Documentation</li>
{% if enableSandbox %}
<li data-pane="sandbox">Sandbox</li> <li data-pane="sandbox">Sandbox</li>
{% endif %}
</ul> </ul>
<div class="panes"> <div class="panes">
@ -106,6 +108,7 @@
{% endif %} {% endif %}
</div> </div>
{% if enableSandbox %}
<div class="pane sandbox"> <div class="pane sandbox">
<form method="{{ data.method|upper }}" action="{{ data.uri }}"> <form method="{{ data.method|upper }}" action="{{ data.uri }}">
<fieldset class="parameters"> <fieldset class="parameters">
@ -181,6 +184,7 @@
<pre class="response prettyprint"></pre> <pre class="response prettyprint"></pre>
</div> </div>
</div> </div>
{% endif %}
</div> </div>
</div> </div>
</li> </li>