1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Manager; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Engines\Markdown\TwigMarkdownEngine; |
11
|
|
|
use allejo\stakx\Object\Configuration; |
12
|
|
|
use allejo\stakx\Twig\FilesystemExtension; |
13
|
|
|
use allejo\stakx\Twig\TextExtension; |
14
|
|
|
use allejo\stakx\Twig\TwigExtension; |
15
|
|
|
use Aptoma\Twig\Extension\MarkdownExtension; |
16
|
|
|
use Twig_Environment; |
17
|
|
|
use Twig_Loader_Filesystem; |
18
|
|
|
|
19
|
|
|
class TwigManager extends BaseManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Twig_Environment |
23
|
|
|
*/ |
24
|
|
|
private static $twig; |
25
|
|
|
|
26
|
12 |
|
public static function &getInstance() |
27
|
|
|
{ |
28
|
12 |
|
return self::$twig; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Configuration $configuration |
33
|
|
|
* @param mixed $options |
34
|
|
|
*/ |
35
|
5 |
|
public function configureTwig($configuration, $options = array()) |
36
|
|
|
{ |
37
|
5 |
|
$loader = new Twig_Loader_Filesystem(array( |
38
|
5 |
|
getcwd(), |
39
|
5 |
|
)); |
40
|
5 |
|
$theme = $configuration->getTheme(); |
41
|
5 |
|
$mdEngine = new TwigMarkdownEngine(); |
42
|
|
|
|
43
|
|
|
// Only load a theme if one is specified and actually exists |
44
|
5 |
|
if (!is_null($theme)) |
45
|
5 |
|
{ |
46
|
|
|
try |
47
|
|
|
{ |
48
|
|
|
$loader->addPath($this->fs->absolutePath('_themes', $configuration->getTheme()), 'theme'); |
49
|
|
|
} |
50
|
|
|
catch (\Twig_Error_Loader $e) |
51
|
|
|
{ |
52
|
|
|
$this->output->error('The following theme could not be loaded: {theme}', array( |
53
|
|
|
'theme' => $theme, |
54
|
|
|
)); |
55
|
|
|
$this->output->error($e->getMessage()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
self::$twig = new Twig_Environment($loader, array( |
60
|
5 |
|
'autoescape' => $configuration->getTwigAutoescape(), |
61
|
5 |
|
'auto_reload' => true, |
62
|
5 |
|
'cache' => $this->fs->absolutePath('.stakx-cache/twig'), |
63
|
5 |
|
)); |
64
|
|
|
|
65
|
5 |
|
foreach ($options['globals'] as $global) |
66
|
|
|
{ |
67
|
|
|
self::$twig->addGlobal($global['name'], $global['value']); |
68
|
5 |
|
} |
69
|
|
|
|
70
|
|
|
// We'll use this to access the current file in our Twig filters |
71
|
5 |
|
self::$twig->addGlobal('__currentTemplate', ''); |
72
|
|
|
|
73
|
5 |
|
self::$twig->addExtension(new TwigExtension()); |
74
|
5 |
|
self::$twig->addExtension(new TextExtension()); |
75
|
5 |
|
self::$twig->addExtension(new MarkdownExtension($mdEngine)); |
76
|
|
|
|
77
|
5 |
|
if (!$options['safe']) |
78
|
5 |
|
{ |
79
|
5 |
|
self::$twig->addExtension(new FilesystemExtension()); |
80
|
5 |
|
} |
81
|
|
|
|
82
|
5 |
|
if ($configuration->isDebug()) |
83
|
5 |
|
{ |
84
|
|
|
self::$twig->addExtension(new \Twig_Extension_Debug()); |
85
|
|
|
self::$twig->enableDebug(); |
86
|
|
|
} |
87
|
5 |
|
} |
88
|
|
|
} |
89
|
|
|
|