From a51d25b94f9ee10f4ea117fdfbd50f1ebf37e37f Mon Sep 17 00:00:00 2001 From: William DURAND Date: Fri, 13 Apr 2012 11:24:57 +0200 Subject: [PATCH] Used Twig instead of plain PHP in the HtmlFormatter. Fixes #1 --- Formatter/AbstractFormatter.php | 9 -- Formatter/HtmlFormatter.php | 74 ++++++---------- Formatter/MarkdownFormatter.php | 27 +++--- Formatter/SimpleFormatter.php | 8 -- Resources/config/formatters.xml | 3 + Resources/views/formatter.html.php | 86 ------------------- Resources/views/formatter_layout.html.php | 32 ------- .../views/formatter_resource_section.html.php | 14 --- Resources/views/layout.html.twig | 32 +++++++ Resources/views/method.html.twig | 86 +++++++++++++++++++ Resources/views/resource.html.twig | 13 +++ Resources/views/resources.html.twig | 20 +++++ 12 files changed, 192 insertions(+), 212 deletions(-) delete mode 100644 Resources/views/formatter.html.php delete mode 100644 Resources/views/formatter_layout.html.php delete mode 100644 Resources/views/formatter_resource_section.html.php create mode 100644 Resources/views/layout.html.twig create mode 100644 Resources/views/method.html.twig create mode 100644 Resources/views/resource.html.twig create mode 100644 Resources/views/resources.html.twig diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index 515877e..9ad7a0f 100644 --- a/Formatter/AbstractFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -61,15 +61,6 @@ abstract class AbstractFormatter implements FormatterInterface */ 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. * diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 7564f88..cdcfd21 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\Formatter; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\Routing\Route; +use Symfony\Component\Templating\EngineInterface; class HtmlFormatter extends AbstractFormatter { @@ -21,6 +22,11 @@ class HtmlFormatter extends AbstractFormatter */ private $apiName; + /** + * @var \Symfony\Component\Templating\EngineInterface + */ + private $engine; + /** * @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); - $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()); + $this->engine = $engine; } /** @@ -50,30 +48,10 @@ class HtmlFormatter extends AbstractFormatter */ protected function renderOne(array $data) { - extract($data); - - ob_start(); - 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(); + return $this->engine->render('NelmioApiDocBundle::resource.html.twig', array_merge( + array('data' => $data, 'displayContent' => true), + $this->getGlobalVars() + )); } /** @@ -81,21 +59,21 @@ class HtmlFormatter extends AbstractFormatter */ protected function render(array $collection) { - $content = ''; - foreach ($collection as $resource => $arrayOfData) { - $content .= $this->renderResourceSection($resource, $arrayOfData); - } - - return $this->renderWithLayout($content); + return $this->engine->render('NelmioApiDocBundle::resources.html.twig', array_merge( + array('resources' => $collection), + $this->getGlobalVars() + )); } - private function renderWithLayout($content) + /** + * @return array + */ + private function getGlobalVars() { - extract(array('api_name' => $this->apiName, 'content' => $content)); - - ob_start(); - include __DIR__ . '/../Resources/views/formatter_layout.html.php'; - - return ob_get_clean(); + return array( + 'apiName' => $this->apiName, + 'date' => date(DATE_RFC822), + 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), + ); } } diff --git a/Formatter/MarkdownFormatter.php b/Formatter/MarkdownFormatter.php index 65b20e2..dc748e3 100644 --- a/Formatter/MarkdownFormatter.php +++ b/Formatter/MarkdownFormatter.php @@ -64,21 +64,6 @@ class MarkdownFormatter extends AbstractFormatter 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} */ @@ -92,4 +77,16 @@ class MarkdownFormatter extends AbstractFormatter 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; + } } diff --git a/Formatter/SimpleFormatter.php b/Formatter/SimpleFormatter.php index 2c1c6e3..788f913 100644 --- a/Formatter/SimpleFormatter.php +++ b/Formatter/SimpleFormatter.php @@ -21,14 +21,6 @@ class SimpleFormatter extends AbstractFormatter return $data; } - /** - * {@inheritdoc} - */ - protected function renderResourceSection($resource, array $arrayOfData) - { - return array($resource => $arrayOfData); - } - /** * {@inheritdoc} */ diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index 6207088..4e00a7a 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -24,6 +24,9 @@ parent="nelmio_api_doc.formatter.abstract_formatter" /> + + + %nelmio_api_doc.api_name% diff --git a/Resources/views/formatter.html.php b/Resources/views/formatter.html.php deleted file mode 100644 index 6ec1e1b..0000000 --- a/Resources/views/formatter.html.php +++ /dev/null @@ -1,86 +0,0 @@ -
  • -
    -

    - - - - - - -

    -
      - -
    • - -
    -
    - -
  • diff --git a/Resources/views/formatter_layout.html.php b/Resources/views/formatter_layout.html.php deleted file mode 100644 index e502107..0000000 --- a/Resources/views/formatter_layout.html.php +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - <?php echo $api_name; ?> - - - - - - -
    -
      - -
    -
    -

    - Documentation auto-generated on -

    - - - diff --git a/Resources/views/formatter_resource_section.html.php b/Resources/views/formatter_resource_section.html.php deleted file mode 100644 index faf7c89..0000000 --- a/Resources/views/formatter_resource_section.html.php +++ /dev/null @@ -1,14 +0,0 @@ -
  • - -
    -

    -
    - -
      -
    • -
        - -
      -
    • -
    -
  • diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig new file mode 100644 index 0000000..5f45c46 --- /dev/null +++ b/Resources/views/layout.html.twig @@ -0,0 +1,32 @@ + + + + + + + {{ apiName }} + + + + + + +
    +
      + {% block content %}{% endblock %} +
    +
    +

    + Documentation auto-generated on {{ date }} +

    + + + diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig new file mode 100644 index 0000000..93e17a5 --- /dev/null +++ b/Resources/views/method.html.twig @@ -0,0 +1,86 @@ +
  • +
    +

    + + {{ data.method|upper }} + + + {{ data.uri }} + +

    +
      + {% if data.description is defined %} +
    • {{ data.description }}
    • + {% endif %} +
    +
    + +
  • diff --git a/Resources/views/resource.html.twig b/Resources/views/resource.html.twig new file mode 100644 index 0000000..1a51099 --- /dev/null +++ b/Resources/views/resource.html.twig @@ -0,0 +1,13 @@ +{% extends "NelmioApiDocBundle::layout.html.twig" %} + +{% block content %} +
  • +
      +
    • +
        + {% include 'NelmioApiDocBundle::method.html.twig' %} +
      +
    • +
    +
  • +{% endblock content %} diff --git a/Resources/views/resources.html.twig b/Resources/views/resources.html.twig new file mode 100644 index 0000000..a602100 --- /dev/null +++ b/Resources/views/resources.html.twig @@ -0,0 +1,20 @@ +{% extends "NelmioApiDocBundle::layout.html.twig" %} + +{% block content %} + {% for resource, methods in resources %} +
  • +
    +

    {{ resource }}

    +
    +
      +
    • +
        + {% for data in methods %} + {% include 'NelmioApiDocBundle::method.html.twig' %} + {% endfor %} +
      +
    • +
    +
  • + {% endfor %} +{% endblock content %}