<?php
|
/**
|
* Created by PhpStorm.
|
* User: Fiber
|
* Date: 2017.01.11.
|
* Time: 12:27
|
*/
|
|
namespace EOG\Utils;
|
|
|
use Twig\Environment;
|
use Twig\Extension\CoreExtension;
|
use Twig\Extension\DebugExtension;
|
use Twig\Loader\FilesystemLoader;
|
use Twig\TwigFilter;
|
use Twig\TwigFunction;
|
|
class TwigFactory
|
{
|
private const CACHE_DIR = SITE_ROOT . 'cache/twig-templates/';
|
|
/**
|
* Visszatér egy beállított Twig_environment -el, amikbe már be van húzva a használt alap függvények
|
*
|
* @param array $fileSystemPaths
|
* @param array $twigEnvironmentOptions
|
*
|
* @return Environment
|
*/
|
public static function getEnvironment($fileSystemPaths = [], array $twigEnvironmentOptions = [])
|
{
|
$loader = null;
|
|
if ($fileSystemPaths)
|
{
|
if (is_string($fileSystemPaths))
|
{
|
$fileSystemPaths = [$fileSystemPaths];
|
}
|
|
$loader = new FilesystemLoader($fileSystemPaths);
|
}
|
|
$twigEnvironmentOptions['cache'] = static::CACHE_DIR;
|
$twigEnvironmentOptions['auto_reload'] = true;
|
|
if (IS_DEV)
|
{
|
$twigEnvironmentOptions['debug'] = true;
|
}
|
|
$twigEnvironment = new Environment($loader, $twigEnvironmentOptions);
|
|
if (IS_DEV)
|
{
|
$twigEnvironment->addExtension(new DebugExtension());
|
}
|
|
return self::addFunctions($twigEnvironment);
|
}
|
|
public static function addFunctions(Environment &$te)
|
{
|
|
return $te;
|
}
|
|
public static function clearFileCache()
|
{
|
$di = new \RecursiveDirectoryIterator(self::CACHE_DIR, \FilesystemIterator::SKIP_DOTS);
|
$ri = new \RecursiveIteratorIterator($di, \RecursiveIteratorIterator::CHILD_FIRST);
|
foreach ($ri as $file)
|
{
|
$file->isDir() ? rmdir($file) : unlink($file);
|
}
|
return true;
|
}
|
}
|