1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Templating; |
4
|
|
|
|
5
|
|
|
use allejo\stakx\Command\BuildableCommand; |
6
|
|
|
use allejo\stakx\Configuration; |
7
|
|
|
use allejo\stakx\Engines\Markdown\TwigMarkdownEngine; |
8
|
|
|
use allejo\stakx\Manager\CollectionManager; |
9
|
|
|
use allejo\stakx\Manager\DataManager; |
10
|
|
|
use allejo\stakx\Manager\MenuManager; |
11
|
|
|
use allejo\stakx\Manager\PageManager; |
12
|
|
|
use allejo\stakx\Service; |
13
|
|
|
use allejo\stakx\System\Filesystem; |
14
|
|
|
use allejo\stakx\Twig\FilesystemExtension; |
15
|
|
|
use allejo\stakx\Twig\StakxTwigFileLoader; |
16
|
|
|
use allejo\stakx\Twig\TextExtension; |
17
|
|
|
use allejo\stakx\Twig\TwigExtension; |
18
|
|
|
use Aptoma\Twig\Extension\MarkdownExtension; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Twig_Environment; |
23
|
|
|
use Twig_Extension_Debug; |
24
|
|
|
|
25
|
|
|
class TwigStakxBridgeFactory |
26
|
|
|
{ |
27
|
|
|
public static function createTwigEnvironment( |
28
|
|
|
Configuration $configuration, |
29
|
|
|
CollectionManager $collectionManager, |
30
|
|
|
DataManager $dataManager, |
31
|
|
|
PageManager $pageManager, |
32
|
|
|
MenuManager $menuManager, |
33
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
|
|
|
34
|
|
|
LoggerInterface $logger |
35
|
|
|
) { |
36
|
|
|
$fs = new Filesystem(); |
|
|
|
|
37
|
|
|
$loader = new StakxTwigFileLoader(array( |
38
|
|
|
getcwd(), |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
$theme = $configuration->getTheme(); |
42
|
|
|
$mdEngine = new TwigMarkdownEngine(); |
43
|
|
|
|
44
|
|
|
// Only load a theme if one is specified and actually exists |
45
|
|
|
if ($theme !== null) |
46
|
|
|
{ |
47
|
|
|
try |
48
|
|
|
{ |
49
|
|
|
$loader->addPath($fs->absolutePath('_themes', $theme), 'theme'); |
50
|
|
|
} |
51
|
|
|
catch (\Twig_Error_Loader $e) |
52
|
|
|
{ |
53
|
|
|
$logger->error('The following theme could not be loaded: {theme}', array( |
54
|
|
|
'theme' => $theme, |
55
|
|
|
)); |
56
|
|
|
$logger->error($e->getMessage()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$twig = new Twig_Environment($loader, array( |
61
|
|
|
'autoescape' => $configuration->getTwigAutoescape(), |
62
|
|
|
'auto_reload' => true, |
63
|
|
|
'cache' => $fs->absolutePath('.stakx-cache/twig'), |
64
|
|
|
)); |
65
|
|
|
|
66
|
|
|
// We'll use this to access the current file in our Twig filters |
67
|
|
|
$twig->addGlobal('__currentTemplate', ''); |
68
|
|
|
|
69
|
|
|
// Global variables maintained by stakx |
70
|
|
|
$twig->addGlobal('site', $configuration->getConfiguration()); |
71
|
|
|
$twig->addGlobal('data', $dataManager->getJailedDataItems()); |
72
|
|
|
$twig->addGlobal('collections', $collectionManager->getJailedCollections()); |
73
|
|
|
$twig->addGlobal('menu', $menuManager->getSiteMenu()); |
74
|
|
|
$twig->addGlobal('pages', $pageManager->getJailedStaticPageViews()); |
75
|
|
|
|
76
|
|
|
$twig->addExtension(new TwigExtension()); |
77
|
|
|
$twig->addExtension(new TextExtension()); |
78
|
|
|
$twig->addExtension(new MarkdownExtension($mdEngine)); |
79
|
|
|
|
80
|
|
|
if (!Service::getParameter(BuildableCommand::SAFE_MODE)) |
81
|
|
|
{ |
82
|
|
|
$twig->addExtension(new FilesystemExtension()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$profiler = null; |
86
|
|
|
|
87
|
|
|
if (Service::getParameter(BuildableCommand::BUILD_PROFILE)) |
88
|
|
|
{ |
89
|
|
|
$profiler = new \Twig_Profiler_Profile(); |
90
|
|
|
$twig->addExtension(new \Twig_Extension_Profiler($profiler)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($configuration->isDebug()) |
94
|
|
|
{ |
95
|
|
|
$twig->addExtension(new Twig_Extension_Debug()); |
96
|
|
|
$twig->enableDebug(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$bridge = new TwigStakxBridge($twig); |
100
|
|
|
$bridge->setProfiler($profiler); |
101
|
|
|
|
102
|
|
|
return $bridge; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.