Completed
Pull Request — 1.x (#19)
by IWASAKI
04:50 queued 02:30
created

TwigModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
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 21
    public function __construct($paths = [], $options = [])
34
    {
35 21
        $this->paths = $paths;
36 21
        $this->options = $options;
37
38 21
        parent::__construct();
39 21
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 21
    protected function configure()
45
    {
46 21
        AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php');
47
48 21
        $this->bind(RenderInterface::class)->to(TwigRenderer::class)->in(Scope::SINGLETON);
49
50
        $this
51 21
            ->bind(Twig_LoaderInterface::class)
52 21
            ->annotatedWith('twig_loader')
53 21
            ->toConstructor(
54 21
                Twig_Loader_Filesystem::class,
55 21
                'paths=Madapaja\TwigModule\Annotation\TwigPaths'
56
            );
57
58
        $this
59 21
            ->bind(Twig_Environment::class)
60 21
            ->annotatedWith('original')
61 21
            ->toProvider(OriginalTwigEnvironmentProvider::class)
62 21
            ->in(Scope::SINGLETON);
63
64
        $this
65 21
            ->bind(Twig_Environment::class)
66 21
            ->toProvider(TwigEnvironmentProvider::class)
67 21
            ->in(Scope::SINGLETON);
68
69 21
        $this->bindOptionalParameters();
70 21
    }
71
72 21
    private function bindOptionalParameters()
73
    {
74 21
        if (is_array($this->paths) && !empty($this->paths)) {
75 13
            $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
76
        }
77
78 21
        if (is_array($this->options) && !empty($this->options)) {
79 1
            $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
80
        }
81 21
    }
82
}
83