Completed
Push — master ( 2edeb4...8f93c5 )
by Albert
03:04
created

TwigServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
B register() 0 34 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