Used Twig instead of plain PHP in the HtmlFormatter. Fixes #1

This commit is contained in:
William DURAND 2012-04-13 11:24:57 +02:00
parent a74520057a
commit a51d25b94f
12 changed files with 192 additions and 212 deletions

View File

@ -61,15 +61,6 @@ abstract class AbstractFormatter implements FormatterInterface
*/ */
protected abstract function renderOne(array $data); protected abstract function renderOne(array $data);
/**
* Format a set of data for a given resource.
*
* @param string $resource A resource name.
* @param array $arrayOfData A set of data.
* @return string|array
*/
protected abstract function renderResourceSection($resource, array $arrayOfData);
/** /**
* Format a set of resource sections. * Format a set of resource sections.
* *

View File

@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\Formatter;
use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Templating\EngineInterface;
class HtmlFormatter extends AbstractFormatter class HtmlFormatter extends AbstractFormatter
{ {
@ -21,6 +22,11 @@ class HtmlFormatter extends AbstractFormatter
*/ */
private $apiName; private $apiName;
/**
* @var \Symfony\Component\Templating\EngineInterface
*/
private $engine;
/** /**
* @param string $apiName * @param string $apiName
*/ */
@ -30,19 +36,11 @@ class HtmlFormatter extends AbstractFormatter
} }
/** /**
* {@inheritdoc} * @param EngineInterface $engine
*/ */
public function formatOne(ApiDoc $apiDoc, Route $route) public function setTemplatingEngine(EngineInterface $engine)
{ {
$data = $this->getData($apiDoc, $route); $this->engine = $engine;
$data['display_content'] = true;
extract(array('content' => $this->renderOne($data)));
ob_start();
include __DIR__ . '/../Resources/views/formatter_resource_section.html.php';
return $this->renderWithLayout(ob_get_clean());
} }
/** /**
@ -50,30 +48,10 @@ class HtmlFormatter extends AbstractFormatter
*/ */
protected function renderOne(array $data) protected function renderOne(array $data)
{ {
extract($data); return $this->engine->render('NelmioApiDocBundle::resource.html.twig', array_merge(
array('data' => $data, 'displayContent' => true),
ob_start(); $this->getGlobalVars()
include __DIR__ . '/../Resources/views/formatter.html.php'; ));
return ob_get_clean();
}
/**
* {@inheritdoc}
*/
protected function renderResourceSection($resource, array $arrayOfData)
{
$content = '';
foreach ($arrayOfData as $data) {
$content .= $this->renderOne($data);
}
extract(array('content' => $content));
ob_start();
include __DIR__ . '/../Resources/views/formatter_resource_section.html.php';
return ob_get_clean();
} }
/** /**
@ -81,21 +59,21 @@ class HtmlFormatter extends AbstractFormatter
*/ */
protected function render(array $collection) protected function render(array $collection)
{ {
$content = ''; return $this->engine->render('NelmioApiDocBundle::resources.html.twig', array_merge(
foreach ($collection as $resource => $arrayOfData) { array('resources' => $collection),
$content .= $this->renderResourceSection($resource, $arrayOfData); $this->getGlobalVars()
));
} }
return $this->renderWithLayout($content); /**
} * @return array
*/
private function renderWithLayout($content) private function getGlobalVars()
{ {
extract(array('api_name' => $this->apiName, 'content' => $content)); return array(
'apiName' => $this->apiName,
ob_start(); 'date' => date(DATE_RFC822),
include __DIR__ . '/../Resources/views/formatter_layout.html.php'; 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
);
return ob_get_clean();
} }
} }

View File

@ -64,21 +64,6 @@ class MarkdownFormatter extends AbstractFormatter
return $markdown; return $markdown;
} }
/**
* {@inheritdoc}
*/
protected function renderResourceSection($resource, array $arrayOfData)
{
$markdown = sprintf("# %s #\n\n", $resource);
foreach ($arrayOfData as $data) {
$markdown .= $this->renderOne($data);
$markdown .= "\n";
}
return $markdown;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -92,4 +77,16 @@ class MarkdownFormatter extends AbstractFormatter
return $markdown; return $markdown;
} }
private function renderResourceSection($resource, array $arrayOfData)
{
$markdown = sprintf("# %s #\n\n", $resource);
foreach ($arrayOfData as $data) {
$markdown .= $this->renderOne($data);
$markdown .= "\n";
}
return $markdown;
}
} }

View File

