22
33namespace PHPPM \Bootstraps ;
44
5+ use PHPPM \Utils ;
56use Symfony \Component \HttpFoundation \Request ;
67
78/**
@@ -59,6 +60,7 @@ public function getApplication()
5960 $ request = new Request ();
6061 $ request ->setMethod (Request::METHOD_HEAD );
6162 $ app ->handle ($ request );
63+ $ this ->postHandle ($ app );
6264
6365 return $ app ;
6466 }
@@ -71,14 +73,7 @@ public function getApplication()
7173 public function preHandle ($ app )
7274 {
7375 //resets Kernels startTime, so Symfony can correctly calculate the execution time
74- $ func = \Closure::bind (
75- function () {
76- $ this ->startTime = microtime (true );
77- },
78- $ app ,
79- 'AppKernel '
80- );
81- $ func ($ app );
76+ Utils::hijackProperty ($ app , 'startTime ' , microtime (true ));
8277 }
8378
8479 /**
@@ -88,15 +83,84 @@ function () {
8883 */
8984 public function postHandle ($ app )
9085 {
86+ $ container = $ app ->getContainer ();
87+
9188 //resets stopwatch, so it can correctly calculate the execution time
92- if ($ app -> getContainer () ->has ('debug.stopwatch ' )) {
93- $ app -> getContainer () ->get ('debug.stopwatch ' )->__construct ();
89+ if ($ container ->has ('debug.stopwatch ' )) {
90+ $ container ->get ('debug.stopwatch ' )->__construct ();
9491 }
9592
96- if ($ app ->getContainer ()->has ('profiler ' )) {
93+ //reset all profiler stuff currently supported
94+ if ($ container ->has ('profiler ' )) {
95+ $ profiler = $ container ->get ('profiler ' );
96+
9797 // since Symfony does not reset Profiler::disable() calls after each request, we need to do it,
9898 // so the profiler bar is visible after the second request as well.
99- $ app ->getContainer ()->get ('profiler ' )->enable ();
99+ $ profiler ->enable ();
100+
101+ //PropelLogger
102+ if ($ container ->has ('propel.logger ' )) {
103+ $ propelLogger = $ container ->get ('propel.logger ' );
104+ Utils::hijackProperty ($ propelLogger , 'queries ' , []);
105+ }
106+
107+ //Doctrine
108+ //Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector
109+ if ($ profiler ->has ('db ' )) {
110+ Utils::bindAndCall (function () {
111+ //$logger: \Doctrine\DBAL\Logging\DebugStack
112+ foreach ($ this ->loggers as $ logger ){
113+ Utils::hijackProperty ($ logger , 'queries ' , []);
114+ }
115+ }, $ profiler ->get ('db ' ), null , 'Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector ' );
116+ }
117+
118+ //ExceptionDataCollector
119+ if ($ profiler ->has ('events ' )) {
120+ Utils::hijackProperty ($ profiler ->get ('events ' ), 'data ' , array (
121+ 'called_listeners ' => array (),
122+ 'not_called_listeners ' => array (),
123+ ));
124+ }
125+
126+ //TwigDataCollector
127+ if ($ profiler ->has ('twig ' )) {
128+ Utils::bindAndCall (function () {
129+ Utils::hijackProperty ($ this ->profile , 'profiles ' , []);
130+ }, $ profiler ->get ('twig ' ));
131+ }
132+
133+ //Logger
134+ if ($ container ->has ('logger ' )) {
135+ $ logger = $ container ->get ('logger ' );
136+ Utils::bindAndCall (function () {
137+ if ($ debugLogger = $ this ->getDebugLogger ()) {
138+ //DebugLogger
139+ Utils::hijackProperty ($ debugLogger , 'records ' , []);
140+ }
141+ }, $ logger );
142+ }
143+
144+ //SwiftMailer logger
145+ //Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector
146+ if ($ container ->hasParameter ('swiftmailer.mailers ' )) {
147+ $ mailers = $ container ->getParameter ('swiftmailer.mailers ' );
148+ foreach ($ mailers as $ name => $ mailer ) {
149+ $ loggerName = sprintf ('swiftmailer.mailer.%s.plugin.messagelogger ' , $ name );
150+ if ($ container ->has ($ loggerName )) {
151+ /** @var \Swift_Plugins_MessageLogger $logger */
152+ $ logger = $ container ->get ($ loggerName );
153+ $ logger ->clear ();
154+ }
155+ }
156+ }
157+
158+ //Symfony\Bridge\Swiftmailer\DataCollector\MessageDataCollector
159+ if ($ container ->has ('swiftmailer.plugin.messagelogger ' )) {
160+ $ logger = $ container ->get ('swiftmailer.plugin.messagelogger ' );
161+ $ logger ->clear ();
162+ }
100163 }
164+
101165 }
102166}
0 commit comments