15
15
use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
16
16
use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
17
17
use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
18
+ use Symfony \Component \HttpKernel \Profiler \Profile ;
18
19
use Symfony \Component \HttpKernel \Profiler \Profiler ;
19
20
use Symfony \Component \HttpFoundation \RequestMatcherInterface ;
20
21
@@ -32,6 +33,7 @@ class ProfilerListener
32
33
protected $ exception ;
33
34
protected $ children ;
34
35
protected $ requests ;
36
+ protected $ profiles ;
35
37
36
38
/**
37
39
* Constructor.
@@ -48,6 +50,7 @@ public function __construct(Profiler $profiler, RequestMatcherInterface $matcher
48
50
$ this ->onlyException = (Boolean ) $ onlyException ;
49
51
$ this ->onlyMasterRequests = (Boolean ) $ onlyMasterRequests ;
50
52
$ this ->children = new \SplObjectStorage ();
53
+ $ this ->profiles = array ();
51
54
}
52
55
53
56
/**
@@ -85,42 +88,62 @@ public function onKernelResponse(FilterResponseEvent $event)
85
88
return ;
86
89
}
87
90
91
+ $ request = $ event ->getRequest ();
88
92
$ exception = $ this ->exception ;
89
93
$ this ->exception = null ;
90
94
91
- if (null !== $ this ->matcher && !$ this ->matcher ->matches ($ event -> getRequest () )) {
95
+ if (null !== $ this ->matcher && !$ this ->matcher ->matches ($ request )) {
92
96
return ;
93
97
}
94
98
95
- if (!$ profile = $ this ->profiler ->collect ($ event ->getRequest (), $ event ->getResponse (), $ exception )) {
99
+ if (!$ profile = $ this ->profiler ->collect ($ request , $ event ->getResponse (), $ exception )) {
100
+ return ;
101
+ }
102
+
103
+ $ this ->profiles [] = $ profile ;
104
+
105
+ if (null !== $ exception ) {
106
+ foreach ($ this ->profiles as $ profile ) {
107
+ $ this ->profiler ->saveProfile ($ profile );
108
+ }
109
+
96
110
return ;
97
111
}
98
112
99
113
// keep the profile as the child of its parent
100
114
if (!$ master ) {
101
115
array_pop ($ this ->requests );
102
116
103
- $ parent = $ this ->requests [count ($ this ->requests ) - 1 ];
104
- if (!isset ($ this ->children [$ parent ])) {
105
- $ profiles = array ($ profile );
106
- } else {
107
- $ profiles = $ this ->children [$ parent ];
108
- $ profiles [] = $ profile ;
109
- }
110
-
117
+ $ parent = end ($ this ->requests );
118
+ $ profiles = isset ($ this ->children [$ parent ]) ? $ this ->children [$ parent ] : array ();
119
+ $ profiles [] = $ profile ;
111
120
$ this ->children [$ parent ] = $ profiles ;
112
121
}
113
122
114
- // store the profile and its children
115
- if (isset ($ this ->children [$ event ->getRequest ()])) {
116
- foreach ($ this ->children [$ event ->getRequest ()] as $ child ) {
123
+ if (isset ($ this ->children [$ request ])) {
124
+ foreach ($ this ->children [$ request ] as $ child ) {
117
125
$ child ->setParent ($ profile );
118
126
$ profile ->addChild ($ child );
119
- $ this ->profiler ->saveProfile ($ child );
120
127
}
121
- $ this ->children [$ event -> getRequest () ] = array ();
128
+ $ this ->children [$ request ] = array ();
122
129
}
123
130
131
+ if ($ master ) {
132
+ $ this ->saveProfiles ($ profile );
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Saves the profile hierarchy.
138
+ *
139
+ * @param Profile $profile The root pro
6AEB
file
140
+ */
141
+ private function saveProfiles (Profile $ profile )
142
+ {
124
143
$ this ->profiler ->saveProfile ($ profile );
144
+ foreach ($ profile ->getChildren () as $ profile ) {
145
+ $ this ->saveProfiles ($ profile );
146
+ }
125
147
}
126
148
}
149
+
0 commit comments