|
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\Service; |
|
9
|
|
|
use allejo\stakx\System\Filesystem; |
|
10
|
|
|
use allejo\stakx\Twig\FilesystemExtension; |
|
11
|
|
|
use allejo\stakx\Twig\StakxTwigFileLoader; |
|
12
|
|
|
use allejo\stakx\Twig\TextExtension; |
|
13
|
|
|
use allejo\stakx\Twig\TwigExtension; |
|
14
|
|
|
use Aptoma\Twig\Extension\MarkdownExtension; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
17
|
|
|
use Twig_Environment; |
|
18
|
|
|
use Twig_Extension_Debug; |
|
19
|
|
|
|
|
20
|
|
|
class TwigStakxBridgeFactory |
|
21
|
|
|
{ |
|
22
|
14 |
|
public static function createTwigEnvironment(Configuration $configuration, LoggerInterface $logger) |
|
23
|
|
|
{ |
|
24
|
14 |
|
$fs = new Filesystem(); |
|
|
|
|
|
|
25
|
14 |
|
$loader = new StakxTwigFileLoader(array( |
|
26
|
14 |
|
getcwd(), |
|
27
|
14 |
|
)); |
|
28
|
|
|
|
|
29
|
14 |
|
$theme = $configuration->getTheme(); |
|
30
|
14 |
|
$mdEngine = new TwigMarkdownEngine(); |
|
31
|
|
|
|
|
32
|
|
|
// Only load a theme if one is specified and actually exists |
|
33
|
14 |
|
if ($theme !== null) |
|
34
|
14 |
|
{ |
|
35
|
|
|
try |
|
36
|
|
|
{ |
|
37
|
|
|
$loader->addPath($fs->absolutePath('_themes', $theme), 'theme'); |
|
38
|
|
|
} |
|
39
|
|
|
catch (\Twig_Error_Loader $e) |
|
40
|
|
|
{ |
|
41
|
|
|
$logger->error('The following theme could not be loaded: {theme}', array( |
|
42
|
|
|
'theme' => $theme, |
|
43
|
|
|
)); |
|
44
|
|
|
$logger->error($e->getMessage()); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
14 |
|
$twig = new Twig_Environment($loader, array( |
|
49
|
14 |
|
'autoescape' => $configuration->getTwigAutoescape(), |
|
50
|
14 |
|
'auto_reload' => true, |
|
51
|
14 |
|
'cache' => $fs->absolutePath('.stakx-cache/twig'), |
|
52
|
14 |
|
)); |
|
53
|
|
|
|
|
54
|
|
|
// We'll use this to access the current file in our Twig filters |
|
55
|
14 |
|
$twig->addGlobal('__currentTemplate', ''); |
|
56
|
|
|
|
|
57
|
14 |
|
$twig->addExtension(new TwigExtension()); |
|
58
|
14 |
|
$twig->addExtension(new TextExtension()); |
|
59
|
14 |
|
$twig->addExtension(new MarkdownExtension($mdEngine)); |
|
60
|
|
|
|
|
61
|
14 |
|
if (!Service::getParameter(BuildableCommand::SAFE_MODE)) |
|
62
|
14 |
|
{ |
|
63
|
14 |
|
$twig->addExtension(new FilesystemExtension()); |
|
64
|
14 |
|
} |
|
65
|
|
|
|
|
66
|
14 |
|
$profiler = null; |
|
67
|
|
|
|
|
68
|
14 |
|
if (Service::getParameter(BuildableCommand::BUILD_PROFILE)) |
|
69
|
14 |
|
{ |
|
70
|
|
|
$profiler = new \Twig_Profiler_Profile(); |
|
71
|
|
|
$twig->addExtension(new \Twig_Extension_Profiler($profiler)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
14 |
|
if ($configuration->isDebug()) |
|
75
|
14 |
|
{ |
|
76
|
|
|
$twig->addExtension(new Twig_Extension_Debug()); |
|
77
|
|
|
$twig->enableDebug(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
14 |
|
$bridge = new TwigStakxBridge($twig); |
|
81
|
14 |
|
$bridge->setProfiler($profiler); |
|
82
|
|
|
|
|
83
|
14 |
|
return $bridge; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.