@ -21,14 +21,6 @@ class SimpleFormatter extends AbstractFormatter
return $data; return $data;
} }
/**
* {@inheritdoc}
*/
protected function renderResourceSection($resource, array $arrayOfData)
{
return array($resource => $arrayOfData);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -24,6 +24,9 @@
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="setTemplatingEngine">
<argument type="service" id="templating" />
</call>
<call method="setApiName"> <call method="setApiName">
<argument>%nelmio_api_doc.api_name%</argument> <argument>%nelmio_api_doc.api_name%</argument>
</call> </call>

View File

@ -1,86 +0,0 @@
<li class="<?php echo strtolower($method); ?> operation">
<div class="heading toggler">
<h3>
<span class="http_method">
<a><?php echo $method; ?></a>
</span>
<span class="path">
<?php echo $uri; ?>
</span>
</h3>
<ul class="options">
<?php if (isset($description)) : ?>
<li><?php echo $description; ?></li>
<?php endif; ?>
</ul>
</div>
<div class="content" style="display: none;">
<?php if (isset($requirements) && !empty($requirements)) : ?>
<h4>Requirements</h4>
<table class="fullwidth">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<?php foreach ($requirements as $key => $value) : ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php if (isset($filters)) : ?>
<h4>Filters</h4>
<table class="fullwidth">
<thead>
<tr>
<th>Name</th>
<th>Information</th>
</tr>
</thead>
<tbody>
<?php foreach ($filters as $name => $info) : ?>
<tr>
<td><?php echo $name; ?></td>
<td>
<ul>
<?php foreach ($info as $key => $value) : ?>
<li><em><?php echo $key; ?></em> : <?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php if (isset($parameters)) : ?>
<h4>Parameters</h4>
<table class='fullwidth'>
<thead>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Required?</th>
</tr>
</thead>
<tbody>
<?php foreach ($parameters as $name => $info) : ?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $info['dataType']; ?></td>
<td><?php echo $info['required'] ? 'true' : 'false'; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</li>

View File

@ -1,32 +0,0 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine (even in intranet) and Chrome Frame -->
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
<title><?php echo $api_name; ?></title>
<link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" type="text/css" />
<style type="text/css">
<?php echo file_get_contents(__DIR__ . '/../public/css/screen.css'); ?>
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="header">
<h1><?php echo $api_name; ?></h1>
</div>
<div class="container" id="resources_container">
<ul id="resources">
<?php echo $content; ?>
</ul>
</div>
<p id="colophon">
Documentation auto-generated on <?php echo date(DATE_RFC822); ?>
</p>
<script type="text/javascript">
$('.toggler').click(function() {
$(this).next().slideToggle('slow');
});
</script>
</body>
</html>

View File

@ -1,14 +0,0 @@
<li class="resource">
<?php if (isset($resource)) : ?>
<div class="heading">
<h2><?php echo $resource; ?></h2>
</div>
<?php endif; ?>
<ul class="endpoints">
<li class="endpoint">
<ul class="operations">
<?php echo $content; ?>
</ul>
</li>
</ul>
</li>

View File

@ -0,0 +1,32 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine (even in intranet) and Chrome Frame -->
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
<title>{{ apiName }}</title>
<link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" type="text/css" />
<style type="text/css">
{{ css|raw }}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="header">
<h1>{{ apiName }}</h1>
</div>
<div class="container" id="resources_container">
<ul id="resources">
{% block content %}{% endblock %}
</ul>
</div>
<p id="colophon">
Documentation auto-generated on {{ date }}
</p>
<script type="text/javascript">
$('.toggler').click(function() {
$(this).next().slideToggle('slow');
});
</script>
</body>
</html>

View File

@ -0,0 +1,86 @@
<li class="{{ data.method|lower }} operation">
<div class="heading toggler">
<h3>
<span class="http_method">
<a>{{ data.method|upper }}</a>
</span>
<span class="path">
{{ data.uri }}
</span>
</h3>
<ul class="options">
{% if data.description is defined %}
<li>{{ data.description }}</li>
{% endif %}
</ul>
</div>
<div class="content" style="display: none;">
{% if data.requirements is defined %}
<h4>Requirements</h4>
<table class="fullwidth">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for key, value in data.requirements %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if data.filters is defined %}
<h4>Filters</h4>
<table class="fullwidth">
<thead>
<tr>
<th>Name</th>
<th>Information</th>
</tr>
</thead>
<tbody>
{% for name, infos in data.filters %}
<tr>
<td>{{ name }}</td>
<td>
<ul>
{% for key, value in infos %}
<li><em>{{ key }}</em> : {{ value }}</li>
{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if data.parameters is defined %}
<h4>Parameters</h4>
<table class='fullwidth'>
<thead>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Required?</th>
</tr>
</thead>
<tbody>
{% for name, infos in data.parameters %}
<tr>
<td>{{ name }}</td>
<td>{{ infos.dataType }}</td>
<td>{{ infos.required ? 'true' : 'false' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</li>

View File

@ -0,0 +1,13 @@
{% extends "NelmioApiDocBundle::layout.html.twig" %}
{% block content %}
<li class="resource">
<ul class="endpoints">
<li class="endpoint">
<ul class="operations">
{% include 'NelmioApiDocBundle::method.html.twig' %}
</ul>
</li>
</ul>
</li>
{% endblock content %}

View File

@ -0,0 +1,20 @@
{% extends "NelmioApiDocBundle::layout.html.twig" %}
{% block content %}
{% for resource, methods in resources %}
<li class="resource">
<div class="heading">
<h2>{{ resource }}</h2>
</div>
<ul class="endpoints">
<li class="endpoint">
<ul class="operations">
{% for data in methods %}
{% include 'NelmioApiDocBundle::method.html.twig' %}
{% endfor %}
</ul>
</li>
</ul>
</li>
{% endfor %}
{% endblock content %}