Completed
Pull Request — master (#410)
by Elan
01:38
created

SlimProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 41 2
1
<?php
2
3
namespace XHGui\ServiceProvider;
4
5
use Pimple\Container;
6
use Pimple\ServiceProviderInterface;
7
use Slim\Slim as App;
8
use Slim\Views\Twig;
9
use XHGui\Twig\TwigExtension;
10
11
class SlimProvider implements ServiceProviderInterface
12
{
13
    /**
14
     * Create the Slim app
15
     */
16
    public function register(Container $c): void
17
    {
18
        $c['view'] = static function ($c) {
19
            // Configure Twig view for slim
20
            $view = new Twig();
21
22
            $view->twigTemplateDirs = [
23
                $c['app.template_dir'],
24
            ];
25
            $view->parserOptions = [
26
                'charset' => 'utf-8',
27
                'cache' => $c['app.cache_dir'],
28
                'auto_reload' => true,
29
                'strict_variables' => false,
30
                'autoescape' => 'html',
31
            ];
32
33
            // set global variables to templates
34
            $view->appendData([
35
                'date_format' => $c['config']['date.format'],
36
            ]);
37
38
            return $view;
39
        };
40
41
        $c['app'] = static function ($c) {
42
            if ($c['config']['timezone']) {
43
                date_default_timezone_set($c['config']['timezone']);
44
            }
45
46
            $app = new App($c['config']);
47
48
            $view = $c['view'];
49
            $view->parserExtensions = [
50
                new TwigExtension($app),
51
            ];
52
            $app->view($view);
53
54
            return $app;
55
        };
56
    }
57
}
58