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