* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\View;
use FOS\RestBundle\Context\Context;
use FOS\RestBundle\Serializer\Serializer;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;
use Twig\Environment;
/**
* View may be used in controllers to build up a response in a format agnostic way
* The View class takes care of encoding your data in json, xml, or renders a
* template for html via the Serializer component.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Lukas K. Smith <smith@pooteeweet.org>
*
* @final since 2.8
*/
class ViewHandler implements ConfigurableViewHandlerInterface
{
/**
* Key format, value a callable that returns a Response instance.
*
* @var array
*/
protected $customHandlers = [];
/**
* The supported formats as keys and if the given formats
* uses templating is denoted by a true value.
*
* @var array
*/
protected $formats;
/**
* @var int
*/
protected $failedValidationCode;
/**
* @var int
*/
protected $emptyContentCode;
/**
* @var bool
*/
protected $serializeNull;
/**
* If to force a redirect for the given key format,
* with value being the status code to use.
*
* @var array<string,int>
*/
protected $forceRedirects;
/**
* @var string|null
*/
protected $defaultEngine;
/**
* @var array
*/
protected $exclusionStrategyGroups = [];
/**
* @var string
*/
protected $exclusionStrategyVersion;
/**
* @var bool
*/
protected $serializeNullStrategy;
private $urlGenerator;
private $serializer;
private $templating;
private $requestStack;
private $options;
/**
* @param EngineInterface|Environment $templating The configured templating engine
*/
public function __construct(
UrlGeneratorInterface $urlGenerator,
Serializer $serializer,
$templating,
RequestStack $requestStack,
array $formats = null,
int $failedValidationCode = Response::HTTP_BAD_REQUEST,
int $emptyContentCode = Response::HTTP_NO_CONTENT,
bool $serializeNull = false,
array $forceRedirects = null,
?string $defaultEngine = 'twig',
array $options = []
) {
if (11 >= func_num_args() || func_get_arg(11)) {
@trigger_error(sprintf('The constructor of the %s class is deprecated, use the static create() factory method instead.', __CLASS__), E_USER_DEPRECATED);
throw new \TypeError(sprintf('If provided, the templating engine must be an instance of %s or %s, but %s was given.', EngineInterface::class, Environment::class, get_class($templating)));
public function renderTemplate(View $view, $format)
{
if (2 === func_num_args() || func_get_arg(2)) {
@trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.', __METHOD__), E_USER_DEPRECATED);
}
if (null === $this->templating) {
throw new \LogicException(sprintf('An instance of %s or %s must be injected in %s to render templates.', EngineInterface::class, Environment::class, __CLASS__));