Completed
Pull Request — 2.x (#29)
by Akihito
01:35
created

TwigModule::bindRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
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 BEAR\Sunday\Extension\Error\ErrorInterface;
11
use Madapaja\TwigModule\Annotation\TwigLoader;
12
use Madapaja\TwigModule\Annotation\TwigOptions;
13
use Madapaja\TwigModule\Annotation\TwigPaths;
14
use Ray\Di\AbstractModule;
15
use Ray\Di\Scope;
16
use Twig_Environment;
17
use Twig_Loader_Filesystem;
18
use Twig_LoaderInterface;
19
20
class TwigModule extends AbstractModule
21
{
22
    /**
23
     * @var array
24
     */
25
    private $paths;
26
27
    /**
28
     * @var array
29
     */
30
    private $options;
31
32
    /**
33
     * @param array $paths   Twig template paths
34
     * @param array $options Twig_Environment options
35
     *
36
     * @see http://twig.sensiolabs.org/api/master/Twig_Environment.html
37
     */
38 23
    public function __construct($paths = [], $options = [], AbstractModule $module = null)
39
    {
40 23
        $this->paths = $paths;
41 23
        $this->options = $options;
42 23
        parent::__construct($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by parameter $module on line 38 can also be of type object<Ray\Di\AbstractModule>; however, Ray\Di\AbstractModule::__construct() does only seem to accept null|object<self>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 23
    protected function configure()
49
    {
50 23
        $this->bindRender();
51 23
        $this->bindTwigLoader();
52 23
        $this->bindTwigEnvironment();
53
        $this->bindTwigPaths();
54
        $this->bindTwigOptions();
55
        $this->bind(RenderInterface::class)->annotatedWith('error_page')->to(ErrorPagerRenderer::class);
56
        $this->bind(ErrorInterface::class)->to(TwigErrorHandler::class);
57
        $this->bind(TwigErrorPage::class);
58
    }
59
60 23
    private function bindRender()
61
    {
62 23
        $this->bind(RenderInterface::class)
63 23
             ->to(TwigRenderer::class)
64 23
             ->in(Scope::SINGLETON);
65 23
    }
66
67 23
    private function bindTwigLoader()
68
    {
69
        $this
70 23
            ->bind(Twig_LoaderInterface::class)
71 23
            ->annotatedWith(TwigLoader::class)
72 23
            ->toConstructor(
73 23
                Twig_Loader_Filesystem::class,
74 23
                'paths=Madapaja\TwigModule\Annotation\TwigPaths'
75
            );
76 23
    }
77
78 23
    private function bindTwigEnvironment()
79
    {
80
        $this
81 23
            ->bind(Twig_Environment::class)
82 23
            ->annotatedWith('original')
83 23
            ->toConstructor(
84 23
                Twig_Environment::class,
85
                [
86 23
                    'loader' => TwigLoader::class,
87
                    'options' => TwigOptions::class
88
                ]
89
            );
90
91
        $this
92
            ->bind(Twig_Environment::class)
93
            ->toConstructor(
94
                Twig_Environment::class,
95
                [
96
                    'loader' => TwigLoader::class,
97
                    'options' => TwigOptions::class
98
                ]
99
            );
100
    }
101
102
    private function bindTwigPaths()
103
    {
104
        if ($this->isNotEmpty($this->paths)) {
105
            $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
106
107
            return;
108
        }
109
        $this->bind()->annotatedWith(TwigPaths::class)->toProvider(AppPathProvider::class);
110
    }
111
112
    private function bindTwigOptions()
113
    {
114
        if ($this->isNotEmpty($this->options)) {
115
            $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
116
117
            return;
118
        }
119
        $this->bind()->annotatedWith(TwigOptions::class)->toProvider(OptionProvider::class);
120
    }
121
122
    private function isNotEmpty($var)
123
    {
124
        return \is_array($var) && ! empty($var);
125
    }
126
}
127