NelmioApiDocBundle/Formatter/HtmlFormatter.php

232 lines
4.9 KiB
PHP
Raw Normal View History

<?php
2012-04-13 11:03:05 +02:00
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2012-04-12 18:37:42 +02:00
namespace Nelmio\ApiDocBundle\Formatter;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment as TwigEnvironment;
class HtmlFormatter extends AbstractFormatter
{
/**
2012-11-17 17:38:35 +01:00
* @var string
*/
2012-11-17 17:38:35 +01:00
protected $apiName;
/**
* @var string
*/
2012-11-17 17:38:35 +01:00
protected $endpoint;
/**
* @var string
*/
2012-11-17 17:38:35 +01:00
protected $defaultRequestFormat;
/**
* @var EngineInterface|TwigEnvironment
*/
2012-11-17 17:38:35 +01:00
protected $engine;
/**
2024-10-01 15:54:04 +03:00
* @var bool
*/
2012-11-17 17:38:35 +01:00
private $enableSandbox;
2014-05-17 22:45:30 +02:00
/**
* @var array
*/
private $requestFormats;
/**
* @var string
*/
2012-10-17 15:15:35 +04:00
private $requestFormatMethod;
/**
* @var string
*/
private $acceptType;
/**
* @var array
*/
private $bodyFormats;
/**
* @var string
*/
private $defaultBodyFormat;
/**
2012-11-17 17:38:35 +01:00
* @var array
*/
2012-11-17 17:38:35 +01:00
private $authentication;
2013-04-08 11:44:43 +02:00
/**
* @var string
*/
private $motdTemplate;
/**
2024-10-01 15:54:04 +03:00
* @var bool
*/
private $defaultSectionsOpened;
2024-10-01 15:54:04 +03:00
public function setAuthentication(?array $authentication = null): void
{
$this->authentication = $authentication;
}
/**
* @param string $apiName
*/
2024-10-01 15:54:04 +03:00
public function setApiName($apiName): void
{
$this->apiName = $apiName;
}
/**
* @param string $endpoint
*/
2024-10-01 15:54:04 +03:00
public function setEndpoint($endpoint): void
{
$this->endpoint = $endpoint;
}
/**
2024-10-01 15:54:04 +03:00
* @param bool $enableSandbox
*/
2024-10-01 15:54:04 +03:00
public function setEnableSandbox($enableSandbox): void
{
$this->enableSandbox = $enableSandbox;
}
/**
* @param EngineInterface|TwigEnvironment $engine
*/
2024-10-01 15:54:04 +03:00
public function setTemplatingEngine($engine): void
{
$this->engine = $engine;
}
/**
* @param string $acceptType
*/
2024-10-01 15:54:04 +03:00
public function setAcceptType($acceptType): void
{
$this->acceptType = $acceptType;
}
2024-10-01 15:54:04 +03:00
public function setBodyFormats(array $bodyFormats): void
{
$this->bodyFormats = $bodyFormats;
}
/**
* @param string $defaultBodyFormat
*/
2024-10-01 15:54:04 +03:00
public function setDefaultBodyFormat($defaultBodyFormat): void
{
$this->defaultBodyFormat = $defaultBodyFormat;
}
/**
* @param string $method
*/
2024-10-01 15:54:04 +03:00
public function setRequestFormatMethod($method): void
{
2012-10-17 15:15:35 +04:00
$this->requestFormatMethod = $method;
}
2024-10-01 15:54:04 +03:00
public function setRequestFormats(array $formats): void
2014-05-17 22:45:30 +02:00
{
$this->requestFormats = $formats;
}
/**
* @param string $format
*/
2024-10-01 15:54:04 +03:00
public function setDefaultRequestFormat($format): void
{
$this->defaultRequestFormat = $format;
}
2013-04-08 11:44:43 +02:00
/**
* @param string $motdTemplate
*/
2024-10-01 15:54:04 +03:00
public function setMotdTemplate($motdTemplate): void
2013-04-08 11:44:43 +02:00
{
$this->motdTemplate = $motdTemplate;
}
/**
* @return string
*/
public function getMotdTemplate()
{
return $this->motdTemplate;
}
/**
2024-10-01 15:54:04 +03:00
* @param bool $defaultSectionsOpened
*/
2024-10-01 15:54:04 +03:00
public function setDefaultSectionsOpened($defaultSectionsOpened): void
{
$this->defaultSectionsOpened = $defaultSectionsOpened;
}
protected function renderOne(array $data)
{
return $this->engine->render('@NelmioApiDoc/resource.html.twig', array_merge(
2024-10-01 15:54:04 +03:00
[
'data' => $data,
'displayContent' => true,
2024-10-01 15:54:04 +03:00
],
$this->getGlobalVars()
));
}
protected function render(array $collection)
{
return $this->engine->render('@NelmioApiDoc/resources.html.twig', array_merge(
2024-10-01 15:54:04 +03:00
[
'resources' => $collection,
2024-10-01 15:54:04 +03:00
],
$this->getGlobalVars()
));
}
/**
* @return array
*/
private function getGlobalVars()
{
2024-10-01 15:54:04 +03:00
return [
'apiName' => $this->apiName,
'authentication' => $this->authentication,
'endpoint' => $this->endpoint,
'enableSandbox' => $this->enableSandbox,
'requestFormatMethod' => $this->requestFormatMethod,
'acceptType' => $this->acceptType,
'bodyFormats' => $this->bodyFormats,
'defaultBodyFormat' => $this->defaultBodyFormat,
'requestFormats' => $this->requestFormats,
'defaultRequestFormat' => $this->defaultRequestFormat,
'date' => date(DATE_RFC822),
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
'motdTemplate' => $this->motdTemplate,
'defaultSectionsOpened' => $this->defaultSectionsOpened,
2024-10-01 15:54:04 +03:00
];
}
}