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