8000 feature #24289 [FrameworkBundle][HttpKernel] Reset profiler (derrabus) · symfony/symfony@86684f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86684f1

Browse files
committed
feature #24289 [FrameworkBundle][HttpKernel] Reset profiler (derrabus)
This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle][HttpKernel] Reset profiler | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #18244 | License | MIT | Doc PR | N/A This PR adds the ability to reset the profiler between requests. Furthermore, the profiler service has been tagged with the new `kernel.reset` tag from #24155. For this, I had to readd the ability to define multiple reset methods for a service. Note: This PR requires twigphp/Twig#2560. Commits ------- 8c39bf7 Reset profiler.
2 parents d6b68e1 + 8c39bf7 commit 86684f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+426
-37
lines changed

UPGRADE-3.4.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Debug
6363

6464
* Support for stacked errors in the `ErrorHandler` is deprecated and will be removed in Symfony 4.0.
6565

66+
EventDispatcher
67+
---------------
68+
69+
* Implementing `TraceableEventDispatcherInterface` without the `reset()` method
70+
is deprecated and will be unsupported in 4.0.
71+
6672
Filesystem
6773
----------
6874

@@ -270,6 +276,10 @@ HttpKernel
270276

271277
* The `Symfony\Component\HttpKernel\Config\EnvParametersResource` class has been deprecated and will be removed in 4.0.
272278

279+
* Implementing `DataCollectorInterface` without a `reset()` method has been deprecated and will be unsupported in 4.0.
280+
281+
* Implementing `DebugLoggerInterface` without a `clear()` method has been deprecated and will be unsupported in 4.0.
282+
273283
* The `ChainCacheClearer::add()` method has been deprecated and will be removed in 4.0,
274284
inject the list of clearers as a constructor argument instead.
275285

@@ -320,11 +330,11 @@ SecurityBundle
320330

321331
* Deprecated the HTTP digest authentication: `HttpDigestFactory` will be removed in 4.0.
322332
Use another authentication system like `http_basic` instead.
323-
333+
324334
* Deprecated setting the `switch_user.stateless` option to false when the firewall is `stateless`.
325335
Setting it to false will have no effect in 4.0.
326336

327-
* Not configuring explicitly the provider on a firewall is ambiguous when there is more than one registered provider.
337+
* Not configuring explicitly the provider on a firewall is ambiguous when there is more than one registered provider.
328338
Using the first configured provider is deprecated since 3.4 and will throw an exception on 4.0.
329339
Explicitly configure the provider to use on your firewalls.
330340

UPGRADE-4.0.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ EventDispatcher
192192
* The `ContainerAwareEventDispatcher` class has been removed.
193193
Use `EventDispatcher` with closure factories instead.
194194

195+
* The `reset()` method has been added to `TraceableEventDispatcherInterface`.
196+
195197
ExpressionLanguage
196198
------------------
197199

@@ -611,6 +613,10 @@ HttpKernel
611613

612614
* The `Symfony\Component\HttpKernel\Config\EnvParametersResource` class has been removed.
613615

616+
* The `reset()` method has been added to `Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface`.
617+
618+
* The `clear()` method has been added to `Symfony\Component\HttpKernel\Log\DebugLoggerInterface`.
619+
614620
* The `ChainCacheClearer::add()` method has been removed,
615621
inject the list of clearers as a constructor argument instead.
616622

@@ -693,10 +699,10 @@ SecurityBundle
693699

694700
* Removed the HTTP digest authentication system. The `HttpDigestFactory` class
695701
has been removed. Use another authentication system like `http_basic` instead.
696-
702+
697703
* The `switch_user.stateless` option is now always true if the firewall is stateless.
698704

699-
* Not configuring explicitly the provider on a firewall is ambiguous when there is more than one registered provider.
705+
* Not configuring explicitly the provider on a firewall is ambiguous when there is more than one registered provider.
700706
The first configured provider is not used anymore and an exception is thrown instead.
701707
Explicitly configure the provider to use on your firewalls.
702708

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-xml": "*",
2121
"doctrine/common": "~2.4",
2222
"fig/link-util": "^1.0",
23-
"twig/twig": "~1.34|~2.4",
23+
"twig/twig": "^1.35|^2.4.4",
2424
"psr/cache": "~1.0",
2525
"psr/container": "^1.0",
2626
"psr/link": "^1.0",

src/Symfony/Bridge/Doctrine/DataCollector/DoctrineDataCollector.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class DoctrineDataCollector extends DataCollector
2828
private $registry;
2929
private $connections;
3030
private $managers;
31+
32+
/**
33+
* @var DebugStack[]
34+
*/
3135
private $loggers = array();
3236

3337
public function __construct(ManagerRegistry $registry)
@@ -65,6 +69,16 @@ public function collect(Request $request, Response $response, \Exception $except
6569
);
6670
}
6771

72+
public function reset()
73+
{
74+
$this->data = array();
75+
76+
foreach ($this->loggers as $logger) {
77+
$logger->queries = array();
78+
$logger->currentQuery = 0;
79+
}
80+
}
81+
6882
public function getManagers()
6983
{
7084
return $this->data['managers'];

src/Symfony/Bridge/Doctrine/Tests/DataCollector/DoctrineDataCollectorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ public function testCollectQueryWithNoParams()
101101
$this->assertTrue($collectedQueries['default'][1]['explainable']);
102102
}
103103

104+
public function testReset()
105+
{
106+
$queries = array(
107+
array('sql' => 'SELECT * FROM table1', 'params' => array(), 'types' => array(), 'executionMS' => 1),
108+
);
109+
$c = $this->createCollector($queries);
110+
$c->collect(new Request(), new Response());
111+
112+
$c->reset();
113+
$c->collect(new Request(), new Response());
114+
115+
$this->assertEquals(array('default' => array()), $c->getQueries());
116+
}
117+
104118
/**
105119
* @dataProvider paramProvider
106120
*/

src/Symfony/Bridge/Monolog/Logger.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public function countErrors()
4545
return 0;
4646
}
4747

48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function clear()
52+
{
53+
if (($logger = $this->getDebugLogger()) && method_exists($logger, 'clear')) {
54+
$logger->clear();
55+
}
56+
}
57+
4858
/**
4959
* Returns a DebugLoggerInterface instance if one is registered with this logger.
5060
*

src/Symfony/Bridge/Monolog/Processor/DebugProcessor.php

Lines changed: 9 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ public function countErrors()
5555
{
5656
return $this->errorCount;
5757
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function clear()
63+
{
64+
$this->records = array();
65+
$this->errorCount = 0;
66+
}
5867
}

src/Symfony/Bridge/Monolog/Tests/LoggerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,17 @@ public function testGetLogsWithDebugProcessor2()
128128
$this->assertEquals('test', $record['message']);
129129
$this->assertEquals(Logger::INFO, $record['priority']);
130130
}
131+
132+
public function testClear()
133+
{
134+
$handler = new TestHandler();
135+
$logger = new Logger('test', array($handler));
136+
$logger->pushProcessor(new DebugProcessor());
137+
138+
$logger->addInfo('test');
139+
$logger->clear();
140+
141+
$this->assertEmpty($logger->getLogs());
142+
$this->assertSame(0, $logger->countErrors());
143+
}
131144
}

src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public function collect(Request $request, Response $response, \Exception $except
4444
{
4545
}
4646

47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function reset()
51+
{
52+
$this->profile->reset();
53+
$this->computed = null;
54+
$this->data = array();
55+
}
56+
4757
/**
4858
* {@inheritdoc}
4959
*/

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^5.5.9|>=7.0.8",
20-
"twig/twig": "~1.34|~2.4"
20+
"twig/twig": "^1.35|^2.4.4"
2121
},
2222
"require-dev": {
2323
"fig/link-util": "^1.0",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,9 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
609609
}
610610
}
611611

612-
if (!$config['collect']) {
613-
$container->getDefinition('profiler')->addMethodCall('disable', array());
614-
}
612+
$container->getDefinition('profiler')
613+
->addArgument($config['collect'])
614+
->addTag('kernel.reset', array('method' => 'reset'));
615615
}
616616

617617
/**

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ public function collect(Request $request, Response $response, \Exception $except
203203
}
204204
}
205205

206+
/**
207+
* {@inheritdoc}
208+
*/
209+
public function reset()
210+
{
211+
$this->data = array();
212+
}
213+
206214
public function lateCollect()
207215
{
208216
$this->data = $this->cloneVar($this->data);

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public function collect(Request $request, Response $response, \Exception $except
5353
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
5454
}
5555

56+
public function reset()
57+
{
58+
$this->data = array();
59+
foreach ($this->instances as $instance) {
60+
// Calling getCalls() will clear the calls.
61+
$instance->getCalls();
62+
}
63+
}
64+
5665
public function lateCollect()
5766
{
5867
$this->data = $this->cloneVar($this->data);

src/Symfony/Component/EventDispatcher/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* Implementing `TraceableEventDispatcherInterface` without the `reset()` method has been deprecated.
8+
49
3.3.0
510
-----
611

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ public function getNotCalledListeners()
212212
return $notCalled;
213213
}
214214

215+
public function reset()
216+
{
217+
$this->called = array();
218+
}
219+
215220
/**
216221
* Proxies all method calls to the original event dispatcher.
217222
*

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcherInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
/**
1717
* @author Fabien Potencier <fabien@symfony.com>
18+
*
19+
* @method reset() Resets the trace.
1820
*/
1921
interface TraceableEventDispatcherInterface extends EventDispatcherInterface
2022
{

src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ public function testGetCalledListeners()
124124
$this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
125125
}
126126

127+
public function testClearCalledListeners()
128+
{
129+
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
130+
$tdispatcher->addListener('foo', function () {}, 5);
131+
132+
$tdispatcher->dispatch('foo');
133+
$tdispatcher->reset();
134+
135+
$listeners = $tdispatcher->getNotCalledListeners();
136+
$this->assertArrayHasKey('stub', $listeners['foo.closure']);
137+
unset($listeners['foo.closure']['stub']);
138+
$this->assertEquals(array(), $tdispatcher->getCalledListeners());
139+
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
140+
}
141+
127142
public function testGetCalledListenersNested()
128143
{
129144
$tdispatcher = null;

src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@ class FormDataCollector extends DataCollector implements FormDataCollectorInterf
7272
public function __construct(FormDataExtractorInterface $dataExtractor)
7373
{
7474
$this->dataExtractor = $dataExtractor;
75-
$this->data = array(
76-
'forms' => array(),
77-
'forms_by_hash' => array(),
78-
'nb_errors' => 0,
79-
);
8075
$this->hasVarDumper = class_exists(ClassStub::class);
76+
77+
$this->reset();
8178
}
8279

8380
/**
@@ -87,6 +84,15 @@ public function collect(Request $request, Response $response, \Exception $except
8784
{
8885
}
8986

87+
public function reset()
88+
{
89+
$this->data = array(
90+
'forms' => array(),
91+
'forms_by_hash' => array(),
92+
'nb_errors' => 0,
93+
);
94+
}
95+
9096
/**
9197
* {@inheritdoc}
9298
*/

src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,36 @@ public function testCollectSubmittedDataExpandedFormsErrors()
695695
$this->assertFalse(isset($child21Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
696696
}
697697

698+
public function testReset()
699+
{
700+
$form = $this->createForm('my_form');
701+
702+
$this->dataExtractor->expects($this->any())
703+
->method('extractConfiguration')
704+
->will($this->returnValue(array()));
705+
$this->dataExtractor->expects($this->any())
706+
->method('extractDefaultData')
707+
->will($this->returnValue(array()));
708+
$this->dataExtractor->expects($this->any())
709+
->method('extractSubmittedData')
710+
->with($form)
711+
->will($this->returnValue(array('errors' => array('baz'))));
712+
713+
$this->dataCollector->buildPreliminaryFormTree($form);
714+
$this->dataCollector->collectSubmittedData($form);
715+
716+
$this->dataCollector->reset();
717+
718+
$this->assertSame(
719+
array(
720+
'forms' => array(),
721+
'forms_by_hash' => array(),
722+
'nb_errors' => 0,
723+
),
724+
$this->dataCollector->getData()
725+
);
726+
}
727+
698728
private function createForm($name)
699729
{
700730
$builder = new FormBuilder($name, null, $this->dispatcher, $this->factory);

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ CHANGELOG
1414
* deprecated the `ChainCacheClearer::add()` method
1515
* deprecated the `CacheaWarmerAggregate::add()` and `setWarmers()` methods
1616
* made `CacheWarmerAggregate` and `ChainCacheClearer` classes final
17-
17+
* added the possibility to reset the profiler to its initial state
18+
* deprecated data collectors without a `reset()` method
19+
* deprecated implementing `DebugLoggerInterface` without a `clear()` method
1820

1921
3.3.0
2022
-----

src/Symfony/Component/HttpKernel/DataCollector/AjaxDataCollector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function collect(Request $request, Response $response, \Exception $except
2626
// all collecting is done client side
2727
}
2828

29+
public function reset()
30+
{
31+
// all collecting is done client side
32+
}
33+
2934
public function getName()
3035
{
3136
return 'ajax';

0 commit comments

Comments
 (0)
0