Completed
Push — master ( 392874...829d42 )
by Albert
05:23
created

TwigServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Albert221\Blog\ServiceProvider;
4
5
use Albert221\Blog\Pagination\PaginatorBuilder;
6
use Albert221\Blog\Repository\CategoryRepositoryInterface;
7
use Albert221\Blog\Repository\PostRepositoryInterface;
8
use Albert221\Blog\Repository\SettingRepositoryInterface;
9
use Albert221\Blog\Repository\TagRepositoryInterface;
10
use Albert221\Blog\Widget\TwigWidgetExtension;
11
use League\Container\ServiceProvider\AbstractServiceProvider;
12
use Twig_Environment;
13
use Twig_Loader_Filesystem;
14
15
class TwigServiceProvider extends AbstractServiceProvider
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $provides = [
21
        'twig',
22
        'twigWidgetExtension',
23
        'paginatorBuilder'
24
    ];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function register()
30
    {
31
        $this->getContainer()->share('twig', function () {
32
            $loader = new Twig_Loader_Filesystem(
33
                $this->getContainer()->get('baseDir').'/views'
34
            );
35
36
            $config = [
37
                'autoescape' => false
38
            ];
39
40
            if (!$this->getContainer()->get('config')['debug']) {
41
                $config['cache'] = sprintf('%s/cache/twig', $this->getContainer()->get('baseDir'));
42
            }
43
44
            $twig = new Twig_Environment($loader, $config);
45
            $twig->addGlobal('settings', $this->getContainer()->get(SettingRepositoryInterface::class));
46
47
            return $twig;
48
        });
49
        
50
        $this->getContainer()->share('twigWidgetExtension', function () {
51
            return new TwigWidgetExtension(
52
                $this->getContainer()->get('twig'),
53
                $this->getContainer()->get(PostRepositoryInterface::class),
54
                $this->getContainer()->get(CategoryRepositoryInterface::class),
55
                $this->getContainer()->get(TagRepositoryInterface::class)
56
            );
57
        });
58
        
59
        $this->getContainer()->share('paginatorBuilder', function () {
60
            return new PaginatorBuilder(
61
                $this->getContainer()->get('twig'),
62
                $this->getContainer()->get('config')['pagination']['perPage']
63
            );
64
        });
65
    }
66
}
67