This repository has been archived by the owner on Nov 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProfilerServiceProvider.php
55 lines (48 loc) · 2.36 KB
/
ProfilerServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace Rafal\ProfilerServiceProvider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Rafal\ProfilerServiceProvider\Twig\ProfilerExtension;
use Symfony\Component\HttpFoundation\Cookie;
class ProfilerServiceProvider implements ServiceProviderInterface {
public function register(Application $app)
{
if (!isset($app['profiler.data_url'])) {
$app['profiler.data_url'] = '/__fetch_profiler_data';
}
if (!isset($app['profiler.cookie_name'])) {
$app['profiler.cookie_name'] = 'webprofiler';
}
$app->get($app['profiler.data_url'], function() use($app){
$data = unserialize(urldecode($app['request']->cookies->get($app['profiler.cookie_name'])));
return $app['twig']->render('__profiler-data.twig', array(
'data' => $data
));
});
$loader = new \Twig_Loader_Filesystem(__DIR__.'/Resources/views/');
$app['twig.loader']->addLoader($loader);
$app['dispatcher']->addListener('silex.after', function($event) use($app) {
$data = array();
$request = $event->getRequest();
$response = $event->getResponse();
$data['request'] = array(
'method' => $request->getMethod(),
'uri' => $request->getRequestUri(),
'format' => $request->getRequestFormat(),
'accept' => $request->headers->get('accept'),
'accept-charset' => $request->headers->get('accept-charset'),
'accept-encoding' => $request->headers->get('accept-encoding'),
'accept-language' => $request->headers->get('accept-language'),
'connection' => $request->headers->get('connection'),
'host' => $request->headers->get('host'),
'user-agent' => $request->headers->get('user-agent'),
);
$data['response'] = array(
'code' => $response->getStatusCode(),
'cache-control' => $response->headers->get('cache-control')
);
$event->getResponse()->headers->setCookie(new Cookie($app['profiler.cookie_name'], urlencode(serialize($data))));
});
$app['twig']->addExtension(new ProfilerExtension($app['twig']));
}
}