SlimProvider   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

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