Completed
Pull Request — 1.x (#19)
by IWASAKI
09:16 queued 05:43
created

TwigModule   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 11
c 7
b 1
f 1
lcom 1
cbo 3
dl 0
loc 91
ccs 44
cts 44
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 10 1
A bindRender() 0 6 1
A bindTwigLoader() 0 10 1
A bindTwigEnvironment() 0 13 1
A bindTwigPaths() 0 6 2
A bindTwigOptions() 0 6 2
A isNotEmpty() 0 4 2
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->bindRender();
49 21
        $this->bindTwigLoader();
50 21
        $this->bindTwigEnvironment();
51 21
        $this->bindTwigPaths();
52 21
        $this->bindTwigOptions();
53 21
    }
54
55 21
    private function bindRender()
56
    {
57 21
        $this->bind(RenderInterface::class)
58 21
             ->to(TwigRenderer::class)
59 21
             ->in(Scope::SINGLETON);
60 21
    }
61
62 21
    private function bindTwigLoader()
63
    {
64
        $this
65 21
            ->bind(Twig_LoaderInterface::class)
66 21
            ->annotatedWith('twig_loader')
67 21
            ->toConstructor(
68 21
                Twig_Loader_Filesystem::class,
69 21
                'paths=Madapaja\TwigModule\Annotation\TwigPaths'
70
            );
71 21
    }
72
73 21
    private function bindTwigEnvironment()
74
    {
75
        $this
76 21
            ->bind(Twig_Environment::class)
77 21
            ->annotatedWith('original')
78 21
            ->toProvider(OriginalTwigEnvironmentProvider::class)
79 21
            ->in(Scope::SINGLETON);
80
81
        $this
82 21
            ->bind(Twig_Environment::class)
83 21
            ->toProvider(TwigEnvironmentProvider::class)
84 21
            ->in(Scope::SINGLETON);
85 21
    }
86
87 21
    private function bindTwigPaths()
88
    {
89 21
        if ($this->isNotEmpty($this->paths)) {
90 13
            $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
91
        }
92 21
    }
93
94 21
    private function bindTwigOptions()
95
    {
96 21
        if ($this->isNotEmpty($this->options)) {
97 1
            $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
98
        }
99 21
    }
100
101 21
    private function isNotEmpty($var)
102
    {
103 21
        return is_array($var) && !empty($var);
104
    }
105
}
106