2
2
3
3
namespace Sourceability \ConsoleToolbarBundle \Behat \Listener ;
4
4
5
+ use function array_filter ;
5
6
use Behat \Behat \EventDispatcher \Event \ScenarioTested ;
6
7
use Behat \Behat \EventDispatcher \Event \StepTested ;
8
+ use function max ;
7
9
use Sourceability \ConsoleToolbarBundle \Console \ProfilerToolbarRenderer ;
8
10
use Sourceability \ConsoleToolbarBundle \Profiler \RecentProfileLoader ;
11
+ use function sprintf ;
9
12
use Symfony \Component \Console \Output \ConsoleOutput ;
10
13
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
11
14
use Symfony \Component \HttpKernel \KernelInterface ;
12
15
use Symfony \Component \HttpKernel \Profiler \Profile ;
13
16
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
14
17
use Symfony \Component \Routing \RequestContext ;
15
18
use Symfony \Component \Routing \Router ;
16
- use function array_filter ;
17
- use function count ;
18
- use function in_array ;
19
- use function max ;
20
- use function sprintf ;
21
19
use function time ;
22
20
23
21
class ProfilerToolbarListener implements EventSubscriberInterface
24
22
{
25
- /**
26
- * {@inheritdoc}
27
- */
28
- public static function getSubscribedEvents ()
29
- {
30
- return [
31
- ScenarioTested::BEFORE => ['beforeScenario ' , 10 ],
32
- StepTested::BEFORE => ['beforeAfterStep ' , 10 ],
33
- StepTested::AFTER => ['beforeAfterStep ' , 10 ],
34
- ScenarioTested::AFTER => ['afterScenario ' , 10 ],
35
- ];
36
- }
37
-
38
23
private RecentProfileLoader $ recentProfileLoader ;
24
+
39
25
private ProfilerToolbarRenderer $ profilerToolbarRenderer ;
26
+
40
27
private Router $ router ;
28
+
41
29
private RequestContext $ originalRouterContext ;
42
30
43
31
private ?int $ beforeScenarioTimestamp = null ;
32
+
44
33
private ?int $ lastProfileTimestamp = null ;
45
34
46
35
/**
@@ -50,20 +39,36 @@ public static function getSubscribedEvents()
50
39
51
40
public function __construct (KernelInterface $ kernel )
52
41
{
53
- $ recentProfileLoader = $ kernel ->getContainer ()->get ('sourceability.console_toolbar.profiler.recent_profile_loader ' );
54
- $ profilerToolbarRenderer = $ kernel ->getContainer ()->get ('sourceability.console_toolbar.console.profiler_toolbar_renderer ' );
55
- $ router = $ kernel ->getContainer ()->get ('router ' );
56
-
57
- assert ($ recentProfileLoader instanceof RecentProfileLoader);
58
- assert ($ profilerToolbarRenderer instanceof ProfilerToolbarRenderer);
59
- assert ($ router instanceof Router);
42
+ $ recentProfileLoader = $ kernel ->getContainer ()
43
+ ->get ('sourceability.console_toolbar.profiler.recent_profile_loader ' )
44
+ ;
45
+ $ profilerToolbarRenderer = $ kernel ->getContainer ()
46
+ ->get ('sourceability.console_toolbar.console.profiler_toolbar_renderer ' )
47
+ ;
48
+ $ router = $ kernel ->getContainer ()
49
+ ->get ('router ' )
50
+ ;
51
+
52
+ \assert ($ recentProfileLoader instanceof RecentProfileLoader);
53
+ \assert ($ profilerToolbarRenderer instanceof ProfilerToolbarRenderer);
54
+ \assert ($ router instanceof Router);
60
55
61
56
$ this ->recentProfileLoader = $ recentProfileLoader ;
62
57
$ this ->profilerToolbarRenderer = $ profilerToolbarRenderer ;
63
58
$ this ->router = $ router ;
64
59
$ this ->originalRouterContext = clone $ this ->router ->getContext (); // clone is important here, we're making a copy
65
60
}
66
61
62
+ public static function getSubscribedEvents ()
63
+ {
64
+ return [
65
+ ScenarioTested::BEFORE => ['beforeScenario ' , 10 ],
66
+ StepTested::BEFORE => ['beforeAfterStep ' , 10 ],
67
+ StepTested::AFTER => ['beforeAfterStep ' , 10 ],
68
+ ScenarioTested::AFTER => ['afterScenario ' , 10 ],
69
+ ];
70
+ }
71
+
67
72
public function beforeScenario (ScenarioTested $ event ): void
68
73
{
69
74
$ this ->beforeScenarioTimestamp = time ();
@@ -77,18 +82,15 @@ public function beforeAfterStep(StepTested $event): void
77
82
78
83
$ profiles = array_filter (
79
84
$ profiles ,
80
- fn (Profile $ newProfile ) => !in_array ($ newProfile ->getToken (), $ this ->profileTokensShown , true )
85
+ fn (Profile $ newProfile ) => !\ in_array ($ newProfile ->getToken (), $ this ->profileTokensShown , true )
81
86
);
82
87
83
- if (count ($ profiles ) > 0 ) {
88
+ if (\ count ($ profiles ) > 0 ) {
84
89
$ this ->profilerToolbarRenderer ->render (new ConsoleOutput (), $ profiles );
85
90
}
86
91
87
92
foreach ($ profiles as $ profile ) {
88
- $ this ->lastProfileTimestamp = max (
89
- $ this ->lastProfileTimestamp ?? 0 ,
90
- $ profile ->getTime ()
91
- );
93
+ $ this ->lastProfileTimestamp = max ($ this ->lastProfileTimestamp ?? 0 , $ profile ->getTime ());
92
94
$ this ->profileTokensShown [] = $ profile ->getToken ();
93
95
}
94
96
}
@@ -110,19 +112,16 @@ public function afterScenario(ScenarioTested $event): void
110
112
[
111
113
'start ' => $ from ,
112
114
'end ' => $ to ,
113
- 'limit ' => 100 , // required otherwise list has no items
115
+ 'limit ' => 100 ,
116
+ // required otherwise list has no items
114
117
],
115
118
UrlGeneratorInterface::ABSOLUTE_URL
116
119
);
117
120
});
118
121
119
122
$ output = new ConsoleOutput ();
120
123
$ output ->writeln (
121
- sprintf (
122
- '%d profiles collected, <href=%s>Open profile list</> ' ,
123
- $ profileCount ,
124
- $ profilerUrl
125
- )
124
+ sprintf ('%d profiles collected, <href=%s>Open profile list</> ' , $ profileCount , $ profilerUrl )
126
125
);
127
126
}
128
127
0 commit comments