Fibinger Ádám
2019-10-09 2b49a0e495a546e8b87bbab1ce386d79d933dce6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?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;
    }
}