33namespace Xhgui \Profiler ;
44
55use Xhgui \Profiler \Exception \ProfilerException ;
6+ use Xhgui \Profiler \RequestContext \Provider \RequestContextProviderInterface ;
7+ use Xhgui \Profiler \RequestContext \RequestContextInterface ;
68use Xhgui \Profiler \Profilers \ProfilerInterface ;
79use Xhgui \Profiler \Saver \SaverInterface ;
810
@@ -36,6 +38,16 @@ final class Profiler
3638 */
3739 private $ profiler ;
3840
41+ /**
42+ * @var RequestContextProviderInterface|null
43+ */
44+ private $ requestContextProvider ;
45+
46+ /**
47+ * @var RequestContextInterface|null
48+ */
49+ private $ requestContext ;
50+
3951 /**
4052 * Simple state variable to hold the value of 'Is the profiler running or not?'
4153 *
@@ -99,12 +111,6 @@ public function enable($flags = null, $options = null)
99111 {
100112 $ this ->running = false ;
101113
102- // 'REQUEST_TIME_FLOAT' isn't available before 5.4.0
103- // https://www.php.net/manual/en/reserved.variables.server.php
104- if (!isset ($ _SERVER ['REQUEST_TIME_FLOAT ' ])) {
105- $ _SERVER ['REQUEST_TIME_FLOAT ' ] = microtime (true );
106- }
107-
108114 $ profiler = $ this ->getProfiler ();
109115 if (!$ profiler ) {
110116 throw new ProfilerException ('Unable to create profiler: No suitable profiler found ' );
@@ -122,7 +128,9 @@ public function enable($flags = null, $options = null)
122128 $ options = $ this ->config ['profiler.options ' ];
123129 }
124130
131+ $ context = $ this ->captureRequestContext ();
125132 $ profiler ->enable ($ flags , $ options );
133+ $ this ->requestContext = $ context ;
126134 $ this ->running = true ;
127135 }
128136
@@ -144,10 +152,18 @@ public function disable()
144152 throw new ProfilerException ('Unable to create profiler: No suitable profiler found ' );
145153 }
146154
147- $ profile = new ProfilingData ($ this ->config );
155+ $ context = $ this ->requestContext ;
156+ $ this ->requestContext = null ;
148157 $ this ->running = false ;
158+ $ data = $ profiler ->disable ();
159+
160+ if (!$ context instanceof RequestContextInterface) {
161+ throw new ProfilerException ('Unable to disable profiler: Request context is missing ' );
162+ }
163+
164+ $ profile = new ProfilingData ($ this ->config );
149165
150- return $ profile ->getProfilingData ($ profiler -> disable () );
166+ return $ profile ->getProfilingData ($ data , $ context );
151167 }
152168
153169 /**
@@ -277,4 +293,39 @@ private function getSaver()
277293
278294 return $ this ->saveHandler ?: null ;
279295 }
296+
297+ /**
298+ * @return RequestContextProviderInterface
299+ */
300+ private function getRequestContextProvider ()
301+ {
302+ if ($ this ->requestContextProvider === null ) {
303+ $ this ->requestContextProvider = RequestContextFactory::create ($ this ->config );
304+ }
305+
306+ return $ this ->requestContextProvider ;
307+ }
308+
309+ /**
310+ * @return RequestContextInterface
311+ */
312+ private function captureRequestContext ()
313+ {
314+ $ context = $ this ->getRequestContextProvider ()->capture ();
315+
316+ if (!$ context instanceof RequestContextInterface) {
317+ throw new ProfilerException ('Request context provider must return a RequestContextInterface ' );
318+ }
319+
320+ $ server = $ context ->getServer ();
321+ if (!is_array ($ server ) || !array_key_exists ('REQUEST_TIME_FLOAT ' , $ server )) {
322+ throw new ProfilerException ('Request context provider must capture REQUEST_TIME_FLOAT in server data ' );
323+ }
324+
325+ if (!is_numeric ($ server ['REQUEST_TIME_FLOAT ' ])) {
326+ throw new ProfilerException ('Request context provider must capture a numeric REQUEST_TIME_FLOAT in server data ' );
327+ }
328+
329+ return $ context ;
330+ }
280331}
0 commit comments