TemplateServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 39
rs 10
wmc 3
lcom 0
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 26 3
1
<?php
2
3
namespace Ps2alerts\Api\ServiceProvider;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use Twig_Loader_Filesystem;
7
use Twig_Environment;
8
use Twig_Extension_Debug;
9
use Twig_SimpleFilter;
10
use Cocur\Slugify\Bridge\Twig\SlugifyExtension;
11
use Cocur\Slugify\Slugify;
12
13
class TemplateServiceProvider extends AbstractServiceProvider
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $provides = [
19
        'Twig_Environment'
20
    ];
21
22
    /**
23
     * Register function required by ServiceProvider contract
24
     */
25
    public function register()
26
    {
27
        $config = $this->getContainer()->get('config');
28
        $globals = [
29
            'base_url'  => $config['base_url'],
30
            'asset_url' => $config['base_url'] . '/assets',
31
            'env'       => $config['environment']
32
        ];
33
34
        // Register the singleton with the container
35
        $this->getContainer()->share('Twig_Environment', function () use ($globals, $config) {
36
            $loader = new Twig_Loader_Filesystem(__DIR__ . '/../../template');
37
            $twig   = new Twig_Environment($loader, [
38
                'cache' => false,
39
                'debug' => $config['environment'] === 'development' ? true : false
40
            ]);
41
42
            // Add Globals
43
            foreach ($globals as $key => $val) {
44
                $twig->addGlobal($key, $val);
45
            }
46
            $twig->addExtension(new SlugifyExtension(Slugify::create(null, array('lowercase' => false))));
47
48
            return $twig;
49
        });
50
    }
51
}
52