| 1 | joko | 1.1 | <? | 
| 2 |  |  | // --------------------------------------------------------------------------- | 
| 3 | joko | 1.8 | //  $Id: Object.php,v 1.7 2003/03/01 21:13:39 joko Exp $ | 
| 4 | joko | 1.1 | // --------------------------------------------------------------------------- | 
| 5 | joko | 1.2 | //  $Log: Object.php,v $ | 
| 6 | joko | 1.8 | //  Revision 1.7  2003/03/01 21:13:39  joko | 
| 7 |  |  | //  modified loadModule (take care!) | 
| 8 |  |  | // | 
| 9 | joko | 1.7 | //  Revision 1.6  2003/02/28 04:18:13  joko | 
| 10 |  |  | //  + special-case-handling for single argument pass-in | 
| 11 |  |  | //  + testing with constructor autocalling: not used for now since weird sideeffects occoured! | 
| 12 |  |  | //     o however: rethink it completely! | 
| 13 |  |  | // | 
| 14 | joko | 1.6 | //  Revision 1.5  2003/02/27 16:36:02  joko | 
| 15 |  |  | //  re-allowed underscores ('_') in php classnames | 
| 16 |  |  | //  does this break something? | 
| 17 |  |  | // | 
| 18 | joko | 1.5 | //  Revision 1.4  2003/02/22 16:34:16  joko | 
| 19 |  |  | //  + minor fix: can seperate namespace identifier with slashes ('/') now | 
| 20 |  |  | // | 
| 21 | joko | 1.4 | //  Revision 1.3  2003/02/09 17:16:06  joko | 
| 22 |  |  | //  + handling arguments perl style ;-) | 
| 23 |  |  | // | 
| 24 | joko | 1.3 | //  Revision 1.2  2003/02/03 05:01:48  joko | 
| 25 |  |  | //  + now attributes can get passed in to the constructors | 
| 26 |  |  | // | 
| 27 | joko | 1.2 | //  Revision 1.1  2003/02/03 03:33:48  joko | 
| 28 |  |  | //  + initial commit | 
| 29 |  |  | // | 
| 30 | joko | 1.1 | // --------------------------------------------------------------------------- | 
| 31 |  |  |  | 
| 32 |  |  |  | 
| 33 |  |  | /* | 
| 34 |  |  | class DesignPattern_Object { | 
| 35 |  |  | function DesignPattern_Object() { | 
| 36 |  |  |  | 
| 37 |  |  | } | 
| 38 |  |  | } | 
| 39 |  |  | */ | 
| 40 |  |  |  | 
| 41 |  |  | function _ns2file($nsName) { | 
| 42 |  |  | if ($filename  = str_replace('::', '/', $nsName)) { | 
| 43 |  |  | return $filename; | 
| 44 |  |  | } | 
| 45 |  |  | } | 
| 46 |  |  |  | 
| 47 | joko | 1.7 | function &mkObject() { | 
| 48 | joko | 1.3 | $arg_list = func_get_args(); | 
| 49 | joko | 1.6 |  | 
| 50 |  |  | // patch arglist if all arguments are passed in as a single array | 
| 51 |  |  | if (count($arg_list) == 1 && is_array($arg_list[0])) { | 
| 52 |  |  | $arg_list = $arg_list[0]; | 
| 53 |  |  | } | 
| 54 |  |  |  | 
| 55 | joko | 1.3 | //print Dumper($arg_list); | 
| 56 |  |  | $namespacedClassname = array_shift($arg_list); | 
| 57 |  |  | if (strstr($namespacedClassname, '_')) { | 
| 58 | joko | 1.5 | //print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; | 
| 59 |  |  | //return; | 
| 60 | joko | 1.3 | } | 
| 61 |  |  | //print "class: $classname<br>"; | 
| 62 |  |  | $attributes = $arg_list; | 
| 63 | joko | 1.1 | $classname = $namespacedClassname; | 
| 64 |  |  | if (loadModule($namespacedClassname)) { | 
| 65 |  |  | $classname = str_replace('::', '_', $namespacedClassname); | 
| 66 | joko | 1.4 | $classname = str_replace('/', '_', $classname); | 
| 67 | joko | 1.1 | } | 
| 68 | joko | 1.2 | $obj = new DesignPattern_Bridge($classname, $attributes); | 
| 69 | joko | 1.6 |  | 
| 70 |  |  | // call constructor if possible | 
| 71 |  |  | // FIXME: take care for side-effects!!! | 
| 72 |  |  | /* | 
| 73 |  |  | if (method_exists($obj, 'constructor')) { | 
| 74 |  |  | $obj->constructor(); | 
| 75 |  |  | } | 
| 76 |  |  | */ | 
| 77 |  |  |  | 
| 78 | joko | 1.1 | return $obj; | 
| 79 |  |  | } | 
| 80 |  |  |  | 
| 81 | joko | 1.7 | function loadModule($namespacedClassname, $extension = 'php') { | 
| 82 | joko | 1.1 | if ($filename = _ns2file($namespacedClassname)) { | 
| 83 | joko | 1.7 | if ($extension) { | 
| 84 |  |  | $filename .= ".$extension"; | 
| 85 |  |  | } | 
| 86 |  |  |  | 
| 87 |  |  | // V1: just require | 
| 88 |  |  | //require_once($filename); | 
| 89 |  |  |  | 
| 90 |  |  | // V2: check file for existance (didn't work!!! problem with path-style conversion regarding platform running on?) | 
| 91 |  |  | /* | 
| 92 |  |  | if (file_exists($filename)) { | 
| 93 |  |  | require_once($filename); | 
| 94 |  |  | } else { | 
| 95 |  |  | //user_error("Could not load module '$namespacedClassname', file '$filename' does not exist."); | 
| 96 |  |  | } | 
| 97 |  |  | */ | 
| 98 |  |  |  | 
| 99 |  |  | // V3: wrapped through eval (weird) | 
| 100 |  |  | /* | 
| 101 |  |  | $evalstring = "return include_once('$filename');"; | 
| 102 |  |  | $result = eval($evalstring); | 
| 103 |  |  | if (!$result) { | 
| 104 |  |  | user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); | 
| 105 |  |  | return; | 
| 106 |  |  | } | 
| 107 |  |  | */ | 
| 108 |  |  |  | 
| 109 |  |  | // V4: just include | 
| 110 |  |  | if (!include_once($filename)) { | 
| 111 |  |  | // TODO: do stack-backtracing here to determine function called two or three levels before!!! | 
| 112 |  |  | user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); | 
| 113 |  |  | return; | 
| 114 |  |  | } | 
| 115 |  |  |  | 
| 116 | joko | 1.1 | return 1; | 
| 117 |  |  | } | 
| 118 |  |  | } | 
| 119 |  |  |  | 
| 120 |  |  | ?> |