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

TwigServiceProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 2
c 4
b 0
f 3
lcom 0
cbo 6
dl 0
loc 52
ccs 0
cts 31
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 37 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