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

TwigModule::configure()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 8.8571
c 5
b 0
f 1
cc 2
eloc 21
nc 2
nop 0
crap 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 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 ($this->paths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
49 12
            $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
50 12
            $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
51
        }
52
53
        $this
54 20
            ->bind(Twig_LoaderInterface::class)
55 20
            ->annotatedWith('twig_loader')
56 20
            ->toConstructor(
57 20
                Twig_Loader_Filesystem::class,
58 20
                'paths=Madapaja\TwigModule\Annotation\TwigPaths'
59
            );
60
61
        $this
62 20
            ->bind(Twig_Environment::class)
63 20
            ->annotatedWith('original')
64 20
            ->toProvider(OriginalTwigEnvironmentProvider::class)
65 20
            ->in(Scope::SINGLETON);
66
67
        $this
68 20
            ->bind(Twig_Environment::class)
69 20
            ->toProvider(TwigEnvironmentProvider::class)
70 20
            ->in(Scope::SINGLETON);
71 20
    }
72
}
73