mirror of
https://github.com/jbcr/core.git
synced 2026-04-02 22:32:20 +02:00
96 lines
2.7 KiB
PHP
96 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Bolt\Controller;
|
|
|
|
use Bolt\Configuration\Config;
|
|
use Bolt\Entity\Field\TemplateselectField;
|
|
use Bolt\Snippets\Manager;
|
|
use Bolt\Version;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Tightenco\Collect\Support\Collection;
|
|
use Twig\Environment;
|
|
|
|
class TwigAwareController extends AbstractController
|
|
{
|
|
/** @var Config */
|
|
protected $config;
|
|
|
|
/** @var Environment */
|
|
protected $twig;
|
|
|
|
/** @var Manager */
|
|
private $snippetManager;
|
|
|
|
/** @var Manager */
|
|
private $snippetManager;
|
|
|
|
/**
|
|
* @required
|
|
*/
|
|
public function setAutowire(Config $config, Environment $twig, Manager $snippetManager): void
|
|
{
|
|
$this->config = $config;
|
|
$this->twig = $twig;
|
|
}
|
|
|
|
/**
|
|
* @required
|
|
*/
|
|
public function setSnippetManager(Manager $snippetManager): void
|
|
{
|
|
$this->snippetManager = $snippetManager;
|
|
|
|
$this->snippetManager->registerBoltSnippets();
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
// Render the template
|
|
$content = $this->twig->render($template, $parameters);
|
|
|
|
// Make sure we have a Response
|
|
if ($response === null) {
|
|
$response = new Response();
|
|
}
|
|
$response->setContent($content);
|
|
|
|
// Process the snippet Queue on the Response
|
|
$this->snippetManager->processQueue($response);
|
|
|
|
return $response;
|
|
}
|
|
}
|