Fibinger Ádám
2020-09-10 f2e0b5357a6f019780773cb87cc5c633b27c88f2
commit | author | age
2b49a0 1 <?php
2 /**
3  * Created by PhpStorm.
4  * User: Fiber
5  * Date: 2017.01.11.
6  * Time: 12:27
7  */
8
9 namespace EOG\Utils;
10
11
12 use Twig\Environment;
13 use Twig\Extension\CoreExtension;
14 use Twig\Extension\DebugExtension;
15 use Twig\Loader\FilesystemLoader;
16 use Twig\TwigFilter;
17 use Twig\TwigFunction;
18
19 class TwigFactory
20 {
812087 21     private const CACHE_DIR = SITE_ROOT . '/cache/twig-templates/';
2b49a0 22
23     /**
24      * Visszatér egy beállított Twig_environment -el, amikbe már be van húzva a használt alap függvények
25      *
26      * @param array $fileSystemPaths
27      * @param array $twigEnvironmentOptions
28      *
29      * @return Environment
30      */
31     public static function getEnvironment($fileSystemPaths = [], array $twigEnvironmentOptions = [])
32     {
33         $loader = null;
34
35         if ($fileSystemPaths)
36         {
37             if (is_string($fileSystemPaths))
38             {
39                 $fileSystemPaths = [$fileSystemPaths];
40             }
41
42             $loader = new FilesystemLoader($fileSystemPaths);
43         }
44
b40e93 45         //$twigEnvironmentOptions['cache'] = static::CACHE_DIR;
2b49a0 46         $twigEnvironmentOptions['auto_reload'] = true;
47
48         if (IS_DEV)
49         {
50             $twigEnvironmentOptions['debug'] = true;
51         }
52
53         $twigEnvironment = new Environment($loader, $twigEnvironmentOptions);
54
55         if (IS_DEV)
56         {
57             $twigEnvironment->addExtension(new DebugExtension());
58         }
59
60         return self::addFunctions($twigEnvironment);
61     }
62
63     public static function addFunctions(Environment &$te)
64     {
65
66         return $te;
67     }
68
69     public static function clearFileCache()
70     {
71         $di = new \RecursiveDirectoryIterator(self::CACHE_DIR, \FilesystemIterator::SKIP_DOTS);
72         $ri = new \RecursiveIteratorIterator($di, \RecursiveIteratorIterator::CHILD_FIRST);
73         foreach ($ri as $file)
74         {
75             $file->isDir() ? rmdir($file) : unlink($file);
76         }
77         return true;
78     }
79 }