for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Gvera\Services;
use Gvera\Helpers\config\Config;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class TwigService
{
const VIEWS_PREFIX = __DIR__ . '/../Views/';
private Config $config;
private bool $loadTwig;
private Environment $twig;
/**
* TwigService constructor.
* @param Config $config
*/
public function __construct(Config $config)
$this->config = $config;
}
* @param $controllerName
* @param $controllerMethod
* @param null $path
* @return bool
public function needsTwig($controllerName, $controllerMethod, $path = null): bool
$path = $path ?? self::VIEWS_PREFIX;
if (null === $this->loadTwig) {
$this->loadTwig = file_exists(
$path .
$controllerName .
DIRECTORY_SEPARATOR .
$controllerMethod . '.html.twig'
);
return $this->loadTwig;
* @param string|null $path
* @return Environment
public function loadTwig(string $path = null): Environment
$devMode = boolval($this->config->getConfigItem('devmode'));
$cache = $devMode ? false : __DIR__ . '/../../var/cache/views/';
$loader = new FilesystemLoader($path);
$this->twig = new Environment($loader, ['cache' => $cache, 'debug' => $devMode]);
return $this->twig;
public function render($name, $method, $viewParams)
return $this->twig->render(
DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . $method . '.html.twig',
$viewParams
public function reset()
$this->twig = null;
$this->loadTwig = null;