Added a config parameter to set the API name in the HTML documentation

This commit is contained in:
William DURAND 2012-04-12 20:34:19 +02:00
parent d2285787a5
commit d7f6d665f6
6 changed files with 59 additions and 4 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace Nelmio\ApiBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder
->root('nelmio_api_doc')
->children()
->scalarNode('name')->defaultValue('API documentation')->end()
;
return $treeBuilder;
}
}

View File

@ -5,6 +5,7 @@ namespace Nelmio\ApiDocBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
class NelmioApiExtension extends Extension class NelmioApiExtension extends Extension
@ -14,6 +15,12 @@ class NelmioApiExtension extends Extension
*/ */
public function load(array $configs, ContainerBuilder $container) public function load(array $configs, ContainerBuilder $container)
{ {
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
$container->setParameter('nelmio_api_doc.api_name', $config['name']);
$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');
$loader->load('request_listener.xml'); $loader->load('request_listener.xml');

View File

@ -7,6 +7,19 @@ use Symfony\Component\Routing\Route;
class HtmlFormatter extends AbstractFormatter class HtmlFormatter extends AbstractFormatter
{ {
/**
* @var string
*/
private $apiName;
/**
* @param string $apiName
*/
public function setApiName($apiName)
{
$this->apiName = $apiName;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -69,7 +82,7 @@ class HtmlFormatter extends AbstractFormatter
private function renderWithLayout($content) private function renderWithLayout($content)
{ {
extract(array('content' => $content)); extract(array('api_name' => $this->apiName, 'content' => $content));
ob_start(); ob_start();
include __DIR__ . '/../Resources/views/formatter_layout.html.php'; include __DIR__ . '/../Resources/views/formatter_layout.html.php';

View File

@ -126,6 +126,15 @@ A use case could be to generate a static version of your documentation:
php app/console api:doc:dump --format=html > api.html php app/console api:doc:dump --format=html > api.html
## Configuration ##
You can specify your own API name:
# app/config/config.yml
nelmio_api_doc:
name: My API
## Credits ## ## Credits ##
The design is heavily inspired by the [swagger-ui](https://github.com/wordnik/swagger-ui) project. The design is heavily inspired by the [swagger-ui](https://github.com/wordnik/swagger-ui) project.

View File

@ -23,7 +23,11 @@
<service id="nelmio_api_doc.formatter.simple_formatter" class="%nelmio_api_doc.formatter.simple_formatter.class%" <service id="nelmio_api_doc.formatter.simple_formatter" class="%nelmio_api_doc.formatter.simple_formatter.class%"
parent="nelmio_api_doc.formatter.abstract_formatter" /> parent="nelmio_api_doc.formatter.abstract_formatter" />
<service id="nelmio_api_doc.formatter.html_formatter" class="%nelmio_api_doc.formatter.html_formatter.class%" <service id="nelmio_api_doc.formatter.html_formatter" class="%nelmio_api_doc.formatter.html_formatter.class%"
parent="nelmio_api_doc.formatter.abstract_formatter" /> parent="nelmio_api_doc.formatter.abstract_formatter">
<call method="setApiName">
<argument>%nelmio_api_doc.api_name%</argument>
</call>
</service>
</services> </services>
</container> </container>

View File

@ -4,7 +4,7 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<!-- Always force latest IE rendering engine (even in intranet) and Chrome Frame --> <!-- Always force latest IE rendering engine (even in intranet) and Chrome Frame -->
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" /> <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
<title>API documentation</title> <title><?php echo $api_name; ?></title>
<link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" type="text/css" /> <link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" type="text/css" />
<style type="text/css"> <style type="text/css">
<?php echo file_get_contents(__DIR__ . '/../public/css/screen.css'); ?> <?php echo file_get_contents(__DIR__ . '/../public/css/screen.css'); ?>
@ -13,7 +13,7 @@
</head> </head>
<body> <body>
<div id="header"> <div id="header">
<h1>API documentation</h1> <h1><?php echo $api_name; ?></h1>
</div> </div>
<div class="container" id="resources_container"> <div class="container" id="resources_container">
<ul id="resources"> <ul id="resources">