Add base/profiling.php and data/profiling_pdo.php.
[hoplite.git] / base / profiling.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2013 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\base;
18
19 /*!
20 A static class used to help profile Hoplite components.
21 */
22 class Profiling
23 {
24 /*! @var bool Enable profiling. */
25 static private $profiling_enabled = FALSE;
26
27 /*! Enables profiling globally. */
28 static public function EnableProfiling()
29 {
30 self::$profiling_enabled = TRUE;
31 }
32
33 /*! Gets the current state for profiling. */
34 static public function IsProfilingEnabled()
35 {
36 return self::$profiling_enabled;
37 }
38
39 /*!
40 Prepares a debug_backtrace() array for output to the browser by
41 compressing the array into visible text
42
43 @param array The return value from debug_backtrace().
44
45 @return array Array of strings that can be imploded() for display.
46 */
47 static public function FormatDebugBacktrace($backtrace)
48 {
49 $trace = array();
50 foreach ($backtrace AS $i => $frame) {
51 $args = '';
52 $file = $frame['file'] . ':' . $frame['line'];
53 $file = str_replace($_SERVER['DOCUMENT_ROOT'], '', $file);
54 $funct = (isset($frame['class']) ? $frame['class'] . '::' . $frame['function'] : $frame['function']);
55
56 if (isset($frame['args']) && is_array($frame['args'])) {
57 // Convert arrays and objects to strings.
58 foreach ($frame['args'] AS $id => $arg) {
59 if (is_array($arg))
60 $frame['args']["$id"] = 'Array';
61 else if (is_object($arg))
62 $frame['args']["$id"] = get_class($arg);
63 }
64 $args = implode(', ', $frame['args']);
65 }
66
67 $trace[] = "#$i $funct($args) called at [$file]";
68 }
69
70 return $trace;
71 }
72 }