22 if (!function_exists(
'import')) {
23 function import($class) {
24 $filePath = str_replace(
'.',
'/', $class) .
'.inc.php';
35 static $isErrorCondition =
null;
36 static $showStackTrace =
false;
38 if ($isErrorCondition ===
null) {
39 $isErrorCondition =
true;
41 $isErrorCondition =
false;
44 echo
"<h1>" . htmlspecialchars($reason) .
"</h1>";
46 if ($showStackTrace) {
47 echo
"<h4>Stack Trace:</h4>\n";
48 $trace = debug_backtrace();
59 foreach ($trace as $bt) {
61 if (isset($bt[
'args']))
foreach ($bt[
'args'] as $a) {
65 switch (gettype($a)) {
71 $a = htmlspecialchars(substr($a, 0, 64)).((strlen($a) > 64) ?
'...' :
'');
75 $args .=
'Array('.count($a).
')';
78 $args .=
'Object('.get_class($a).
')';
81 $args .=
'Resource('.strstr($a,
'#').
')';
84 $args .= $a ?
'True' :
'False';
93 $class = isset($bt[
'class'])?$bt[
'class']:
'';
94 $type = isset($bt[
'type'])?$bt[
'type']:
'';
95 $function = isset($bt[
'function'])?$bt[
'function']:
'';
96 $file = isset($bt[
'file'])?$bt[
'file']:
'(unknown)';
97 $line = isset($bt[
'line'])?$bt[
'line']:
'(unknown)';
99 echo
"<strong>File:</strong> {$file} line {$line}<br />\n";
100 echo
"<strong>Function:</strong> {$class}{$type}{$function}($args)<br />\n";
108 if (class_exists(
'Registry')) {
111 $applicationName =
'';
116 error_log($applicationName.$reason);
118 if (defined(
'DONT_DIE_ON_ERROR') && DONT_DIE_ON_ERROR ==
true) {
120 trigger_error($reason);
133 return (version_compare(PHP_VERSION, $version) !== -1);
165 function &
instantiate($fullyQualifiedClassName, $expectedTypes =
null, $expectedPackages =
null, $expectedMethods =
null, $constructorArg =
null) {
169 if (!preg_match(
'/^[a-zA-Z0-9.]+$/', $fullyQualifiedClassName)) {
174 if (!is_null($expectedPackages)) {
175 if (is_scalar($expectedPackages)) $expectedPackages = array($expectedPackages);
176 $validPackage =
false;
177 foreach ($expectedPackages as $expectedPackage) {
179 if (substr($fullyQualifiedClassName, 0, strlen($expectedPackage)+1) == $expectedPackage.
'.') {
180 $validPackage =
true;
188 if (!$validPackage) {
190 $expectedPackageCount = count($expectedPackages);
192 $expectedPackageString =
'';
193 foreach($expectedPackages as $expectedPackageIndex => $expectedPackage) {
194 if ($expectedPackageIndex > 0) {
195 $separator = ($expectedPackageIndex == $expectedPackageCount-1 ?
' or ' :
', ' );
197 $expectedPackageString .= $separator.
'"'.$expectedPackage.
'"';
199 fatalError(
'Trying to instantiate class "'.$fullyQualifiedClassName.
'" which is not in any of the expected packages '.$expectedPackageString.
'.');
204 import($fullyQualifiedClassName);
207 $fullyQualifiedClassNameParts = explode(
'.', $fullyQualifiedClassName);
208 $className = array_pop($fullyQualifiedClassNameParts);
211 if (!class_exists($className)) {
212 fatalError(
'Cannot instantiate class. Class "'.$className.
'" is not declared in "'.$fullyQualifiedClassName.
'".');
216 $expectedMethods = (array) $expectedMethods;
217 $declaredMethods = get_class_methods($className);
218 if (count(array_intersect($expectedMethods, $declaredMethods)) != count($expectedMethods)) {
223 if (is_null($constructorArg)) {
224 $classInstance =
new $className();
226 $classInstance =
new $className($constructorArg);
230 if (!is_null($expectedTypes)) {
231 if (is_scalar($expectedTypes)) $expectedTypes = array($expectedTypes);
233 foreach($expectedTypes as $expectedType) {
234 if (is_a($classInstance, $expectedType)) {
239 if (!$validType)
return $errorFlag;
242 return $classInstance;
251 if (!is_array($array))
return null;
252 return array_filter($array,
function($o) {
264 foreach ($values as $key => $value) {
265 if (is_scalar($value)) {
266 $values[$key] = strip_tags($values[$key]);
281 return strtr($str,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz');
291 return strtr($str,
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
323 if (substr($class, 0, strlen($prefix)) !== $prefix) {
327 $class = substr($class, strlen($prefix));
328 $parts = explode(
'\\', $class);
332 if (count($parts) < 2) {
337 $parts = array_map(
function($part) {
343 $subParts = join(
'/', $parts);
344 $filePath =
"{$rootPath}/{$subParts}/{$className}.inc.php";
346 if (is_file($filePath)) {
347 require_once($filePath);