|
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
|
12 |
|
$mdEngine = new TwigMarkdownEngine(); |
|
43
|
|
|
|
|
44
|
|
|
// Only load a theme if one is specified and actually exists |
|
45
|
12 |
|
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
|
12 |
|
$twig = new Twig_Environment($loader, array( |
|
61
|
12 |
|
'autoescape' => $configuration->getTwigAutoescape(), |
|
62
|
|
|
'auto_reload' => true, |
|
63
|
12 |
|
'cache' => $fs->absolutePath('.stakx-cache/twig'), |
|
64
|
|
|
)); |
|
65
|
|
|
|
|
66
|
|
|
// We'll use this to access the current file in our Twig filters |
|
67
|
12 |
|
$twig->addGlobal('__currentTemplate', ''); |
|
68
|
|
|
|
|
69
|
|
|
// Global variables maintained by stakx |
|
70
|
12 |
|
$twig->addGlobal('site', $configuration->getConfiguration()); |
|
71
|
12 |
|
$twig->addGlobal('data', $dataManager->getJailedDataItems()); |
|
72
|
12 |
|
$twig->addGlobal('collections', $collectionManager->getJailedCollections()); |
|
73
|
12 |
|
$twig->addGlobal('menu', $menuManager->getSiteMenu()); |
|
74
|
12 |
|
$twig->addGlobal('pages', $pageManager->getJailedStaticPageViews()); |
|
75
|
|
|
|
|
76
|
12 |
|
$twig->addExtension($twigExtension); |
|
77
|
12 |
|
$twig->addExtension(new MarkdownExtension($mdEngine)); |
|
78
|
|
|
|
|
79
|
12 |
|
$profiler = null; |
|
80
|
|
|
|
|
81
|
12 |
|
if (Service::getParameter(BuildableCommand::BUILD_PROFILE)) |
|
82
|
|
|
{ |
|
83
|
|
|
$profiler = new \Twig_Profiler_Profile(); |
|
84
|
|
|
$twig->addExtension(new \Twig_Extension_Profiler($profiler)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
12 |
|
if ($configuration->isDebug()) |
|
88
|
|
|
{ |
|
89
|
|
|
$twig->addExtension(new Twig_Extension_Debug()); |
|
90
|
|
|
$twig->enableDebug(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
12 |
|
$bridge = new TwigStakxBridge($twig); |
|
94
|
12 |
|
$bridge->setProfiler($profiler); |
|
95
|
|
|
|
|
96
|
12 |
|
return $bridge; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.