private function registerTwig()
{
$resourcesPath = $this->container->getParameter('tutu.root_path') . '/resources';
if ($customPath = getenv('tutu_resources')) {
if (!file_exists($customPath)) {
throw new \RuntimeException(sprintf('Custom resources path \\"%s\\" does not exist.', $customPath));
}
$resourcesPath = $customPath;
}
$this->container->setParameter('resources_path', $resourcesPath);
$this->container->setStaticDefinition('twig_loader', function ($container) {
$stringLoader = new \Twig_Loader_String();
$filesystemLoader = new \Twig_Loader_Filesystem();
$filesystemLoader->addPath($container->getParameter('resources_path'), 'resources');
return new \Twig_Loader_Chain([$filesystemLoader, $stringLoader]);
});
$this->container->setStaticDefinition('twig', function ($container) {
$defaultOptions = ['cache' => $container->getParameter('tutu.root_path') . '/var/twig', 'globals' => []];
$options = $container->hasParameter('twig') && is_array($container->getParameter('twig')) ? array_merge($defaultOptions, $container->getParameter('twig')) : $defaultOptions;
$twig = new \Twig_Environment($container->getService('twig_loader'), $options);
if (is_array($options['globals'])) {
foreach ($options['globals'] as $name => $value) {
$twig->addGlobal($name, $this->getValue($value));
}
}
return $twig;
});
}