1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Templating\Twig; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Configuration; |
11
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
12
|
|
|
use allejo\stakx\RuntimeStatus; |
13
|
|
|
use allejo\stakx\Service; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Twig_Environment; |
16
|
|
|
use Twig_Extension_Debug; |
17
|
|
|
|
18
|
|
|
class TwigStakxBridgeFactory |
19
|
|
|
{ |
20
|
48 |
|
public static function createTwigEnvironment( |
21
|
|
|
Configuration $configuration, |
22
|
|
|
TwigExtension $twigExtension, |
23
|
|
|
LoggerInterface $logger |
24
|
|
|
) { |
25
|
48 |
|
$loader = new TwigFileLoader([ |
26
|
48 |
|
Service::getWorkingDirectory(), |
27
|
|
|
]); |
28
|
|
|
|
29
|
48 |
|
$theme = $configuration->getTheme(); |
30
|
|
|
|
31
|
|
|
// Only load a theme if one is specified and actually exists |
32
|
48 |
|
if ($theme !== null) |
33
|
|
|
{ |
34
|
|
|
try |
35
|
|
|
{ |
36
|
|
|
$loader->addPath(fs::absolutePath('_themes', $theme), 'theme'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
catch (\Twig_Error_Loader $e) |
39
|
|
|
{ |
40
|
|
|
$logger->error('The following theme could not be loaded: {theme}', [ |
41
|
|
|
'theme' => $theme, |
42
|
|
|
]); |
43
|
|
|
$logger->error($e->getMessage()); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
48 |
|
$twig = new Twig_Environment($loader, [ |
48
|
48 |
|
'autoescape' => $configuration->getTwigAutoescape(), |
49
|
|
|
'auto_reload' => true, |
50
|
48 |
|
'cache' => fs::absolutePath('.stakx-cache/twig'), |
|
|
|
|
51
|
|
|
]); |
52
|
|
|
|
53
|
48 |
|
$twig->addExtension($twigExtension); |
54
|
|
|
|
55
|
48 |
|
$profiler = null; |
56
|
|
|
|
57
|
48 |
|
if (Service::hasRunTimeFlag(RuntimeStatus::IN_PROFILE_MODE)) |
58
|
|
|
{ |
59
|
|
|
$profiler = new \Twig_Profiler_Profile(); |
60
|
|
|
$twig->addExtension(new \Twig_Extension_Profiler($profiler)); |
61
|
|
|
} |
62
|
|
|
|
63
|
48 |
|
if ($configuration->isDebug()) |
64
|
|
|
{ |
65
|
|
|
$twig->addExtension(new Twig_Extension_Debug()); |
66
|
|
|
$twig->enableDebug(); |
67
|
|
|
} |
68
|
|
|
|
69
|
48 |
|
$bridge = new TwigStakxBridge($twig); |
70
|
48 |
|
$bridge->setProfiler($profiler); |
71
|
|
|
|
72
|
48 |
|
return $bridge; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: