Completed
Push — master ( 2edeb4...8f93c5 )
by Albert
03:04
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\Controller\AbstractController;
6
use Albert221\Blog\Controller\AbstractWidgetController;
7
use Albert221\Blog\Pagination\PaginatorBuilder;
8
use Albert221\Blog\Repository\CategoryRepositoryInterface;
9
use Albert221\Blog\Repository\PostRepositoryInterface;
10
use Albert221\Blog\Repository\TagRepositoryInterface;
11
use Albert221\Blog\Widget\TwigWidgetExtension;
12
use League\Container\ServiceProvider\AbstractServiceProvider;
13
use League\Container\ServiceProvider\BootableServiceProviderInterface;
14
use Twig_Environment;
15
use Twig_Loader_Filesystem;
16
17
class TwigServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $provides = [
23
        'twig',
24
        'twigWidgetExtension',
25
        'paginatorBuilder'
26
    ];
27
28
    public function boot()
29
    {
30
        $this->getContainer()->inflector(AbstractController::class)
31
            ->invokeMethod('setTwig', ['twig']);
32
33
        $this->getContainer()->inflector(AbstractWidgetController::class)
34
            ->invokeMethod('setWidgetExtension', ['twigWidgetExtension']);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function register()
41
    {
42
        $this->getContainer()->share('twig', function () {
43
            $loader = new Twig_Loader_Filesystem(
44
                $this->getContainer()->get('baseDir').'/views'
45
            );
46
47
            $config = [
48
                'autoescape' => false
49
            ];
50
51
            if (!$this->getContainer()->get('config')['debug']) {
52
                $config['cache'] = sprintf('%s/cache/twig', $this->getContainer()->get('baseDir'));
53
            }
54
55
            return new Twig_Environment($loader, $config);
56
        });
57
        
58
        $this->getContainer()->share('twigWidgetExtension', function () {
59
            return new TwigWidgetExtension(
60
                $this->getContainer()->get('twig'),
61
                $this->getContainer()->get(PostRepositoryInterface::class),
62
                $this->getContainer()->get(CategoryRepositoryInterface::class),
63
                $this->getContainer()->get(TagRepositoryInterface::class)
64
            );
65
        });
66
        
67
        $this->getContainer()->share('paginatorBuilder', function () {
68
            return new PaginatorBuilder(
69
                $this->getContainer()->get('twig'),
70
                $this->getContainer()->get('config')['pagination']['perPage']
71
            );
72
        });
73
    }
74
}
75