/[cvs]/nfo/php/libs/org.netfrag.glib/php_extensions.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.glib/php_extensions.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Fri Apr 4 17:35:40 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.6: +55 -63 lines
fixed php croaking
moved error handling function to 'php_errors.php'
+ function php::maintenance

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed