Skip to content

Commit d8a00a8

Browse files
author
Amrouche Hamza
committed
[WIP][WebProfilerBundle] add a way to limit ajax request
1 parent 0032368 commit d8a00a8

File tree

8 files changed

+29
-11
lines changed

8 files changed

+29
-11
lines changed

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ class ProfilerController
3434
private $templates;
3535
private $cspHandler;
3636
private $baseDir;
37+
private $maxAjaxRequests;
3738

38-
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, Environment $twig, array $templates, ContentSecurityPolicyHandler $cspHandler = null, string $baseDir = null)
39+
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, Environment $twig, array $templates, ContentSecurityPolicyHandler $cspHandler = null, string $baseDir = null, int $maxAjaxRequests = null)
3940
{
4041
$this->generator = $generator;
4142
$this->profiler = $profiler;
4243
$this->twig = $twig;
4344
$this->templates = $templates;
4445
$this->cspHandler = $cspHandler;
4546
$this->baseDir = $baseDir;
47+
$this->maxAjaxRequests = $maxAjaxRequests;
4648
}
4749

4850
/**
@@ -110,6 +112,7 @@ public function panelAction(Request $request, $token)
110112
'templates' => $this->getTemplateManager()->getNames($profile),
111113
'is_ajax' => $request->isXmlHttpRequest(),
112114
'profiler_markup_version' => 2, // 1 = original profiler, 2 = Symfony 2.8+ profiler
115+
'max_ajax_requests_in_toolbar' => $this->maxAjaxRequests,
113116
)), 200, array('Content-Type' => 'text/html'));
114117
}
115118

@@ -159,7 +162,8 @@ public function toolbarAction(Request $request, $token)
159162
'templates' => $this->getTemplateManager()->getNames($profile),
160163
'profiler_url' => $url,
161164
'token' => $token,
162-
'profiler_markup_version' => 2, // 1 = original toolbar, 2 = Symfony 2.8+ toolbar
165+
'profiler_markup_version' => 2, // 1 = original toolbar, 2 = Symfony 2.8+ toolbar,
166+
'max_ajax_requests_in_toolbar' => $this->maxAjaxRequests,
163167
));
164168
}
165169

@@ -213,6 +217,7 @@ public function searchBarAction(Request $request)
213217
'end' => $end,
214218
'limit' => $limit,
215219
'request' => $request,
220+
'max_ajax_requests_in_toolbar' => $this->maxAjaxRequests,
216221
)),
217222
200,
218223
array('Content-Type' => 'text/html')
@@ -264,6 +269,7 @@ public function searchResultsAction(Request $request, $token)
264269
'end' => $end,
265270
'limit' => $limit,
266271
'panel' => null,
272+
'max_ajax_requests_in_toolbar' => $this->maxAjaxRequests,
267273
)), 200, array('Content-Type' => 'text/html'));
268274
}
269275

@@ -317,6 +323,7 @@ public function searchAction(Request $request)
317323
'start' => $start,
318324
'end' => $end,
319325
'limit' => $limit,
326+
'max_ajax_requests_in_toolbar' => $this->maxAjaxRequests,
320327
)), 302, array('Content-Type' => 'text/html'));
321328
}
322329

src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public function getConfigTreeBuilder()
3737
$rootNode
3838
->children()
3939
->booleanNode('toolbar')->defaultFalse()->end()
40+
->scalarNode('max_ajax_requests_in_toolbar')->defaultNull()->end()
4041
->booleanNode('intercept_redirects')->defaultFalse()->end()
41-
->scalarNode('excluded_ajax_paths')->defaultValue('^/(app(_[\\w]+)?\\.php/)?_wdt')->end()
42-
->end()
42+
->scalarNode('excluded_ajax_paths')->defaultValue('^/(app(_[\\w]+)?\\.php/)?_wdt')->end()
43+
->end()
4344
;
4445

4546
return $treeBuilder;

src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function load(array $configs, ContainerBuilder $container)
4444

4545
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
4646
$loader->load('profiler.xml');
47-
47+
$container->setParameter('web_profiler.max_ajax_requests_in_toolbar', $config['max_ajax_requests_in_toolbar']);
4848
if ($config['toolbar'] || $config['intercept_redirects']) {
4949
$loader->load('toolbar.xml');
5050
$container->getDefinition('web_profiler.debug_toolbar')->replaceArgument(4, $config['excluded_ajax_paths']);

src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<argument>%data_collector.templates%</argument>
1515
<argument type="service" id="web_profiler.csp.handler" />
1616
<argument>%kernel.project_dir%</argument>
17+
<argument>%web_profiler.max_ajax_requests_in_toolbar%</argument>
1718
</service>
1819

1920
<service id="web_profiler.controller.router" class="Symfony\Bundle\WebProfilerBundle\Controller\RouterController" public="true">

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@
9292
if (!requestCounter) {
9393
return;
9494
}
95+
96+
var maxAjaxNumber = {{ max_ajax_requests_in_toolbar }};
97+
98+
if (null !== maxAjaxNumber && maxAjaxNumber < requestStack.length ) {
99+
return;
100+
}
101+
95102
requestCounter.textContent = requestStack.length;
96103
97104
var infoSpan = document.querySelector(".sf-toolbar-ajax-info");

src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ProfilerControllerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public function testSearchResult($withCsp)
173173
'limit' => 2,
174174
'panel' => null,
175175
'request' => $request,
176+
'max_ajax_requests_in_toolbar' => null,
176177
)));
177178

178179
$response = $controller->searchResultsAction($request, 'empty');

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public function testConfigTree($options, $results)
3232
public function getDebugModes()
3333
{
3434
return array(
35-
array(array(), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt')),
36-
array(array('intercept_redirects' => true), array('intercept_redirects' => true, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt')),
37-
array(array('intercept_redirects' => false), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt')),
38-
array(array('toolbar' => true), array('intercept_redirects' => false, 'toolbar' => true, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt')),
39-
array(array('excluded_ajax_paths' => 'test'), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => 'test')),
35+
array(array(), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt', 'max_ajax_requests_in_toolbar' => null)),
36+
array(array('max_ajax_requests_in_toolbar' => 10), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt', 'max_ajax_requests_in_toolbar' => 10)),
37+
array(array('intercept_redirects' => true), array('intercept_redirects' => true, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt', 'max_ajax_requests_in_toolbar' => null)),
38+
array(array('intercept_redirects' => false), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt', 'max_ajax_requests_in_toolbar' => null)),
39+
array(array('toolbar' => true), array('intercept_redirects' => false, 'toolbar' => true, 'excluded_ajax_paths' => '^/(app(_[\\w]+)?\\.php/)?_wdt', 'max_ajax_requests_in_toolbar' => null)),
40+
array(array('excluded_ajax_paths' => 'test'), array('intercept_redirects' => false, 'toolbar' => false, 'excluded_ajax_paths' => 'test', 'max_ajax_requests_in_toolbar' => null)),
4041
);
4142
}
4243
}

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testDefaultConfig($debug)
101101
public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listenerInjected, $listenerEnabled)
102102
{
103103
$extension = new WebProfilerExtension();
104-
$extension->load(array(array('toolbar' => $toolbarEnabled, 'intercept_redirects' => $interceptRedirects)), $this->container);
104+
$extension->load(array(array('toolbar' => $toolbarEnabled, 'intercept_redirects' => $interceptRedirects, 'max_ajax_requests_in_toolbar' => 1)), $this->container);
105105

106106
$this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar'));
107107

0 commit comments

Comments
 (0)