Completed
Push — master ( 8affb2...358096 )
by Mikhail
06:38
created

SilexPinbaProvider::boot()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 9.2
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\Api\BootableProviderInterface;
15
use Silex\Application;
16
use SilexPinbaProvider\Twig\TimedTwigEnvironment;
17
18
class SilexPinbaProvider implements ServiceProviderInterface, BootableProviderInterface
19
{
20
21
    /**
22
     * Registers services on the given app.
23
     *
24
     * This method should only be used to configure services and parameters.
25
     * It should not get services.
26
     * @param Container $app
27
     */
28
    public function register(Container $app)
29
    {
30
31
        $app['intaro_pinba.script_name_configure.class']      = 'Intaro\PinbaBundle\EventListener\ScriptNameConfigureListener';
32
        $app['intaro_pinba.stopwatch.class']                  = 'Intaro\PinbaBundle\Stopwatch\Stopwatch';
33
        $app['intaro_pinba.templating.engine.twig.class']     = 'SilexPinbaProvider\Twig\TimedTwigEnvironment';
34
        $app['intaro_pinba.dbal.logger.class']                = 'Intaro\PinbaBundle\Logger\DbalLogger';
35
        $app['intaro_pinba.server.name']                      = 'localhost';
36
        $app['intaro_pinba.script_name_configure.enable']     = true;
37
38
39
        $app['intaro_pinba.script_name_configure.listener'] = function () use ($app) {
40
            return new $app['intaro_pinba.script_name_configure.class'];
41
        };
42
43
        $app['intaro_pinba.stopwatch'] = function () use ($app) {
44
            return new $app['intaro_pinba.stopwatch.class'];
45
        };
46
47
        $app['doctrine.dbal.logger'] = function () use ($app) {
48
            /**
49
             * @see \Intaro\PinbaBundle\Logger\DbalLogger
50
             */
51
            $className = $app['intaro_pinba.dbal.logger.class'];
52
            $host = isset($app["intaro_pinba.doctrine.database_host"]) ? $app["intaro_pinba.doctrine.database_host"] : $app['intaro_pinba.server.name'];
53
            return new $className($app["intaro_pinba.stopwatch"], $host);
54
        };
55
56
        $app['dbs.config'] = function ($app) {
57
            $app['dbs.options.initializer']();
58
59
            $configs = new Container();
60
            foreach ($app['dbs.options'] as $name => $options) {
61
                $configs[$name] = new Configuration();
62
63
                if (isset($app['logger']) && class_exists('Intaro\PinbaBundle\Logger\DbalLogger')) {
64
                    $configs[$name]->setSQLLogger($app['doctrine.dbal.logger']);
65
                }
66
            }
67
68
            return $configs;
69
        };
70
71
        $app['twig.environment_factory'] = $app->protect(function ($app) {
72
            $className = $app['intaro_pinba.templating.engine.twig.class'];
73
            $twig      = new $className($app['twig.loader'], $app['twig.options']);
74
            if($twig instanceof TimedTwigEnvironment) {
75
                $twig
76
                    ->setStopwatch( $app['intaro_pinba.stopwatch'])
77
                    ->setServerName($app['intaro_pinba.server.name'])
78
                ;
79
            }
80
            return $twig;
81
        });
82
    }
83
84
    /**
85
     * Bootstraps the application.
86
     *
87
     * This method is called after all services are registered
88
     * and should be used for "dynamic" configuration (whenever
89
     * a service must be requested).
90
     * @param Application $app
91
     */
92
    public function boot(Application $app)
93
    {
94
        if (!function_exists('pinba_script_name_set') || PHP_SAPI === 'cli' || !$app['intaro_pinba.script_name_configure.enable']) {
95
            return;
96
        }
97
        /**
98
         * @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface
99
         */
100
        $dispatcher = $app['dispatcher'];
101
        $dispatcher->addListener('kernel.request', array($app['intaro_pinba.script_name_configure.listener'], 'onRequest'));
102
103
    }
104
105
}