Automad
 All Classes Functions Variables Pages
debug.php
1 <?php
2 /*
3  * ....
4  * .: '':.
5  * :::: ':..
6  * ::. ''..
7  * .:'.. ..':.:::' . :. '':.
8  * :. '' '' '. ::::.. ..:
9  * ::::. ..':.. .'''::::: .
10  * :::::::.. '..:::: :. :::: :
11  * ::'':::::::. ':::.'':.:::: :
12  * :.. ''::::::....': '':: :
13  * :::::. '::::: : .. '' .
14  * .''::::::::... ':::.'' ..'' :.''''.
15  * :..:::''::::: :::::...:'' :..:
16  * ::::::. ':::: :::::::: ..:: .
17  * ::::::::.:::: :::::::: :'':.:: .''
18  * ::: '::::::::.' ''::::: :.' '': :
19  * ::: :::::::::..' :::: ::...' .
20  * ::: .:::::::::: :::: :::: .:'
21  * '::' ''::::::: :::: : :: :
22  * ':::: :::: :'' .:
23  * :::: :::: ..''
24  * :::: ..:::: .:''
25  * '''' '''''
26  *
27  *
28  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
54 class Debug {
55 
56 
61  private static $buffer = '';
62 
63 
68  private static $time = NULL;
69 
70 
75  public static function errorReporting() {
76 
77  if (AM_DEBUG_ENABLED) {
78  error_reporting(E_ALL);
79  }
80 
81  }
82 
83 
88  public static function getLog() {
89 
90  if (AM_DEBUG_ENABLED) {
91 
92  // Stop timer.
93  self::timerStop();
94 
95  // Get user & server constants.
96  self::uc();
97  self::log($_SERVER, 'Server');
98 
99  return '<script type="text/javascript">' . "\n" . self::$buffer . '</script>' . "\n";
100 
101  }
102 
103  }
104 
105 
113  public static function log($element, $description = '') {
114 
115  if (AM_DEBUG_ENABLED) {
116 
117  // Start timer. self::timerStart() only saves the time on the first call.
118  self::timerStart();
119 
120  // Get backtrace.
121  $backtraceAll = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
122 
123  // Remove all backtrace items without any class defined (standard PHP functions) and the items with the functions Debug::log() and {closure}
124  // To get a clean array with only the relevant Automad methods in the backtrace.
125  $ignoreFunctions = array('log', __NAMESPACE__ . '\{closure}');
126  $backtrace = array_filter($backtraceAll, function($item) use ($ignoreFunctions) {
127  return (isset($item['class'], $item['type'], $item['function']) && !in_array($item['function'], $ignoreFunctions));
128  });
129 
130  // If class, type & method exist, use them to build the description prefix. Else use just the file name from the full backtrace.
131  if (count($backtrace) > 0) {
132  // When the backtrace array got reduced to the actually relevant items in the backtrace, take the first element (the one calling Debug::log()).
133  $backtrace = array_shift($backtrace);
134  $prefix = basename(str_replace('\\', '/', $backtrace['class'])) . $backtrace['type'] . $backtrace['function'] . '(): ';
135  } else {
136  $prefix = basename($backtraceAll[0]['file']) . ': ';
137  }
138 
139  // Prepend the method to $description.
140  $description = trim($prefix . $description, ': ');
141 
142  self::$buffer .= "console.log(" . json_encode(array($description => $element)) . ");\n";
143 
144  }
145 
146  }
147 
148 
153  private static function timerStart() {
154 
155  // Only save time on first call.
156  if (!self::$time) {
157  self::$time = microtime(true);
158  self::log(date('d. M Y, H:i:s'));
159  }
160 
161  }
162 
163 
168  private static function timerStop() {
169 
170  if (self::$time) {
171  $executionTime = microtime(true) - self::$time;
172  self::log($executionTime . ' seconds', 'Time for execution');
173  }
174 
175  }
176 
177 
182  private static function uc() {
183 
184  $definedConstants = get_defined_constants(true);
185  $userConstants = $definedConstants['user'];
186  self::log($userConstants, 'Automad constants');
187 
188  }
189 
190 
191 }
192 
193 
194 ?>
static log($element, $description= '')
Definition: debug.php:113
static timerStop()
Definition: debug.php:168
static getLog()
Definition: debug.php:88
static timerStart()
Definition: debug.php:153
static uc()
Definition: debug.php:182
static errorReporting()
Definition: debug.php:75