/
Twig.php
120 lines (103 loc) · 3.29 KB
/
Twig.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace tourze\Twig;
use Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine;
use Aptoma\Twig\Extension\MarkdownExtension;
use Aptoma\Twig\Extension\MarkdownEngine;
use Asm89\Twig\CacheExtension\CacheProvider\DoctrineCacheAdapter;
use Asm89\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;
use Doctrine\Common\Cache\ArrayCache;
use tourze\Base\Base;
use tourze\Base\Config;
use tourze\Base\Helper\Arr;
use tourze\Twig\Exception\TwigException;
use tourze\Twig\Loader\ArrayLoader;
use tourze\Twig\Loader\FileLoader;
use Twig_Environment;
use Twig_Extension_HTMLHelpers;
use Twig_LoaderInterface;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Twig助手类
*
* @package tourze\Twig
*/
class Twig extends Base
{
/**
* 数组加载器
*/
const ARRAY_LOADER = 1;
/**
* @const 文件加载器
*/
const FILE_LOADER = 2;
/**
* @param array $options
* @return ArrayLoader|FileLoader
* @throws \tourze\Twig\Exception\TwigException
*/
public static function createLoader(array $options)
{
$type = Arr::get($options, 'type', 'Array');
$args = Arr::get($options, 'args', []);
switch ($type)
{
case self::ARRAY_LOADER:
$instance = new ArrayLoader($args);
break;
case self::FILE_LOADER:
$instance = new FileLoader($args);
break;
default:
throw new TwigException('The requested twig loader not found.');
}
return $instance;
}
/**
* @param Twig_LoaderInterface $loader
* @param array $options
* @return Twig_Environment
*/
public static function createEnvironment(Twig_LoaderInterface $loader, $options)
{
$environment = new Twig_Environment($loader, $options);
// 缓存插件设置
$cacheProvider = new DoctrineCacheAdapter(new ArrayCache);
$cacheStrategy = new LifetimeCacheStrategy($cacheProvider);
$cacheExtension = new CacheExtension($cacheStrategy);
$environment->addExtension($cacheExtension);
// 增加markdown支持
$engine = new MichelfMarkdownEngine();
$environment->addExtension(new MarkdownExtension($engine));
// 一些额外的html助手方法
$environment->addExtension(new Twig_Extension_HTMLHelpers);
// 自定义filter
foreach (Config::load('twig')->get('filter') as $name => $filter)
{
$environment->addFilter(new Twig_SimpleFilter($name, $filter));
}
// 自定义函数
foreach (Config::load('twig')->get('function') as $name => $function)
{
$environment->addFunction(new Twig_SimpleFunction($name, $function));
}
// 自定义token解析器
foreach (Config::load('twig')->get('tokenParser') as $name => $tokenParser)
{
$environment->addTokenParser(new $tokenParser);
}
return $environment;
}
/**
* 获取TWIG组件
*
* @return \tourze\Twig\Component\Twig
* @throws \tourze\Base\Exception\ComponentNotFoundException
*/
public static function getTwig()
{
return self::get('twig');
}
}