1 |
<?php |
2 |
|
3 |
/** |
4 |
* This file contains some core functions extending php. |
5 |
* |
6 |
* @author Andreas Motl <andreas.motl@ilo.de> |
7 |
* @package org.netfrag.glib |
8 |
* @name php |
9 |
* |
10 |
*/ |
11 |
|
12 |
/** |
13 |
* $Id: php_extensions.php,v 1.2 2003/03/05 11:58:49 joko Exp $ |
14 |
* |
15 |
* $Log: php_extensions.php,v $ |
16 |
* Revision 1.2 2003/03/05 11:58:49 joko |
17 |
* modified is_hash, mkInstance and others |
18 |
* |
19 |
* Revision 1.1 2003/03/03 21:08:21 joko |
20 |
* refactored from flib/utils |
21 |
* |
22 |
* |
23 |
*/ |
24 |
|
25 |
|
26 |
/** |
27 |
* --- shortcut functions |
28 |
* Is there a mechanism in php to 'export' these methods |
29 |
* to the global symbol table automagically? |
30 |
* We wouldn't require declaring these shortcuts below and |
31 |
* could also do some nice stuff at runtime. |
32 |
* (maybe resembling CPAN's Exporter) |
33 |
* |
34 |
* Okay, 'eval' and 'create_function' are alternatives, but i'm |
35 |
* still looking for something that would let us manipulate the |
36 |
* symbol table.... (would also be a nice-to-have feature for |
37 |
* mungling "other" symbol table entries, like variables |
38 |
* and/or object references etc.) |
39 |
* |
40 |
* TODO: $php::EXPORT_OK = array('Dumper', 'session_register_safe', 'mkObject', 'is_hash', 'merge_to'); |
41 |
* |
42 |
*/ |
43 |
// this is (and will probably always be) required by the "Exporter" |
44 |
function &Dumper() { |
45 |
$args = func_get_args(); |
46 |
// shrink array to single item? |
47 |
//if (sizeof($args) == 1) { $args = $args[0]; } |
48 |
php::array_shrink($args); |
49 |
return php::Dumper($args); |
50 |
} |
51 |
|
52 |
// these could be refactore using Exporter proposal below, |
53 |
// which just does this code at runtime via eval |
54 |
//function &session_register_safe() { return php::session_register_safe(func_get_args()); } |
55 |
//function &is_hash() { return php::is_hash(func_get_args()); } |
56 |
//function &loadModule() { return php::loadModule(func_get_args()); } |
57 |
//function &mkObject() { return php::mkInstance(func_get_args()); } |
58 |
|
59 |
|
60 |
//exit; |
61 |
|
62 |
// yes! |
63 |
php::export_symbols(); |
64 |
|
65 |
|
66 |
|
67 |
/** |
68 |
* --- php extension functions lying in the php:: namespace |
69 |
* |
70 |
* <pre> |
71 |
* x php::Dumper |
72 |
* x php::session_register_safe |
73 |
* x php::array_init |
74 |
* x php::is_hash |
75 |
* x php::array_join_merge |
76 |
* o php::merge |
77 |
* o php::merge_to |
78 |
* x php::loadModule |
79 |
* x php::mkInstance |
80 |
* </pre> |
81 |
* |
82 |
* @author Andreas Motl <andreas.motl@ilo.de> |
83 |
* @copyright (c) 2003 - All Rights reserved. |
84 |
* @license GNU LGPL (GNU Lesser General Public License) |
85 |
* |
86 |
* @link http://www.netfrag.org/~joko/ |
87 |
* @link http://www.gnu.org/licenses/lgpl.txt |
88 |
* |
89 |
* @package org.netfrag.glib |
90 |
* @name php |
91 |
* |
92 |
* @todo |
93 |
* <pre> |
94 |
* Todo: |
95 |
* |
96 |
* o establish mkInstance here (move from DesignPattern::Object::mkObject) |
97 |
* o establish loadModule here (move from DesignPattern::Object::loadModule) |
98 |
* |
99 |
* Ideas: |
100 |
* o php::create_redirector_function('func', array( type => 'lamba|function|method', name => 'Abc::do_xyz') ) |
101 |
* o php::create_redirector_method('class|obj_ref', 'method', $target) |
102 |
* o php::export_symbol(from, to) (maybe solves above two) |
103 |
* </pre> |
104 |
* |
105 |
*/ |
106 |
class php { |
107 |
|
108 |
|
109 |
// Exporter proposal |
110 |
|
111 |
// does (e.g.) Exporter::export_symbol('php', 'loadModule', array( php_function => 'loadModule' )); |
112 |
// (or) Exporter::export_symbol('php', 'mkInstance', array( php_function => 'mkObject' )); |
113 |
// -> just all from EXPORT_OK |
114 |
|
115 |
function export_symbols() { |
116 |
|
117 |
$symbols = func_get_args(); |
118 |
php::array_shrink($symbols); |
119 |
|
120 |
global $PHP_EXTENSIONS_EXPORT; |
121 |
//print Dumper($PHP_EXTENSIONS_EXPORT); |
122 |
//exit; |
123 |
|
124 |
if (is_array($PHP_EXTENSIONS_EXPORT)) { |
125 |
//if (defined('EXPORT_OK')) { |
126 |
//print EXPORT_OK . "<br/>"; |
127 |
//$symbols = split(' ', EXPORT_OK); |
128 |
//foreach ($php->EXPORT_OK as $symbol) { |
129 |
/* |
130 |
foreach (EXPORT_OK as $symbol) { |
131 |
print "exporting: $symbol<br/>"; |
132 |
Exporter::export_symbol('php', $symbol, array( php_function => $symbol )); |
133 |
} |
134 |
*/ |
135 |
|
136 |
$symbols = php::array_join_merge($PHP_EXTENSIONS_EXPORT, $symbols); |
137 |
|
138 |
php::loadModule('Exporter'); |
139 |
if (class_exists('Exporter')) { |
140 |
Exporter::export_symbols('php', $symbols); |
141 |
} |
142 |
|
143 |
} |
144 |
|
145 |
} |
146 |
|
147 |
function Dumper() { |
148 |
$arg_list = func_get_args(); |
149 |
$count = 1; |
150 |
|
151 |
// build dump by catching output of php's 'print_r' |
152 |
// using php's output buffering functions |
153 |
ob_start(); |
154 |
foreach ($arg_list as $arg) { |
155 |
print_r($arg); |
156 |
if (is_string($arg)) { |
157 |
print "\n"; |
158 |
} |
159 |
$count++; |
160 |
} |
161 |
$var_dump = ob_get_contents(); |
162 |
ob_end_clean(); |
163 |
|
164 |
//print "mode: " . $this->Dumper_mode . "<br/>"; |
165 |
//if ($this->Dumper_mode == HTML) { |
166 |
$var_dump = str_replace("\n", '<br/>', $var_dump); |
167 |
$var_dump = str_replace(" ", ' ', $var_dump); |
168 |
//} |
169 |
|
170 |
//if (sizeof($args) == 1) { $var_dump .= '<br/>'; } |
171 |
|
172 |
return $var_dump; |
173 |
} |
174 |
|
175 |
function session_register_safe($varname) { |
176 |
if (!session_is_registered($varname)) { |
177 |
session_register($varname); |
178 |
return 1; |
179 |
} |
180 |
} |
181 |
|
182 |
// ------------------------------------------------- |
183 |
// array initializer |
184 |
// initializes passed variable (taken by reference) with an empty array |
185 |
// does this only if it doesn't exist yet or initialization is explicitely requested ($clear = 1) |
186 |
function array_init(&$var, $clear = 0) { |
187 |
if (!is_array($var) || $clear) { $var = array(); } |
188 |
} |
189 |
|
190 |
// from: php.net - http://www.php.net/manual/en/function.array-merge-recursive.php |
191 |
function is_hash( $var = null ) { |
192 |
if( is_array( $var ) ) { |
193 |
$keys = array_keys( $var ); |
194 |
$all_num = true; |
195 |
for( $i=0; $i<count($keys); $i++ ) |
196 |
if( is_string($keys[$i] ) ) return true; |
197 |
} |
198 |
return false; |
199 |
} |
200 |
|
201 |
// from: php.net - http://www.php.net/manual/en/function.array-merge-recursive.php |
202 |
function array_join_merge( $arr1, $arr2 ) { |
203 |
if( is_array( $arr1 ) and is_array( $arr2 ) ) { |
204 |
// the same -> merge |
205 |
$new_array = array(); |
206 |
|
207 |
if( php::is_hash( $arr1 ) and php::is_hash( $arr2 ) ) { |
208 |
// hashes -> merge based on keys |
209 |
$keys = array_merge( array_keys( $arr1 ), array_keys( $arr2 ) ); |
210 |
foreach( $keys as $key ) { |
211 |
$new_array[$key] = php::array_join_merge( $arr1[$key], $arr2[$key] ); |
212 |
} |
213 |
} else { |
214 |
// two real arrays -> merge |
215 |
$new_array = |
216 |
array_reverse(array_unique(array_reverse(array_merge($arr1,$arr2)))); |
217 |
} |
218 |
|
219 |
return $new_array; |
220 |
} else { |
221 |
// not the same ... take new one if defined, else the old one stays |
222 |
return $arr2 ? $arr2 : $arr1; |
223 |
} |
224 |
} |
225 |
|
226 |
|
227 |
// TODO: |
228 |
// o refactor to the php:: namespace? |
229 |
// o PEAR::??? |
230 |
// x rename mkObject to mkInstance |
231 |
// x rename loadModule? let it like is.... (suggestions? loadPackage?) |
232 |
// o enhance loadModule: make it possible to load packages from anywhere?! #?)&%$ |
233 |
|
234 |
function namespace2filename($nsName) { |
235 |
if ($filename = str_replace('::', '/', $nsName)) { |
236 |
return $filename; |
237 |
} |
238 |
} |
239 |
|
240 |
function loadModule($namespacedClassname, $options = null) { |
241 |
|
242 |
if (DEBUG_PHP_EXTENSIONS == 1) { |
243 |
print "php::loadModule: " . Dumper($namespacedClassname); |
244 |
} |
245 |
if (LOG_PHP_EXTENSIONS == 1) { |
246 |
php::append_log( "php::loadModule( $namespacedClassname )" ); |
247 |
} |
248 |
|
249 |
if ($filename = php::namespace2filename($namespacedClassname)) { |
250 |
|
251 |
if (is_array($options)) { |
252 |
$extension = $options[extension]; |
253 |
} elseif ($options) { |
254 |
$extension = $options; |
255 |
} else { |
256 |
$extension = 'php'; |
257 |
} |
258 |
|
259 |
$filename .= ".$extension"; |
260 |
|
261 |
// V1: just require |
262 |
//require_once($filename); |
263 |
|
264 |
// V2: check file for existance (didn't work!!! problem with path-style conversion regarding platform running on?) |
265 |
/* |
266 |
if (file_exists($filename)) { |
267 |
require_once($filename); |
268 |
} else { |
269 |
//user_error("Could not load module '$namespacedClassname', file '$filename' does not exist."); |
270 |
} |
271 |
*/ |
272 |
|
273 |
// V3: wrapped through eval (weird) |
274 |
/* |
275 |
$evalstring = "return include_once('$filename');"; |
276 |
$result = eval($evalstring); |
277 |
if (!$result) { |
278 |
user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); |
279 |
return; |
280 |
} |
281 |
*/ |
282 |
|
283 |
// V4: just include |
284 |
//define ("WARNING", E_USER_NOTICE); |
285 |
//error_reporting(E_USER_WARNING); |
286 |
//error_reporting(E_ALL); |
287 |
//error_reporting(E_CORE_ERROR); |
288 |
//set_error_handler(array('php::error_handler_2')); |
289 |
|
290 |
//set_error_handler('php_error_handler'); |
291 |
// TODO: do stack-backtracing here to determine function called two or three levels before!!! |
292 |
//print "file: $filename<br/>"; |
293 |
if (!include_once($filename)) { |
294 |
$msg = "php::loadModule error: Could not load module '$namespacedClassname', file '$filename' does not exist."; |
295 |
user_error($msg); |
296 |
php::append_log($msg, PEAR_LOG_ERR); |
297 |
//trigger_error($msg); |
298 |
//print "msg: $msg<br/>"; |
299 |
return; |
300 |
} |
301 |
//restore_error_handler(); |
302 |
|
303 |
//exit; |
304 |
|
305 |
return 1; |
306 |
} |
307 |
} |
308 |
|
309 |
|
310 |
function &mkComponent() { |
311 |
|
312 |
// argument handling - perl style |
313 |
$arg_list = &func_get_args(); |
314 |
|
315 |
// trace |
316 |
//print "php::mkInstance: " . Dumper($arg_list); |
317 |
//exit; |
318 |
|
319 |
// patch arglist if all arguments are passed in as a single array |
320 |
if (sizeof($arg_list) == 1 && is_array($arg_list[0])) { |
321 |
$arg_list = &$arg_list[0]; |
322 |
} |
323 |
|
324 |
//print "arg_list: $arg_list<br/>"; |
325 |
$namespacedClassname = array_shift($arg_list); |
326 |
|
327 |
// debug |
328 |
if (DEBUG_PHP_EXTENSIONS == 1) { |
329 |
print "php::mkComponent: $namespacedClassname<br/>"; |
330 |
} |
331 |
if (LOG_PHP_EXTENSIONS == 1) { |
332 |
php::append_log( "php::mkComponent( $namespacedClassname )" ); |
333 |
} |
334 |
|
335 |
$classname = $namespacedClassname; |
336 |
$filename = $namespacedClassname; |
337 |
$load_module_options = null; |
338 |
|
339 |
if (strstr($namespacedClassname, '_')) { |
340 |
//print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; |
341 |
//return; |
342 |
} |
343 |
|
344 |
if (strstr($namespacedClassname, '.')) { |
345 |
//print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; |
346 |
//return; |
347 |
$parts = split('\.', $namespacedClassname); |
348 |
$filename = $parts[0]; |
349 |
$extension = $parts[sizeof($parts) - 1]; |
350 |
$load_module_options = array( extension => $extension ); |
351 |
} |
352 |
|
353 |
|
354 |
// trace |
355 |
//print "mkObject: $classname<br>"; |
356 |
//print "file: $filename<br>"; |
357 |
|
358 |
// build native class name from namespaced one |
359 |
$classname = str_replace('::', '_', $classname); |
360 |
$classname = str_replace('/', '_', $classname); |
361 |
|
362 |
// load class-file |
363 |
if (!class_exists($classname)) { |
364 |
php::loadModule($filename, $load_module_options); |
365 |
} |
366 |
|
367 |
// instantiate object |
368 |
$attributes = $arg_list; |
369 |
// V0: |
370 |
//$obj = new $classname($attributes); |
371 |
// V1: |
372 |
//$obj = new DesignPattern_Bridge($classname, $attributes); |
373 |
// V2: |
374 |
$obj = php::mkInstance($classname, $attributes); |
375 |
|
376 |
// constructor autocall - if possible |
377 |
// FIXME: take care for side-effects!!! |
378 |
/* |
379 |
if (method_exists($obj, 'constructor')) { |
380 |
$obj->constructor(); |
381 |
} |
382 |
*/ |
383 |
|
384 |
// return instance |
385 |
return $obj; |
386 |
|
387 |
} |
388 |
|
389 |
// from: http://www.php.net/manual/en/function.method-exists.php |
390 |
function class_has_method($className, $methodName) { |
391 |
$bool_exists = (in_array(strtolower($methodName), get_class_methods($className))); |
392 |
return $bool_exists; |
393 |
} |
394 |
|
395 |
// down: array( item ) => item |
396 |
function &array_shrink(&$array) { |
397 |
// shrink array to single item? yes! |
398 |
if (is_array($array) && !php::is_hash($array) && (sizeof($array) == 1)) { $array = $array[0]; } |
399 |
return $array; |
400 |
} |
401 |
|
402 |
function append_log($message, $level = PEAR_LOG_DEBUG) { |
403 |
static $Class_Logger_loaded; |
404 |
static $Class_Logger_instance; |
405 |
if (!$Class_Logger_loaded) { |
406 |
$Class_Logger_loaded++; |
407 |
//print "LOAD<br/>"; |
408 |
php::loadModule('Class::Logger'); |
409 |
} |
410 |
if (class_exists('Class_Logger')) { |
411 |
//$Class_Logger_instance = new Class_Logger(); |
412 |
$Class_Logger_instance = php::mkComponent('Class::Logger'); |
413 |
//print "YAI<br/>"; |
414 |
//Class_Logger::log($message); |
415 |
$Class_Logger_instance->log($message, $level); |
416 |
} else { |
417 |
// FIXME! pre-logger log-messages (two or three): |
418 |
// [PEAR_LOG_DEBUG] php::loadModule( Class::Logger ) |
419 |
// [PEAR_LOG_DEBUG] php::loadModule( DesignPattern::AbstractClass ) |
420 |
//print "[$level] $message" . "<br/>"; |
421 |
} |
422 |
} |
423 |
|
424 |
function &mkInstance($classname, $attributes = array()) { |
425 |
|
426 |
$oo = is_object($this); |
427 |
|
428 |
// autocall constructor? |
429 |
if ($oo) { |
430 |
//print Dumper($this); |
431 |
//exit; |
432 |
//print "this: $this<br/>"; |
433 |
//print "parent: $parent<br/>"; |
434 |
// V1: |
435 |
//parent::constructor(); |
436 |
// V2: |
437 |
//call_user_func('parent::constructor'); |
438 |
// V3: |
439 |
//call_user_func(array($this, 'constructor')); |
440 |
if (method_exists($this, 'constructor')) { |
441 |
//print "YAI<br/>"; |
442 |
//call_user_func(array($this, 'constructor')); |
443 |
} |
444 |
} |
445 |
|
446 |
if ($oo) { |
447 |
//$this->log( get_class($this) . "->mkInstance( classname $classname )" ); |
448 |
//Class_Logger::log( get_class($this) . "->mkInstance( classname $classname )" ); |
449 |
} else { |
450 |
//php::append_log( "php::mkInstance( classname $classname )" ); |
451 |
} |
452 |
|
453 |
if (isset($attributes)) { |
454 |
//print Dumper($attributes); |
455 |
|
456 |
/* |
457 |
// pass single argument 1:1 |
458 |
if (count($attributes) == 1) { |
459 |
$attributes_merged = $attributes[0]; |
460 |
|
461 |
|
462 |
|
463 |
// pass hash 1:1 |
464 |
} elseif (is_hash($attributes)) { |
465 |
$attributes_merged = $attributes; |
466 |
|
467 |
} else { |
468 |
$attributes_merged = $attributes; |
469 |
} |
470 |
*/ |
471 |
|
472 |
$args_pass = array(); |
473 |
for ($i=0; $i<=count($attributes); $i++) { |
474 |
array_push($args_pass, '&$attributes[' . $i . ']'); |
475 |
} |
476 |
|
477 |
$arg_string = join(', ', $args_pass); |
478 |
|
479 |
/* |
480 |
// merge entries of numerical indexed arrays together into one hash |
481 |
} else { |
482 |
$attributes_merged = array(); |
483 |
foreach ($attributes as $entry) { |
484 |
$attributes_merged = array_merge($attributes_merged, $entry); |
485 |
} |
486 |
} |
487 |
*/ |
488 |
|
489 |
if (!class_exists($classname)) { |
490 |
user_error("Class '$classname' doesn't exist."); |
491 |
return; |
492 |
} |
493 |
|
494 |
//print Dumper($attributes_merged); |
495 |
//print Dumper($attributes); |
496 |
//$instance = new $classname($attributes_merged); |
497 |
//$instance = new $classname($attributes[0]); |
498 |
$evalstr = 'return new $classname(' . $arg_string . ');'; |
499 |
|
500 |
//print "eval: $evalstr<br/>"; |
501 |
|
502 |
$instance = eval($evalstr); |
503 |
//print $evalstr . "<br>"; |
504 |
} else { |
505 |
$instance = new $classname; |
506 |
} |
507 |
//$this->log("ok"); |
508 |
return $instance; |
509 |
} |
510 |
|
511 |
// up: string -> array |
512 |
function array_cast_safe(&$array_or_not) { |
513 |
if (!is_array($array_or_not)) { |
514 |
$array_or_not = array($array_or_not); |
515 |
//print Dumper($array_or_not); |
516 |
//exit; |
517 |
} |
518 |
} |
519 |
|
520 |
// To return all ancestors class of an object |
521 |
// from: http://www.php.net/manual/en/function.get-parent-class.php |
522 |
function get_ancestors_class($classname) { |
523 |
$father = get_parent_class($classname); |
524 |
if ($father != "") { |
525 |
$ancestors = php::get_ancestors_class($father); |
526 |
$ancestors[] = $father; |
527 |
} |
528 |
return $ancestors; |
529 |
} |
530 |
|
531 |
// ------ override/expand php's 'include_path' setting ------ |
532 |
function add_libpath($paths) { |
533 |
|
534 |
if (!is_array($paths)) { |
535 |
$paths = array($paths); |
536 |
} |
537 |
array_push($paths, ini_get("include_path")); |
538 |
|
539 |
// determine OS |
540 |
$os = 'linux'; |
541 |
if (stristr($_SERVER["SERVER_SOFTWARE"], 'win32')) { |
542 |
$os = 'windows'; |
543 |
} |
544 |
|
545 |
$path_delimiter = ':'; |
546 |
// change path-delimiter for win32 |
547 |
if ($os == 'windows') { $path_delimiter = ';'; } |
548 |
// build new 'include_path'-string |
549 |
$path_new = join($path_delimiter, $paths); |
550 |
ini_set("include_path", $path_new); |
551 |
} |
552 |
|
553 |
// from: http://www.php.net/manual/en/function.is-subclass-of.php |
554 |
function is_subclass($child, $parent) { |
555 |
if (is_string($child)) { |
556 |
do { |
557 |
$child = get_parent_class($child); |
558 |
if (!$child) return false; |
559 |
//} while ($child == $parent); |
560 |
} while ($child != $parent); |
561 |
return true; |
562 |
} else { |
563 |
return is_subclass_of($child, $parent); |
564 |
} |
565 |
} |
566 |
|
567 |
|
568 |
} |
569 |
|
570 |
//function error_handler($errno, $errstr, $errfile, $errline) { |
571 |
function php_error_handler() { |
572 |
//print "ERROR!<br/>"; |
573 |
$error_raw = func_get_args(); |
574 |
|
575 |
//print Dumper($error_raw); |
576 |
|
577 |
$error = array( |
578 |
'level' => $error_raw[0], |
579 |
'message' => $error_raw[1], |
580 |
'file' => $error_raw[2], |
581 |
'line' => $error_raw[3], |
582 |
'context' => &$error_raw[4], |
583 |
'object_context' => &$error_raw[4][this], |
584 |
); |
585 |
$error[component] = $error[object_context]->_component_name; |
586 |
|
587 |
//$output_order = array( 'message', 'component', 'file', 'line', 'level' ); |
588 |
$output_order = array( 'message', 'file', 'line' ); |
589 |
|
590 |
/* |
591 |
$level = $error[0]; |
592 |
$context = &$error[4]; |
593 |
$cc = &$context['this']; |
594 |
*/ |
595 |
|
596 |
|
597 |
//print Dumper($context); |
598 |
//print Dumper($cc); |
599 |
//print Dumper($cc->_component_name); |
600 |
|
601 |
// by level |
602 |
//$do_trace = ($level <= 5); |
603 |
|
604 |
//$c_name = $context['this']['_component_name']; |
605 |
|
606 |
//print "c_error: $c_name<br/>"; |
607 |
|
608 |
// if component |
609 |
//$do_trace = isset($context[this][_component_name]); |
610 |
//$do_trace = isset($context->this[_component_name]); |
611 |
//$do_trace = isset($cc->_component_name); |
612 |
$do_trace = isset($error[component]); |
613 |
|
614 |
//$do_trace = 1; |
615 |
|
616 |
//print Dumper($context); |
617 |
|
618 |
if ($do_trace) { |
619 |
|
620 |
//print php::Dumper($error); |
621 |
//exit; |
622 |
|
623 |
//print "<hr/><b><font color=\"red\">ERROR:</font></b><br/>"; |
624 |
print "<b><font color=\"red\">ERROR:</font></b><br/> "; |
625 |
|
626 |
foreach ($output_order as $key) { |
627 |
print "<b>$key:</b> $error[$key]<br/>"; |
628 |
} |
629 |
|
630 |
} |
631 |
|
632 |
} |
633 |
|
634 |
|
635 |
?> |