-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tau.php
236 lines (199 loc) · 5.24 KB
/
Tau.php
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* The main Tau module.
*
* Just include it and go.
*
* @Author theyak
* @Copyright 2012
* @Project Page None!
* @Dependencies TauError, TauFS
* @Documentation None!
*
* changelog:
* 1.0.1 2013-09-29 Do not use class_exists for auto-loader. hiphop-php does not support.
*/
if (!defined('TAU'))
{
define('TAU', true);
}
// Store the start time of when this script is executed.
// Purely for informational purposes.
Tau::$start_time = microtime(true);
// Set up autoloader so that developers do not have to spend their
// time checking if things have been included and then including if not.
spl_autoload_register('Tau::autoloadModule');
// Set the location of the root path to the Tau library, which
// unless you are doing something really funky, should be the directory
// in which this file is found.
Tau::$root_path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
// Set up End of Line characters
if (Tau::isCli())
{
Tau::$EOL = PHP_EOL;
}
else
{
Tau::$EOL = "<br>";
}
class Tau
{
// Path to the root of the TAU library. Generally where TAU.php is located.
public static $root_path;
// Start time as indicated by the time TAU is instantiated.
public static $start_time = 0;
// The cache, if used
public static $cache;
// End of line to use
public static $EOL;
// Directory separator, short form
public static $DS = DIRECTORY_SEPARATOR;
const DS = DIRECTORY_SEPARATOR;
function __construct()
{
}
/**
* Autoloader, called automatically by PHP when referenceing a class that
* is not yet loaded.
*
* @param string $module
*/
public static function autoloadModule($module)
{
self::module($module, false);
}
/**
* Load a Tau module
*
* @param string $module Name of module to load
* @param boolean $exception Flag indicating if exception should be thrown if unable to load module
* @throws Exception
*/
public static function module($module, $exception = true)
{
// hiphop-php does not support class_exists, so use this instead
static $modules = array();
$path = self::$root_path . 'modules' . DIRECTORY_SEPARATOR;
// If prefix isn't Tau, then it isn't a Tau module.
// This is most useful to help speed things up when other
// autoloaders are defined so we don't have to go through
// the presumably slow process of checking if the file exists or not.
if (substr($module, 0, 3) != 'Tau')
{
return;
}
// Check if module has already been loaded. If so, do nothing.
if (isset($modules[$module]))
{
return;
}
// Check if module exists. If it does not, throw exception as necessary
$module = str_replace("\\", Tau::DS, $module);
if (!file_exists($path . $module . '.php'))
{
// TODO: Check a repository for class and download, save, and
// include if available.
if ($exception) {
throw new Exception('Undefined module ' . $module);
} else {
return;
}
}
include $path . $module . '.php';
$modules[$module] = $path . $module . '.php';
}
/* Ultra-common utilitiy methods */
/**
* Check if script is run from command prompt
*
* @return boolean
*/
public static function isCli()
{
return php_sapi_name() == 'cli';
}
public static function isAjax()
{
return
( defined( 'AJAX' ) && AJAX === true ) ||
( isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && $_SERVER[ 'HTTP_X_REQUESTED_WITH'] === "XMLHttpRequest" );
}
/**
* Dump the contents of a variable to the display
*
* @param mixed $message
* @param int $line
* @param string $file
*/
public static function dump($message, $line='', $file='')
{
if (empty($file) && function_exists('debug_backtrace'))
{
$dbg = debug_backtrace();
$file = $dbg[0]['file'];
$line = $dbg[0]['line'];
unset($dbg);
}
$title = array();
if (!empty($line))
{
$title[] = 'Line: ' . $line;
}
if (!empty($file))
{
$title[] = 'File: ' . $file;
}
if (Tau::isCli() || Tau::isAjax() )
{
echo empty($title) ? '' : implode(' - ', $title) . Tau::$EOL;
}
else
{
echo '<pre style="text-align:left;background-color: #ffffff; color: #000000; border: 1px; border-style: outset; padding: 5px;"><strong>' . (empty($title) ? '' : implode(' - ', $title) . '</strong><br />');
}
if (is_bool($message))
{
echo $message ? 'TRUE' : 'FALSE';
}
else if (empty($message))
{
echo is_numeric($message) ? $message : 'Empty';
}
else if (is_array($message) || is_object($message))
{
// An empty error handler is required for things such
// as result sets which do not work with print_r.
$old_handler = set_error_handler( function( $errno ) {
;
} );
if (Tau::isCli() || Tau::isAjax())
{
print_r($message);
}
else
{
echo htmlspecialchars(print_r($message, true));
}
set_error_handler( $old_handler );
}
else
{
if (Tau::isCli() || Tau::isAjax())
{
echo $message;
}
else
{
echo str_replace("\t", ' ', htmlspecialchars($message));
}
}
if (Tau::isCli() || Tau::isAjax() )
{
echo Tau::$EOL;
}
else
{
echo '</pre>';
}
}
}