|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Madapaja\TwigModule; |
|
4
|
|
|
|
|
5
|
|
|
use BEAR\Resource\RenderInterface; |
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
7
|
|
|
use Madapaja\TwigModule\Annotation\TwigOptions; |
|
8
|
|
|
use Madapaja\TwigModule\Annotation\TwigPaths; |
|
9
|
|
|
use Ray\Di\AbstractModule; |
|
10
|
|
|
use Ray\Di\Scope; |
|
11
|
|
|
use Twig_Environment; |
|
12
|
|
|
use Twig_Loader_Filesystem; |
|
13
|
|
|
use Twig_LoaderInterface; |
|
14
|
|
|
|
|
15
|
|
|
class TwigModule extends AbstractModule |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $paths; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $options; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array $paths Twig template paths |
|
29
|
|
|
* @param array $options Twig_Environment options |
|
30
|
|
|
* |
|
31
|
|
|
* @see http://twig.sensiolabs.org/api/master/Twig_Environment.html |
|
32
|
|
|
*/ |
|
33
|
20 |
|
public function __construct($paths = [], $options = []) |
|
34
|
|
|
{ |
|
35
|
20 |
|
$this->paths = $paths; |
|
36
|
20 |
|
$this->options = $options; |
|
37
|
20 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
20 |
|
protected function configure() |
|
43
|
|
|
{ |
|
44
|
20 |
|
AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php'); |
|
45
|
|
|
|
|
46
|
20 |
|
$this->bind(RenderInterface::class)->to(TwigRenderer::class)->in(Scope::SINGLETON); |
|
47
|
|
|
|
|
48
|
20 |
|
if (is_array($this->paths) && !empty($this->paths)) { |
|
49
|
12 |
|
$this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
20 |
|
if (is_array($this->options) && !empty($this->options)) { |
|
53
|
|
|
$this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this |
|
57
|
20 |
|
->bind(Twig_LoaderInterface::class) |
|
58
|
20 |
|
->annotatedWith('twig_loader') |
|
59
|
20 |
|
->toConstructor( |
|
60
|
20 |
|
Twig_Loader_Filesystem::class, |
|
61
|
20 |
|
'paths=Madapaja\TwigModule\Annotation\TwigPaths' |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this |
|
65
|
20 |
|
->bind(Twig_Environment::class) |
|
66
|
20 |
|
->annotatedWith('original') |
|
67
|
20 |
|
->toProvider(OriginalTwigEnvironmentProvider::class) |
|
68
|
20 |
|
->in(Scope::SINGLETON); |
|
69
|
|
|
|
|
70
|
|
|
$this |
|
71
|
20 |
|
->bind(Twig_Environment::class) |
|
72
|
20 |
|
->toProvider(TwigEnvironmentProvider::class) |
|
73
|
20 |
|
->in(Scope::SINGLETON); |
|
74
|
20 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|