SilexPinbaProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 66 7
1
<?php
2
/**
3
 * @author Mikhail Dolgov <[email protected]>
4
 * @date   04.03.2016 15:27
5
 */
6
7
namespace SilexPinbaProvider;
8
9
10
use Doctrine\DBAL\Configuration;
11
use Intaro\PinbaBundle\Logger\DbalLogger;
12
use Pimple\Container;
13
use Pimple\ServiceProviderInterface;
14
use Silex\Application;
15
use SilexPinbaProvider\Twig\TimedTwigEnvironment;
16
17
class SilexPinbaProvider implements ServiceProviderInterface
18
{
19
20
    /**
21
     * Registers services on the given app.
22
     *
23
     * This method should only be used to configure services and parameters.
24
     * It should not get services.
25
     * @param Container $app
26
     */
27
    public function register(Container $app)
28
    {
29
30
        $app['intaro_pinba.script_name_configure.class']      = 'Intaro\PinbaBundle\EventListener\ScriptNameConfigureListener';
31
        $app['intaro_pinba.stopwatch.class']                  = 'Intaro\PinbaBundle\Stopwatch\Stopwatch';
32
        $app['intaro_pinba.templating.engine.twig.class']     = 'SilexPinbaProvider\Twig\TimedTwigEnvironment';
33
        $app['intaro_pinba.dbal.logger.class']                = 'Intaro\PinbaBundle\Logger\DbalLogger';
34
        $app['intaro_pinba.server.name']                      = 'localhost';
35
        $app['intaro_pinba.script_name_configure.enable']     = true;
36
37
38
        $app['intaro_pinba.script_name_configure.listener'] = function () use ($app) {
39
            return new $app['intaro_pinba.script_name_configure.class'];
40
        };
41
42
        $app['intaro_pinba.stopwatch'] = function () use ($app) {
43
            return new $app['intaro_pinba.stopwatch.class'];
44
        };
45
46
        $app['doctrine.dbal.logger'] = function () use ($app) {
47
            /**
48
             * @see \Intaro\PinbaBundle\Logger\DbalLogger
49
             */
50
            $className = $app['intaro_pinba.dbal.logger.class'];
51
            $host = isset($app["intaro_pinba.doctrine.database_host"]) ? $app["intaro_pinba.doctrine.database_host"] : $app['intaro_pinba.server.name'];
52
            return new $className($app["intaro_pinba.stopwatch"], $host);
53
        };
54
55
        $app['dbs.config'] = function ($app) {
56
            $app['dbs.options.initializer']();
57
58
            $configs = new Container();
59
            foreach ($app['dbs.options'] as $name => $options) {
60
                $configs[$name] = new Configuration();
61
62
                if (isset($app['logger']) && class_exists('Intaro\PinbaBundle\Logger\DbalLogger')) {
63
                    $configs[$name]->setSQLLogger($app['doctrine.dbal.logger']);
64
                }
65
            }
66
67
            return $configs;
68
        };
69
70
        $app['twig.environment_factory'] = $app->protect(function ($app) {
71
            $className = $app['intaro_pinba.templating.engine.twig.class'];
72
            $twig      = new $className($app['twig.loader'], $app['twig.options']);
73
            if($twig instanceof TimedTwigEnvironment) {
74
                $twig
75
                    ->setStopwatch( $app['intaro_pinba.stopwatch'])
76
                    ->setServerName($app['intaro_pinba.server.name'])
77
                ;
78
            }
79
            return $twig;
80
        });
81
82
83
        if (PHP_SAPI !== 'cli') {
84
            $app->extend('dispatcher', function ($dispatcher) use ($app) {
85
                /**
86
                 * @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface
87
                 */
88
                $dispatcher->addListener('kernel.request', array($app['intaro_pinba.script_name_configure.listener'], 'onRequest'));
89
                return $dispatcher;
90
            });
91
        }
92
    }
93
}