From e6438d657fe494ae3a7989ec2dfaaac975933b87 Mon Sep 17 00:00:00 2001 From: kphoen Date: Thu, 24 May 2012 01:22:45 +0200 Subject: [PATCH] Add: sandbox, to easily try API methods --- DependencyInjection/Configuration.php | 3 +- DependencyInjection/NelmioApiDocExtension.php | 1 + Formatter/HtmlFormatter.php | 20 +- Resources/config/formatters.xml | 3 + Resources/public/css/screen.css | 57 +++++ Resources/views/layout.html.twig | 125 ++++++++++ Resources/views/method.html.twig | 223 +++++++++++------- 7 files changed, 349 insertions(+), 83 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e88d4e8..8c18d39 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -23,7 +23,8 @@ class Configuration implements ConfigurationInterface ->root('nelmio_api_doc') ->children() ->scalarNode('name')->defaultValue('API documentation')->end() - ; + ->scalarNode('sandbox_target')->defaultValue('/app_dev.php')->end() + ->end(); return $treeBuilder; } diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 63c4107..0aad910 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -29,6 +29,7 @@ class NelmioApiDocExtension extends Extension $config = $processor->processConfiguration($configuration, $configs); $container->setParameter('nelmio_api_doc.api_name', $config['name']); + $container->setParameter('nelmio_api_doc.api_sandbox_target', $config['sandbox_target']); $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('formatters.xml'); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 170004c..90480ee 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -20,6 +20,11 @@ class HtmlFormatter extends AbstractFormatter */ private $apiName; + /** + * @var string + */ + private $sandboxTarget; + /** * @var \Symfony\Component\Templating\EngineInterface */ @@ -33,6 +38,14 @@ class HtmlFormatter extends AbstractFormatter $this->apiName = $apiName; } + /** + * @param string $sandboxTarget + */ + public function setSandboxTarget($sandboxTarget) + { + $this->sandboxTarget = $sandboxTarget; + } + /** * @param EngineInterface $engine */ @@ -69,9 +82,10 @@ class HtmlFormatter extends AbstractFormatter private function getGlobalVars() { return array( - 'apiName' => $this->apiName, - 'date' => date(DATE_RFC822), - 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), + 'apiName' => $this->apiName, + 'sandboxTarget' => $this->sandboxTarget, + 'date' => date(DATE_RFC822), + 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), ); } } diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index 4e00a7a..1bf12c9 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -30,6 +30,9 @@ %nelmio_api_doc.api_name% + + %nelmio_api_doc.api_sandbox_target% + diff --git a/Resources/public/css/screen.css b/Resources/public/css/screen.css index 8fb3bf9..430cc84 100644 --- a/Resources/public/css/screen.css +++ b/Resources/public/css/screen.css @@ -1139,3 +1139,60 @@ body ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operatio #header a { text-decoration: none; } + + +/** sandbox */ +.pane:not(.selected) { + display: none; +} + +.result pre { + border: none; + padding: 10px; + font-size: 0.9em; + overflow: auto; +} + +.tabs li { + display: inline; + float: left; + margin: 10px; + cursor: pointer; +} + +.tabs li.selected { + font-weight: bold; +} + +.panes { + clear: both; + border-top: 1px solid #C3D9EC; +} + +.pane.sandbox { + border: 1px solid #C3D9EC; + border-top: none; + padding: 10px; +} + +.pane.sandbox legend { + margin-bottom: 5px; +} + +.remove { + cursor: pointer; +} + +.pane.sandbox .result { + display: none; +} + +.parameters { + float: left; + width: 50%; +} + +.buttons { + clear: both; + padding-top: 10px; +} diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 3ef872c..ff715e3 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -6,10 +6,12 @@ {{ apiName }} + +