This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
forked from silexphp/Silex-WebProfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebProfilerServiceProvider.php
208 lines (170 loc) · 10.2 KB
/
WebProfilerServiceProvider.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Symfony\Bundle\WebProfilerBundle\Controller\ExceptionController;
use Symfony\Bundle\WebProfilerBundle\Controller\RouterController;
use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
use Symfony\Component\Form\Extension\DataCollector\FormDataExtractor;
use Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeFactoryDataCollectorProxy;
use Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\EventListener\ProfilerListener;
use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
use Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector;
use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Symfony\Component\HttpKernel\DataCollector\RouterDataCollector;
use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
use Symfony\Component\HttpKernel\DataCollector\EventDataCollector;
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Bridge\Twig\Extension\CodeExtension;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\ControllerProviderInterface;
use Silex\ServiceControllerResolver;
/**
* Symfony Web Profiler provider.
*
* @author Fabien Potencier <[email protected]>
*/
class WebProfilerServiceProvider implements ServiceProviderInterface, ControllerProviderInterface
{
public function register(Application $app)
{
$app['profiler.mount_prefix'] = '/_profiler';
$app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) {
$dispatcher = new TraceableEventDispatcher($dispatcher, $app['stopwatch'], $app['logger']);
return $dispatcher;
}));
$app['data_collector.templates'] = array(
array('config', '@WebProfiler/Collector/config.html.twig'),
array('request', '@WebProfiler/Collector/request.html.twig'),
array('exception', '@WebProfiler/Collector/exception.html.twig'),
array('events', '@WebProfiler/Collector/events.html.twig'),
array('logger', '@WebProfiler/Collector/logger.html.twig'),
array('time', '@WebProfiler/Collector/time.html.twig'),
array('router', '@WebProfiler/Collector/router.html.twig'),
array('memory', '@WebProfiler/Collector/memory.html.twig'),
array('form', '@WebProfiler/Collector/form.html.twig'),
);
$app['data_collectors'] = $app->share(function ($app) {
return array(
'config' => $app->share(function ($app) { return new ConfigDataCollector(); }),
'request' => $app->share(function ($app) { return new RequestDataCollector(); }),
'exception' => $app->share(function ($app) { return new ExceptionDataCollector(); }),
'events' => $app->share(function ($app) { return new EventDataCollector($app['dispatcher']); }),
'logger' => $app->share(function ($app) { return new LoggerDataCollector($app['logger']); }),
'time' => $app->share(function ($app) { return new TimeDataCollector(null, $app['stopwatch']); }),
'router' => $app->share(function ($app) { return new RouterDataCollector(); }),
'memory' => $app->share(function ($app) { return new MemoryDataCollector(); }),
);
});
if (isset($app['form.resolved_type_factory']) && class_exists('\Symfony\Component\Form\Extension\DataCollector\FormDataCollector')) {
$app['data_collectors.form.extractor'] = $app->share(function () { return new FormDataExtractor(); });
$app['data_collectors'] = $app->share($app->extend('data_collectors', function ($collectors, $app) {
$collectors['form'] = $app->share(function ($app) { return new FormDataCollector($app['data_collectors.form.extractor']); });
return $collectors;
}));
$app['form.resolved_type_factory'] = $app->share($app->extend('form.resolved_type_factory', function ($factory, $app) {
return new ResolvedTypeFactoryDataCollectorProxy($factory, $app['data_collectors']['form']($app));
}));
$app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function ($extensions, $app) {
$extensions[] = new DataCollectorTypeExtension($app['data_collectors']['form']($app));
return $extensions;
}));
}
$app['web_profiler.controller.profiler'] = $app->share(function ($app) {
return new ProfilerController($app['url_generator'], $app['profiler'], $app['twig'], $app['data_collector.templates'], $app['web_profiler.debug_toolbar.position']);
});
$app['web_profiler.controller.router'] = $app->share(function ($app) {
return new RouterController($app['profiler'], $app['twig'], isset($app['url_matcher']) ? $app['url_matcher'] : null, $app['routes']);
});
$app['web_profiler.controller.exception'] = $app->share(function ($app) {
return new ExceptionController($app['profiler'], $app['twig'], $app['debug']);
});
$app['web_profiler.toolbar.listener'] = $app->share(function ($app) {
return new WebDebugToolbarListener($app['twig']);
});
$app['web_profiler.debug_toolbar.position'] = 'bottom';
$app['profiler'] = $app->share(function ($app) {
$profiler = new Profiler($app['profiler.storage'], $app['logger']);
foreach ($app['data_collectors'] as $collector) {
$profiler->add($collector($app));
}
return $profiler;
});
$app['profiler.storage'] = $app->share(function ($app) {
return new FileProfilerStorage('file:'.$app['profiler.cache_dir']);
});
$app['profiler.request_matcher'] = null;
$app['profiler.only_exceptions'] = false;
$app['profiler.only_master_requests'] = false;
$app['profiler.listener'] = $app->share(function ($app) {
return new ProfilerListener(
$app['profiler'],
$app['profiler.request_matcher'],
$app['profiler.only_exceptions'],
$app['profiler.only_master_requests']
);
});
$app['stopwatch'] = $app->share(function () {
return new Stopwatch();
});
$app['code.file_link_format'] = null;
$app['twig'] = $app->share($app->extend('twig', function ($twig, $app) {
$twig->addExtension(new CodeExtension($app['code.file_link_format'], '', $app['charset']));
return $twig;
}));
$app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', function ($loader, $app) {
$loader->addPath($app['profiler.templates_path'], 'WebProfiler');
return $loader;
}));
$app['profiler.templates_path'] = function () {
$r = new \ReflectionClass('Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener');
return dirname(dirname($r->getFileName())).'/Resources/views';
};
}
public function connect(Application $app)
{
if (!$app['resolver'] instanceof ServiceControllerResolver) {
// using RuntimeException crashes PHP?!
throw new \LogicException('You must enable the ServiceController service provider to be able to use the WebProfiler.');
}
$controllers = $app['controllers_factory'];
$controllers->get('/router/{token}', 'web_profiler.controller.router:panelAction')->bind('_profiler_router');
$controllers->get('/exception/{token}.css', 'web_profiler.controller.exception:cssAction')->bind('_profiler_exception_css');
$controllers->get('/exception/{token}', 'web_profiler.controller.exception:showAction')->bind('_profiler_exception');
$controllers->get('/search', 'web_profiler.controller.profiler:searchAction')->bind('_profiler_search');
$controllers->get('/search_bar', 'web_profiler.controller.profiler:searchBarAction')->bind('_profiler_search_bar');
$controllers->get('/purge', 'web_profiler.controller.profiler:purgeAction')->bind('_profiler_purge');
$controllers->get('/info/{about}', 'web_profiler.controller.profiler:infoAction')->bind('_profiler_info');
$controllers->get('/import', 'web_profiler.controller.profiler:importAction')->bind('_profiler_import');
$controllers->get('/export/{token}.txt', 'web_profiler.controller.profiler:exportAction')->bind('_profiler_export');
$controllers->get('/phpinfo', 'web_profiler.controller.profiler:phpinfoAction')->bind('_profiler_phpinfo');
$controllers->get('/{token}/search/results', 'web_profiler.controller.profiler:searchResultsAction')->bind('_profiler_search_results');
$controllers->get('/{token}', 'web_profiler.controller.profiler:panelAction')->bind('_profiler');
$controllers->get('/wdt/{token}', 'web_profiler.controller.profiler:toolbarAction')->bind('_wdt');
$controllers->get('/', 'web_profiler.controller.profiler:homeAction')->bind('_profiler_home');
return $controllers;
}
public function boot(Application $app)
{
$dispatcher = $app['dispatcher'];
$dispatcher->addSubscriber($app['profiler.listener']);
$dispatcher->addSubscriber($app['web_profiler.toolbar.listener']);
$dispatcher->addSubscriber($app['profiler']->get('request'));
$app->mount($app['profiler.mount_prefix'], $this->connect($app));
}
}