|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Madapaja.TwigModule package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Madapaja\TwigModule; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\Resource\RenderInterface; |
|
10
|
|
|
use Madapaja\TwigModule\Annotation\TwigLoader; |
|
11
|
|
|
use Madapaja\TwigModule\Annotation\TwigOptions; |
|
12
|
|
|
use Madapaja\TwigModule\Annotation\TwigPaths; |
|
13
|
|
|
use Madapaja\TwigModule\Annotation\TwigRedirectPath; |
|
14
|
|
|
use Ray\Di\AbstractModule; |
|
15
|
|
|
use Ray\Di\Scope; |
|
16
|
|
|
use Twig\Environment; |
|
17
|
|
|
use Twig\Loader\FilesystemLoader; |
|
18
|
|
|
use Twig\Loader\LoaderInterface; |
|
19
|
|
|
|
|
20
|
|
|
class TwigModule extends AbstractModule |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $paths; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $options; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $paths Twig template paths |
|
34
|
|
|
* @param array $options Twig_Environment options |
|
35
|
|
|
* @param AbstractModule|null $module |
|
36
|
|
|
* |
|
37
|
|
|
* @see http://twig.sensiolabs.org/api/master/Twig_Environment.html |
|
38
|
23 |
|
*/ |
|
39
|
|
|
public function __construct($paths = [], $options = [], AbstractModule $module = null) |
|
40
|
23 |
|
{ |
|
41
|
23 |
|
$this->paths = $paths; |
|
42
|
23 |
|
$this->options = $options; |
|
43
|
23 |
|
parent::__construct($module); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
23 |
|
*/ |
|
49
|
|
|
protected function configure() |
|
50
|
23 |
|
{ |
|
51
|
23 |
|
$this->bindRender(); |
|
52
|
23 |
|
$this->bindTwigLoader(); |
|
53
|
23 |
|
$this->bindTwigEnvironment(); |
|
54
|
23 |
|
$this->bindTwigPaths(); |
|
55
|
23 |
|
$this->bindTwigOptions(); |
|
56
|
23 |
|
$this->bindTwigRedirectPath(); |
|
57
|
23 |
|
} |
|
58
|
23 |
|
|
|
59
|
|
|
private function bindRender() |
|
60
|
23 |
|
{ |
|
61
|
|
|
$this->bind(RenderInterface::class) |
|
62
|
23 |
|
->to(TwigRenderer::class) |
|
63
|
23 |
|
->in(Scope::SINGLETON); |
|
64
|
23 |
|
} |
|
65
|
23 |
|
|
|
66
|
|
|
private function bindTwigLoader() |
|
67
|
23 |
|
{ |
|
68
|
|
|
$this |
|
69
|
|
|
->bind(LoaderInterface::class) |
|
70
|
23 |
|
->annotatedWith(TwigLoader::class) |
|
71
|
23 |
|
->toConstructor( |
|
72
|
23 |
|
FilesystemLoader::class, |
|
73
|
23 |
|
'paths=Madapaja\TwigModule\Annotation\TwigPaths' |
|
74
|
23 |
|
); |
|
75
|
|
|
} |
|
76
|
23 |
|
|
|
77
|
|
|
private function bindTwigEnvironment() |
|
78
|
23 |
|
{ |
|
79
|
|
|
$this |
|
80
|
|
|
->bind(Environment::class) |
|
81
|
23 |
|
->annotatedWith('original') |
|
82
|
23 |
|
->toConstructor( |
|
83
|
23 |
|
Environment::class, |
|
84
|
23 |
|
[ |
|
85
|
|
|
'loader' => TwigLoader::class, |
|
86
|
23 |
|
'options' => TwigOptions::class |
|
87
|
|
|
] |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$this |
|
91
|
|
|
->bind(Environment::class) |
|
92
|
23 |
|
->toConstructor( |
|
93
|
23 |
|
Environment::class, |
|
94
|
23 |
|
[ |
|
95
|
|
|
'loader' => TwigLoader::class, |
|
96
|
23 |
|
'options' => TwigOptions::class |
|
97
|
|
|
] |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
23 |
|
|
|
101
|
|
|
private function bindTwigPaths() |
|
102
|
23 |
|
{ |
|
103
|
|
|
if ($this->isNotEmpty($this->paths)) { |
|
104
|
23 |
|
$this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths); |
|
105
|
13 |
|
|
|
106
|
|
|
return; |
|
107
|
13 |
|
} |
|
108
|
|
|
$this->bind()->annotatedWith(TwigPaths::class)->toProvider(AppPathProvider::class); |
|
109
|
10 |
|
} |
|
110
|
10 |
|
|
|
111
|
|
|
private function bindTwigOptions() |
|
112
|
23 |
|
{ |
|
113
|
|
|
if ($this->isNotEmpty($this->options)) { |
|
114
|
23 |
|
$this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options); |
|
115
|
1 |
|
|
|
116
|
|
|
return; |
|
117
|
1 |
|
} |
|
118
|
|
|
$this->bind()->annotatedWith(TwigOptions::class)->toProvider(OptionProvider::class); |
|
119
|
22 |
|
} |
|
120
|
22 |
|
|
|
121
|
|
|
private function bindTwigRedirectPath() |
|
122
|
23 |
|
{ |
|
123
|
|
|
$this->bind()->annotatedWith(TwigRedirectPath::class)->toInstance('/redirect/redirect.html.twig'); |
|
124
|
23 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function isNotEmpty($var) |
|
127
|
|
|
{ |
|
128
|
|
|
return \is_array($var) && ! empty($var); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.