|
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 BEAR\Sunday\Extension\Error\ErrorInterface; |
|
11
|
|
|
use Madapaja\TwigModule\Annotation\TwigLoader; |
|
12
|
|
|
use Madapaja\TwigModule\Annotation\TwigOptions; |
|
13
|
|
|
use Madapaja\TwigModule\Annotation\TwigPaths; |
|
14
|
|
|
use Ray\Di\AbstractModule; |
|
15
|
|
|
use Ray\Di\Scope; |
|
16
|
|
|
use Twig_Environment; |
|
17
|
|
|
use Twig_Loader_Filesystem; |
|
18
|
|
|
use Twig_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
|
|
|
* |
|
36
|
|
|
* @see http://twig.sensiolabs.org/api/master/Twig_Environment.html |
|
37
|
|
|
*/ |
|
38
|
23 |
|
public function __construct($paths = [], $options = [], AbstractModule $module = null) |
|
39
|
|
|
{ |
|
40
|
23 |
|
$this->paths = $paths; |
|
41
|
23 |
|
$this->options = $options; |
|
42
|
23 |
|
parent::__construct($module); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
23 |
|
protected function configure() |
|
49
|
|
|
{ |
|
50
|
23 |
|
$this->bindRender(); |
|
51
|
23 |
|
$this->bindTwigLoader(); |
|
52
|
23 |
|
$this->bindTwigEnvironment(); |
|
53
|
|
|
$this->bindTwigPaths(); |
|
54
|
|
|
$this->bindTwigOptions(); |
|
55
|
|
|
$this->bind(RenderInterface::class)->annotatedWith('error_page')->to(ErrorPagerRenderer::class); |
|
56
|
|
|
$this->bind(ErrorInterface::class)->to(TwigErrorHandler::class); |
|
57
|
|
|
$this->bind(TwigErrorPage::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
23 |
|
private function bindRender() |
|
61
|
|
|
{ |
|
62
|
23 |
|
$this->bind(RenderInterface::class) |
|
63
|
23 |
|
->to(TwigRenderer::class) |
|
64
|
23 |
|
->in(Scope::SINGLETON); |
|
65
|
23 |
|
} |
|
66
|
|
|
|
|
67
|
23 |
|
private function bindTwigLoader() |
|
68
|
|
|
{ |
|
69
|
|
|
$this |
|
70
|
23 |
|
->bind(Twig_LoaderInterface::class) |
|
71
|
23 |
|
->annotatedWith(TwigLoader::class) |
|
72
|
23 |
|
->toConstructor( |
|
73
|
23 |
|
Twig_Loader_Filesystem::class, |
|
74
|
23 |
|
'paths=Madapaja\TwigModule\Annotation\TwigPaths' |
|
75
|
|
|
); |
|
76
|
23 |
|
} |
|
77
|
|
|
|
|
78
|
23 |
|
private function bindTwigEnvironment() |
|
79
|
|
|
{ |
|
80
|
|
|
$this |
|
81
|
23 |
|
->bind(Twig_Environment::class) |
|
82
|
23 |
|
->annotatedWith('original') |
|
83
|
23 |
|
->toConstructor( |
|
84
|
23 |
|
Twig_Environment::class, |
|
85
|
|
|
[ |
|
86
|
23 |
|
'loader' => TwigLoader::class, |
|
87
|
|
|
'options' => TwigOptions::class |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$this |
|
92
|
|
|
->bind(Twig_Environment::class) |
|
93
|
|
|
->toConstructor( |
|
94
|
|
|
Twig_Environment::class, |
|
95
|
|
|
[ |
|
96
|
|
|
'loader' => TwigLoader::class, |
|
97
|
|
|
'options' => TwigOptions::class |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function bindTwigPaths() |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->isNotEmpty($this->paths)) { |
|
105
|
|
|
$this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths); |
|
106
|
|
|
|
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
$this->bind()->annotatedWith(TwigPaths::class)->toProvider(AppPathProvider::class); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function bindTwigOptions() |
|
113
|
|
|
{ |
|
114
|
|
|
if ($this->isNotEmpty($this->options)) { |
|
115
|
|
|
$this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options); |
|
116
|
|
|
|
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
$this->bind()->annotatedWith(TwigOptions::class)->toProvider(OptionProvider::class); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function isNotEmpty($var) |
|
123
|
|
|
{ |
|
124
|
|
|
return \is_array($var) && ! empty($var); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.