1515use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
1616use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
1717use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
18+ use Symfony \Component \HttpKernel \Profiler \Profile ;
1819use Symfony \Component \HttpKernel \Profiler \Profiler ;
1920use Symfony \Component \HttpFoundation \RequestMatcherInterface ;
2021
@@ -32,6 +33,7 @@ class ProfilerListener
3233 protected $ exception ;
3334 protected $ children ;
3435 protected $ requests ;
36+ protected $ profiles ;
3537
3638 /**
3739 * Constructor.
@@ -48,6 +50,7 @@ public function __construct(Profiler $profiler, RequestMatcherInterface $matcher
4850 $ this ->onlyException = (Boolean ) $ onlyException ;
4951 $ this ->onlyMasterRequests = (Boolean ) $ onlyMasterRequests ;
5052 $ this ->children = new \SplObjectStorage ();
53+ $ this ->profiles = array ();
5154 }
5255
5356 /**
@@ -85,42 +88,62 @@ public function onKernelResponse(FilterResponseEvent $event)
8588 return ;
8689 }
8790
91+ $ request = $ event ->getRequest ();
8892 $ exception = $ this ->exception ;
8993 $ this ->exception = null ;
9094
91- if (null !== $ this ->matcher && !$ this ->matcher ->matches ($ event -> getRequest () )) {
95+ if (null !== $ this ->matcher && !$ this ->matcher ->matches ($ request )) {
9296 return ;
9397 }
9498
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+
96110 return ;
97111 }
98112
99113 // keep the profile as the child of its parent
100114 if (!$ master ) {
101115 array_pop ($ this ->requests );
102116
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 ;
111120 $ this ->children [$ parent ] = $ profiles ;
112121 }
113122
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 ) {
117125 $ child ->setParent ($ profile );
118126 $ profile ->addChild ($ child );
119- $ this ->profiler ->saveProfile ($ child );
120127 }
121- $ this ->children [$ event -> getRequest () ] = array ();
128+ $ this ->children [$ request ] = array ();
122129 }
123130
131+ if ($ master ) {
132+ $ this ->saveProfiles ($ profile );
133+ }
134+ }
135+
136+ /**
137+ * Saves the profile hierarchy.
138+ *
139+ * @param Profile $profile The root profile
140+ */
141+ private function saveProfiles (Profile $ profile )
142+ {
124143 $ this ->profiler ->saveProfile ($ profile );
144+ foreach ($ profile ->getChildren () as $ profile ) {
145+ $ this ->saveProfiles ($ profile );
146+ }
125147 }
126148}
149+
0 commit comments