Completed
Push — 1.x ( c3d43d...7da29a )
by IWASAKI
03:11
created

TwigModule::isNotEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
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 Doctrine\Common\Annotations\AnnotationRegistry;
11
use Madapaja\TwigModule\Annotation\TwigOptions;
12
use Madapaja\TwigModule\Annotation\TwigPaths;
13
use Ray\Di\AbstractModule;
14
use Ray\Di\Scope;
15
use Twig_Environment;
16
use Twig_Loader_Filesystem;
17
use Twig_LoaderInterface;
18
19
class TwigModule extends AbstractModule
20
{
21
    /**
22
     * @var array
23
     */
24
    private $paths;
25
26
    /**
27
     * @var array
28
     */
29
    private $options;
30
31
    /**
32
     * @param array $paths   Twig template paths
33
     * @param array $options Twig_Environment options
34
     *
35
     * @see http://twig.sensiolabs.org/api/master/Twig_Environment.html
36
     */
37 22
    public function __construct($paths = [], $options = [])
38
    {
39 22
        $this->paths = $paths;
40 22
        $this->options = $options;
41
42 22
        parent::__construct();
43 22
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 22
    protected function configure()
49
    {
50 22
        AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php');
51
52 22
        $this->bindRender();
53 22
        $this->bindTwigLoader();
54 22
        $this->bindTwigEnvironment();
55 22
        $this->bindTwigPaths();
56 22
        $this->bindTwigOptions();
57 22
    }
58
59 22
    private function bindRender()
60
    {
61 22
        $this->bind(RenderInterface::class)
62 22
             ->to(TwigRenderer::class)
63 22
             ->in(Scope::SINGLETON);
64 22
    }
65
66 22
    private function bindTwigLoader()
67
    {
68
        $this
69 22
            ->bind(Twig_LoaderInterface::class)
70 22
            ->annotatedWith('twig_loader')
71 22
            ->toConstructor(
72 22
                Twig_Loader_Filesystem::class,
73 22
                'paths=Madapaja\TwigModule\Annotation\TwigPaths'
74
            );
75 22
    }
76
77 22
    private function bindTwigEnvironment()
78
    {
79
        $this
80 22
            ->bind(Twig_Environment::class)
81 22
            ->annotatedWith('original')
82 22
            ->toProvider(OriginalTwigEnvironmentProvider::class)
83 22
            ->in(Scope::SINGLETON);
84
85
        $this
86 22
            ->bind(Twig_Environment::class)
87 22
            ->toProvider(TwigEnvironmentProvider::class)
88 22
            ->in(Scope::SINGLETON);
89 22
    }
90
91 22
    private function bindTwigPaths()
92
    {
93 22
        if ($this->isNotEmpty($this->paths)) {
94 13
            $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
95
        }
96 22
    }
97
98 22
    private function bindTwigOptions()
99
    {
100 22
        if ($this->isNotEmpty($this->options)) {
101 1
            $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
102
        }
103 22
    }
104
105 22
    private function isNotEmpty($var)
106
    {
107 22
        return is_array($var) && ! empty($var);
108
    }
109
}
110