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