@@ -25,7 +25,7 @@ class Profiler {
2525 */
2626 const REGEXP_PHP_VAR = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/ ' ;
2727
28- protected static $ defaultUnitVarName = '$ProfilerUnit ' ;
28+ protected static $ profilerUnitVarName = '$ProfilerUnit ' ;
2929
3030 protected static $ profilerUnitClass = FunctionUnit::class;
3131
@@ -40,15 +40,18 @@ class Profiler {
4040 protected static $ lastElement ;
4141
4242 /**
43- * @param string $defaultUnitVarName
43+ * @param string $profilerUnitVarName
4444 * @returns bool
4545 */
46- public static function setDefaultUnitVarName (string $ defaultUnitVarName ): bool
46+ public static function setProfilerUnitVarName (string $ profilerUnitVarName ): bool
4747 {
48- if (!preg_match ( self :: REGEXP_PHP_VAR , ltrim ( $ defaultUnitVarName , '$ ' )) ) {
48+ if (!$ profilerUnitVarName || $ profilerUnitVarName [ 0 ] !== '$ ' ) {
4949 return false ;
5050 }
51- self ::$ defaultUnitVarName = $ defaultUnitVarName ;
51+ if (!preg_match (self ::REGEXP_PHP_VAR , substr ($ profilerUnitVarName , 1 ))) {
52+ return false ;
53+ }
54+ self ::$ profilerUnitVarName = $ profilerUnitVarName ;
5255 return true ;
5356 }
5457
@@ -68,12 +71,11 @@ public static function setProfilerUnitClass(string $profilerUnitClass)
6871 /**
6972 * @return array
7073 */
71- protected function &getLastElement ()
74+ protected static function &getLastElement ()
7275 {
7376 if (!self ::$ callsTree ) {
7477 self ::$ callsTree = [
7578 'parent ' => null ,
76- 'data ' => null ,
7779 'timeBeg ' => microtime (true ),
7880 'duration ' => 0 ,
7981 'items ' => [],
@@ -102,7 +104,6 @@ public static function addUnit(UnitInterface $Unit)
102104 'name ' => $ Unit ->getName (),
103105 'count ' => 1 ,
104106 'duration ' => 0 ,
105- 'data ' => null ,
106107 'items ' => [],
107108 ];
108109 }
@@ -158,9 +159,13 @@ public static function getLog(): string
158159 return $ output ;
159160 }
160161
162+ /**
163+ * @param mixed $data
164+ * @return string
165+ */
161166 protected static function formatData ($ data )
162167 {
163- return json_encode ($ data );
168+ return json_encode ($ data, JSON_UNESCAPED_UNICODE );
164169 }
165170
166171 /**
@@ -169,7 +174,7 @@ protected static function formatData($data)
169174 * @param int $totalDuration
170175 * @return string
171176 */
172- protected static function formatElement ($ element , $ level = 0 , $ totalDuration = 0 )
177+ protected static function formatElement ($ element , $ level = 0 , $ totalDuration = 0 , & $ num = 0 )
173178 {
174179 $ output = '' ;
175180 $ hasItems = !empty ($ element ['items ' ]);
@@ -180,14 +185,17 @@ protected static function formatElement($element, $level = 0, $totalDuration = 0
180185 $ total = sprintf ('%02.6f ' , $ element ['duration ' ]);
181186 $ output .= "Profiler, total: {$ total } sec \n" ;
182187 } else {
188+ $ num ++;
189+ $ sp = str_repeat (' ' , strlen ($ num ));
190+
183191 $ output .= str_repeat ($ spaces , $ level ) . PHP_EOL ;
184192 $ output .= str_repeat ($ spaces , $ level - 1 );
185- $ output .= ' > ' . $ element ['name ' ] . PHP_EOL ;
193+ $ output .= $ num . ' | ' . $ element ['name ' ] . PHP_EOL ;
186194
187195 if (!empty ($ element ['data ' ])) {
188196 $ data = self ::formatData ($ element ['data ' ]);
189197 $ output .= str_repeat ($ spaces , $ level - 1 );
190- $ output .= " | data: {$ data }\n" ;
198+ $ output .= "{ $ sp } | data: {$ data }\n" ;
191199 }
192200
193201 $ output .= str_repeat ($ spaces , $ level - 1 );
@@ -196,11 +204,11 @@ protected static function formatElement($element, $level = 0, $totalDuration = 0
196204 $ total = sprintf ('%02.6f ' , $ element ['duration ' ]);
197205 $ cost = sprintf ('%02.1f ' , $ element ['duration ' ] / ($ totalDuration ?: 1 ) * 100 );
198206
199- $ output .= " | cost: {$ cost } %, count: {$ count }, avg: {$ avg } sec, total: {$ total } sec \n" ;
207+ $ output .= "{ $ sp } | cost: {$ cost } %, count: {$ count }, avg: {$ avg } sec, total: {$ total } sec \n" ;
200208 }
201209 if ($ hasItems ) {
202210 foreach ($ element ['items ' ] as $ el ) {
203- $ output .= self ::formatElement ($ el , $ level + 1 , $ element ['duration ' ]);
211+ $ output .= self ::formatElement ($ el , $ level + 1 , $ element ['duration ' ], $ num );
204212 }
205213 }
206214 return $ output ;
@@ -210,9 +218,9 @@ protected static function formatElement($element, $level = 0, $totalDuration = 0
210218 * @param string $filename
211219 * @param bool $withArguments
212220 * @param bool $withResult
213- * @param string $regExpFilter
221+ * @param string|null $regExpFilter
214222 */
215- public static function includeFile (string $ filename , string $ regExpFilter = '' )
223+ public static function includeFile (string $ filename , string $ regExpFilter = null )
216224 {
217225 $ file = file_get_contents ($ filename );
218226 $ file = self ::injectProfilerUnitToCode ($ file , self ::$ profilerUnitClass , $ regExpFilter );
@@ -235,12 +243,12 @@ public static function includeFile(string $filename, string $regExpFilter = '')
235243 * @param string $regExpFilter
236244 * @return string
237245 */
238- protected static function injectProfilerUnitToCode (string $ source , string $ unitClassName , string $ regExpFilter = '' )
246+ protected static function injectProfilerUnitToCode (string $ source , string $ unitClassName , string $ regExpFilter = null )
239247 {
240248 $ tokens = token_get_all ($ source );
241249 unset($ source );
242250
243- $ defaultUnitVarName = self ::$ defaultUnitVarName ;
251+ $ unitVarName = self ::$ profilerUnitVarName ;
244252 $ withArguments = is_subclass_of ($ unitClassName , CollectArgumentsInterface::class);
245253 $ withResult = is_subclass_of ($ unitClassName , CollectResultInterface::class);
246254 $ unitClass = '\\' . ltrim ($ unitClassName , '\\' );
@@ -294,7 +302,7 @@ protected static function injectProfilerUnitToCode(string $source, string $unitC
294302
295303 if ($ withResult && $ functionFound && ($ id === T_RETURN || $ id === T_THROW ) && $ functionName ) {
296304 if (!$ regExpFilter || preg_match ($ regExpFilter , $ functionName )) {
297- $ code .= " {$ defaultUnitVarName }->result = " ;
305+ $ code .= " {$ unitVarName }->result = " ;
298306 }
299307 continue ;
300308 }
@@ -303,9 +311,9 @@ protected static function injectProfilerUnitToCode(string $source, string $unitC
303311 if ($ text === '{ ' ) {
304312 if (!$ stack ) {
305313 if (!$ regExpFilter || preg_match ($ regExpFilter , $ functionName )) {
306- $ code .= "{$ defaultUnitVarName } = {$ unitClass }::create(__METHOD__, {$ functionLine }, {$ functionColumn }); " ;
314+ $ code .= "{$ unitVarName } = {$ unitClass }::create(__METHOD__, {$ functionLine }, {$ functionColumn }); " ;
307315 if ($ withArguments ) {
308- $ code .= "{$ defaultUnitVarName }->setArguments(func_get_args()); " ;
316+ $ code .= "{$ unitVarName }->setArguments(func_get_args()); " ;
309317 }
310318 }
311319 }
0 commit comments