NelmioApiDocBundle/Formatter/HtmlFormatter.php
Alan Poulain b9718f3400 Compatibility with Symfony 4.3 (#1524)
* Use twig instead of templating

* Fix at least one suite test in Travis

* Use Twig namespace (Twig > 1.10 and TwigBundle > 2.2)
2022-04-26 16:58:48 +03:00

247 lines
5.2 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* 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.
*/
namespace Nelmio\ApiDocBundle\Formatter;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment as TwigEnvironment;
class HtmlFormatter extends AbstractFormatter
{
/**
* @var string
*/
protected $apiName;
/**
* @var string
*/
protected $endpoint;
/**
* @var string
*/
protected $defaultRequestFormat;
/**
* @var EngineInterface|TwigEnvironment
*/
protected $engine;
/**
* @var boolean
*/
private $enableSandbox;
/**
* @var array
*/
private $requestFormats;
/**
* @var string
*/
private $requestFormatMethod;
/**
* @var string
*/
private $acceptType;
/**
* @var array
*/
private $bodyFormats;
/**
* @var string
*/
private $defaultBodyFormat;
/**
* @var array
*/
private $authentication;
/**
* @var string
*/
private $motdTemplate;
/**
* @var boolean
*/
private $defaultSectionsOpened;
/**
* @param array $authentication
*/
public function setAuthentication(array $authentication = null)
{
$this->authentication = $authentication;
}
/**
* @param string $apiName
*/
public function setApiName($apiName)
{
$this->apiName = $apiName;
}
/**
* @param string $endpoint
*/
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
}
/**
* @param boolean $enableSandbox
*/
public function setEnableSandbox($enableSandbox)
{
$this->enableSandbox = $enableSandbox;
}
/**
* @param EngineInterface|TwigEnvironment $engine
*/
public function setTemplatingEngine($engine)
{
$this->engine = $engine;
}
/**
* @param string $acceptType
*/
public function setAcceptType($acceptType)
{
$this->acceptType = $acceptType;
}
/**
* @param array $bodyFormats
*/
public function setBodyFormats(array $bodyFormats)
{
$this->bodyFormats = $bodyFormats;
}
/**
* @param string $defaultBodyFormat
*/
public function setDefaultBodyFormat($defaultBodyFormat)
{
$this->defaultBodyFormat = $defaultBodyFormat;
}
/**
* @param string $method
*/
public function setRequestFormatMethod($method)
{
$this->requestFormatMethod = $method;
}
/**
* @param array $formats
*/
public function setRequestFormats(array $formats)
{
$this->requestFormats = $formats;
}
/**
* @param string $format
*/
public function setDefaultRequestFormat($format)
{
$this->defaultRequestFormat = $format;
}
/**
* @param string $motdTemplate
*/
public function setMotdTemplate($motdTemplate)
{
$this->motdTemplate = $motdTemplate;
}
/**
* @return string
*/
public function getMotdTemplate()
{
return $this->motdTemplate;
}
/**
* @param boolean $defaultSectionsOpened
*/
public function setDefaultSectionsOpened($defaultSectionsOpened)
{
$this->defaultSectionsOpened = $defaultSectionsOpened;
}
/**
* {@inheritdoc}
*/
protected function renderOne(array $data)
{
return $this->engine->render('@NelmioApiDoc/resource.html.twig', array_merge(
array(
'data' => $data,
'displayContent' => true,
),
$this->getGlobalVars()
));
}
/**
* {@inheritdoc}
*/
protected function render(array $collection)
{
return $this->engine->render('@NelmioApiDoc/resources.html.twig', array_merge(
array(
'resources' => $collection,
),
$this->getGlobalVars()
));
}
/**
* @return array
*/
private function getGlobalVars()
{
return array(
'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,
);
}
}