1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\SaxulumWebProfiler\Provider; |
4
|
|
|
|
5
|
|
|
use Saxulum\DoctrineMongodbOdmManagerRegistry\Doctrine\ManagerRegistry; |
6
|
|
|
use Saxulum\SaxulumWebProfiler\DataCollector\DoctrineDataCollector; |
7
|
|
|
use Saxulum\SaxulumWebProfiler\DataCollector\DoctrineMongoDbDataCollector; |
8
|
|
|
use Saxulum\SaxulumWebProfiler\DataCollector\DoctrineMongoDbStandardDataCollector; |
9
|
|
|
use Saxulum\SaxulumWebProfiler\Logger\DbalLogger; |
10
|
|
|
use Saxulum\SaxulumWebProfiler\Logger\DoctrineMongoDbAggregateLogger; |
11
|
|
|
use Saxulum\SaxulumWebProfiler\Logger\DoctrineMongoDbLogger; |
12
|
|
|
use Saxulum\SaxulumWebProfiler\Twig\DoctrineExtension; |
13
|
|
|
use Silex\Application; |
14
|
|
|
use Silex\ServiceProviderInterface; |
15
|
|
|
|
16
|
|
|
class SaxulumWebProfilerProvider implements ServiceProviderInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function boot(Application $app) {} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function register(Application $app) |
27
|
|
|
{ |
28
|
|
|
if (isset($app['profiler'])) { |
29
|
|
|
|
30
|
|
|
$app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig) { |
31
|
|
|
$twig->addExtension(new DoctrineExtension()); |
32
|
|
|
|
33
|
|
|
return $twig; |
34
|
|
|
})); |
35
|
|
|
|
36
|
|
|
$app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', |
37
|
|
|
function (\Twig_Loader_Filesystem $twigLoaderFilesystem) { |
38
|
|
|
$twigLoaderFilesystem->addPath(dirname(__DIR__). '/Resources/views', 'SaxulumWebProfilerProvider'); |
39
|
|
|
|
40
|
|
|
return $twigLoaderFilesystem; |
41
|
|
|
} |
42
|
|
|
)); |
43
|
|
|
|
44
|
|
|
$app['data_collectors'] = $app->extend('data_collectors', |
45
|
|
|
$app->share(function(array $collectors) use ($app) { |
46
|
|
|
if(isset($app['doctrine'])) { |
47
|
|
|
$app['saxulum.orm.logger'] = function ($app) { |
48
|
|
|
return new DbalLogger($app['monolog'], $app['stopwatch']); |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
$collectors['db'] = $app->share(function ($app) { |
52
|
|
|
$dataCollector = new DoctrineDataCollector($app['doctrine']); |
53
|
|
|
foreach ($app['doctrine']->getConnectionNames() as $name) { |
54
|
|
|
$logger = $app['saxulum.orm.logger']; |
55
|
|
|
$app['doctrine']->getConnection($name)->getConfiguration()->setSQLLogger($logger); |
56
|
|
|
$dataCollector->addLogger($name, $logger); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $dataCollector; |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if(isset($app['doctrine_mongodb'])) { |
64
|
|
|
$app['saxulum.webprofiler.mongodb.odm.logger.batchthreshold'] = 4; |
65
|
|
|
|
66
|
|
|
$app['saxulum.mongodb.odm.logger'] = $app->share(function ($app) { |
67
|
|
|
$logger = new DoctrineMongoDbLogger($app['monolog']); |
68
|
|
|
$logger->setBatchInsertThreshold($app['saxulum.mongodb.odm.logger.batchthreshold']); |
69
|
|
|
|
70
|
|
|
return $logger; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$app['saxulum.mongodb.odm.loggers'] = $app->share(function ($app) { |
74
|
|
|
$loggers = array(); |
75
|
|
|
$loggers[] = $app['saxulum.mongodb.odm.logger']; |
76
|
|
|
$loggers[] = $app['saxulum.mongodb.odm.datacolletor']; |
77
|
|
|
|
78
|
|
|
return $loggers; |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$app['saxulum.mongodb.odm.aggregatelogger'] = $app->share(function ($app) { |
82
|
|
|
$logger = new DoctrineMongoDbAggregateLogger($app['saxulum.mongodb.odm.loggers']); |
83
|
|
|
|
84
|
|
|
return $logger; |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
$aggregatedLogger = $app['saxulum.mongodb.odm.aggregatelogger']; |
88
|
|
|
$app['doctrine_mongodb'] = $app->extend('doctrine_mongodb', |
89
|
|
|
$app->share(function(ManagerRegistry $registry) use($aggregatedLogger) { |
90
|
|
|
foreach ($registry->getConnectionNames() as $name) { |
91
|
|
|
$registry->getConnection($name)->getConfiguration()->setLoggerCallable(array($aggregatedLogger, 'logQuery')); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
$collectors['mongodb'] = $app->share(function ($app) { |
97
|
|
|
$dataCollector = new DoctrineMongoDbDataCollector(); |
98
|
|
|
$dataCollector->setBatchInsertThreshold($app['saxulum.mongodb.odm.logger.batchthreshold']); |
99
|
|
|
|
100
|
|
|
return $dataCollector; |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $collectors; |
105
|
|
|
} |
106
|
|
|
)); |
107
|
|
|
|
108
|
|
|
$app['data_collector.templates'] = $app->extend('data_collector.templates', |
109
|
|
|
$app->share(function(array $dataCollectorTemplates) use ($app) { |
110
|
|
|
if(isset($app['doctrine'])) { |
111
|
|
|
$dataCollectorTemplates[] = array('db', '@SaxulumWebProfilerProvider/Collector/db.html.twig'); |
112
|
|
|
} |
113
|
|
|
if(isset($app['doctrine_mongodb'])) { |
114
|
|
|
$dataCollectorTemplates[] = array('mongodb', '@SaxulumWebProfilerProvider/Collector/mongodb.html.twig'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $dataCollectorTemplates; |
118
|
|
|
} |
119
|
|
|
)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|