Files
core/src/Controller/TwigAwareController.php
Bob den Otter d448a9c1cb working
2019-02-27 13:11:57 +01:00

76 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Bolt\Controller;
use Bolt\Configuration\Config;
use Bolt\Entity\Field\TemplateselectField;
use Bolt\Version;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Illuminate\Support\Collection;
class TwigAwareController extends AbstractController
{
/**
* @var Config
*/
protected $config;
/**
* @var Environment
*/
protected $twig;
public function __construct(Config $config, Environment $twig)
{
$this->config = $config;
$this->twig = $twig;
}
/**
* Renders a view.
*
* @final
*
* @param string|array $template
*
* @throws \Twig_Error_Loader When none of the templates can be found
* @throws \Twig_Error_Syntax When an error occurred during compilation
* @throws \Twig_Error_Runtime When an error occurred during rendering
*/
protected function renderTemplate($template, array $parameters = [], ?Response $response = null): Response
{
// Set config and version.
$parameters['config'] = $parameters['config'] ?? $this->config;
$parameters['version'] = $parameters['version'] ?? Version::VERSION;
$parameters['user'] = $parameters['user'] ?? $this->getUser();
// Resolve string|array of templates into the first one that is found.
if (is_array($template)) {
$templates = (new Collection($template))
->map(function ($element): ?string {
if ($element instanceof TemplateselectField) {
return $element->__toString();
}
return $element;
})
->filter()
->toArray();
$template = $this->twig->resolveTemplate($templates);
}
$content = $this->twig->render($template, $parameters);
if ($response === null) {
$response = new Response();
}
$response->setContent($content);
return $response;
}
}